- ViewPager2
- indicator
- 화살표안됨
- MariaDB
- Spring
- Android
- override
- Spring legacy
- WebSecurityConfigurerAdapter
- MacOS
- web.xml
- WebSecurityConfigurerAdapter Deprecated
- java
- Fragment
- Keymap
- 복붙안됨
- spring security
- Homebrew
- MAC
- Deprecated
- bean
- TabLayout
- CircleIndicator
- MariaDB 설치
- 안드로이드
- Windows
- SharedPreferences
- Today
- Total
초보 개발자 정선익의 개발일지
spring security WebSecurityConfigurerAdapter Deprecated 이슈 본문
Spring Security를 사용중 WebSecurityConfigurerAdapter를 사용하려는데 Deprecated가 된걸 알았습니다.
SecurityConfig.java
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
@AllArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
/* role setting */
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
String password = passwordEncoder().encode("1234");
auth.inMemoryAuthentication().withUser("user").password(password).roles("user");
auth.inMemoryAuthentication().withUser("admin").password(password).roles("admin");
}
/* templates security setting */
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}
/* encryption setting */
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
/* url roles setting */
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/","/register").permitAll()
.antMatchers("/user").hasRole("user")
.antMatchers("/admin").hasRole("admin")
.anyRequest().authenticated()
.and()
.formLogin();
}
}
관련 문서를 읽어보니..
기존에는 WebSecurityConfigurerAdapter 를 상속 후, 메소드를 오버라이딩하여 설정하는 방식이었는데
이제는 SecurityFilterChain 을 빈으로 등록하는 방식을 권장한다고 합니다.
변경 후
SecurityConfig.java
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
@EnableWebSecurity
@Configuration
@AllArgsConstructor
public class SecurityConfig {
/* role setting */
@Bean
public InMemoryUserDetailsManager userDetailsService() {
String password = passwordEncoder().encode("1234");
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("user")
.password(password)
.roles("user")
.build());
manager.createUser(User.withUsername("admin")
.password(password)
.roles("admin")
.build());
return manager;
}
/* templates security setting */
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}
/* encryption setting */
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
/* url roles setting */
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/user").hasRole("user")
.antMatchers("/admin").hasRole("admin")
.anyRequest().authenticated()
.and()
.formLogin();
return http.build();
}
}
참고
많은 도움을 얻었습니다.
https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter
Spring Security without the WebSecurityConfigurerAdapter
<p>In Spring Security 5.7.0-M2 we <a href="https://github.com/spring-projects/spring-security/issues/10822">deprecated</a> the <code>WebSecurityConfigurerAdapter</code>, as we encourage users to move towards a component-based security configuration.</p> <p
spring.io
WebSecurityConfigurerAdapter (spring-security-docs 5.7.4 API)
Provides a convenient base class for creating a WebSecurityConfigurer instance. The implementation allows customization by overriding methods. Will automatically apply the result of looking up AbstractHttpConfigurer from SpringFactoriesLoader to allow deve
docs.spring.io