Back-End/spring
[spring] 스프링 시큐리티(Spring Security) - WebSecurityConfigurerAdapter is deprecated 해결 방법
철갑
2022. 9. 12. 15:37
728x90
▶The type WebSecurityConfigurerAdapter is deprecated
Spring Security 설정 도중 아래와 같이 WebSecurityConfigurerAdapter가 deprecated 된 것을 확인할 수 있었습니다.

▶ 어떻게 바뀌었을까?
공식문서를 확인해보니 기존에는 WebSecurityConfigurerAdapter를 상속받아 설정을 overriding 했다면 지금은 SecurityFilterChain를 Bean으로 등록해서 사용하라고 나와있습니다.

공식문서에서 권장하는 방식으로 코드를 변경해보았습니다.
▶ 적용 방법
- 변경전 (WebSecurityConfigurerAdapter 상속)
@Configuration
@EnableWebSecurity
public class SecurityStudy extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/user/**").authenticated()
.antMatchers("/manager/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/login");
}
}
- 변경후(SecurityFilterChain를 Bean으로 등록)
@Configuration
@EnableWebSecurity
public class SecurityConfig{
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/user/**").authenticated()
.antMatchers("/manager/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/login");
return http.build();
}
}
▶참고
WebSecurityConfigurerAdapter (spring-security-docs 5.7.3 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
728x90