Java/spring
spring boot 3 migration#02 WebSecurityConfigurerAdapter
LichKing
2023. 5. 14. 15:57
spring security 는 확장을 위해 WebSecurityConfigurer 라는 인터페이스를 제공하고 있다. spring 은 이전부터 확장 인터페이스에 대해 좀 더 용이하게 사용하도록 기본 구현을 하는 구현체들(추상클래스 포함)을 제공하는데 WebSecurityConfigurer 는 WebSecurityConfigurerAdapter 라는 추상 클래스가 그 역할을 한다. 확장이 필요한 경우 WebSecurityConfigurer 를 직접 구현하는 경우는 거의 없다. 이는 WebSecurityConfigurer 의 javadoc 에서부터 명시하고있다.
Allows customization to the WebSecurity. In most instances users will use EnableWebSecurity and either create a Configuration that extends WebSecurityConfigurerAdapter or expose a SecurityFilterChain bean. Both will automatically be applied to the WebSecurity by the EnableWebSecurity annotation. Since https://docs.spring.io/spring-security/site/docs/5.7.0-M2/api/org/springframework/security/config/annotation/web/WebSecurityConfigurer.html |
다만 spring security 5.7.0 부터 WebSecurityConfigurerAdapter 는 deprecate 됐으며, 버전 6 에 와서는 클래스가 아예 제거되어 버렸다. 그래서 spring boot 애플리케이션에서 WebSecurityConfigurerAdapter 를 이용하고 있었다면 이에 대한 마이그레이션도 필요하다. spring security 에서는 SecurityFilterChain 을 이용한 설정을 권장하고 있다.
// 기존 WebSecurityConfigurerAdapter 를 이용한 설정
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());
}
}
// SecurityFilterChain 을 이용한 방식
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());
return http.build();
}
}
WebSecurifyConfigurerAdapter 를 제거한 이유나 추가적인 마이그레이션 방법은 따로 가이드( https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter )가 있으니 참고하도록 하자.