Spring

WebSecurityConfigurerAdapter Deprecated 해결

NYoun 2022. 12. 6. 16:36

SpringSecurity를 사용하던 중 Security 설정에서 상속받아 사용하던 WebSecurityConfigurerAdapter가 Deprecated되었다.

 

처음 Spring Security를 공부했을 때 WebSecurityConfigurerAdapter를 상속받아 설정하는걸 배웠었고 프로젝트에 구현할때도 그렇게 했었는데 어느날 갑자기 선이 쭉 생기면서 Deprecated 되었다는 것을 발견했다.

 

그래서 찾아보니 Spring Security 5.7.0부터 사용을 권장하지 않는다는 공식 문서를 찾게 되었다.

 

공식문서에는 더이상 사용을 하지 않는 다는 말과 함께 어떻게 수정해 사용하면 되는지에 대해 설명해뒀다.

 

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를 사용할때의 설정

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**", "/js/**", "/css/**");
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/user/**")
                .access("hasRole('ROLE_USER')")
            .and()
                .formLogin()
                .successHandler(loginSuccessHandler())
            .and()
                .logout()
                .logoutSuccessUrl("/")
            .and()
                .exceptionHandling().accessDeniedPage("/");
    }
}

 

WebSecurityConfigurerAdapter를 사용하지 않을때의 설정

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers("/resources/**", "/js/**", "/css/**");
    }
    
    @Bean
    protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/user/**")
                .access("hasRole('ROLE_USER')")
            .and()
                .formLogin()
                .successHandler(loginSuccessHandler())
            .and()
                .logout()
                .logoutSuccessHandler(logoutSuccessHandler())
            .and()
                .exceptionHandling().accessDeniedpage("/");
        
        
        return http.build();
    }
}

 

 

코드만 봤을때는 크게 차이는 없다. 코드상으로는 상속받는게 없으니 override하지 않고 Bean으로 등록해준다는 정도의 차이점만 발생한다고 볼 수 있다.

이 외 설정에 대해서도 공식문서에 잘 나와있기 때문에 공식문서를 잘 확인한다면 큰 문제는 없을것 같다.

 

 

몇번 테스트를 해보면서 그런적은 없는데 이 설정 방식에 대해 알아보던 중 이렇게 bean으로 등록했을 때 오류가 발생하는 경우도 있는것 같다.

 

found webSecurityConfigurerAdapter as well as securityFilterChain. please select just one.

이런 오류가 발생하는 경우가 있다는데 그럴때는 securityConfig 클래스에

@ConditionalOnDefaultWebSecurity

@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)

이 두 어노테이션을 추가해주고

securityFilterChain 메소드에 

@Order(SecurityProperties.BASIC_AUTH_ORDER)

어노테이션을 추가해주면 해결이 된다고 한다.