사이트 개발/트러블슈팅

[Spring-Security] WebSecurityConfigurerAdapter 에러, 지원중단, 대체방법

OneCrazyman 2024. 1. 17. 17:34

 

 

인프런의 강좌들은 대부분 시간이 좀 지나온터라 최신버전의 스프링으로 환경을 세팅한 경우에
강좌를 따라가다보면 빨간줄이 생길때가 있다. 강좌에 자주 등장하는 WebSecurityConfigurerAdapter를 extends하는 경우는 Spring에서 더이상 지원하지 않아 다음과 같은 방식으로 진행하였다. 

 

Spring Security 5.7.x 부터 WebSecurityConfigurerAdapter 는 Deprecated

사용할 수 없다

 

스프링에서 추천하는 방식은 필터체인과 빈 등록을 통해 진행하는 것이다.

 

 

 

해결

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8-jwt/dashboard

 

정은구님의 JWT 인프런 강좌에서 사용한 WebSecurityConfigurerAdapter를 아래로 수정하여 해결하였다.

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    /**
     * Spring Security 5.7.x 부터 WebSecurityConfigurerAdapter 는 Deprecated.
     * -> SecurityFilterChain, WebSecurityCustomizer 를 상황에 따라 빈으로 등록해 사용한다.
     */
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
        http
                .authorizeHttpRequests(authz -> authz
                        .requestMatchers("/api/hello").permitAll()
                        .anyRequest().authenticated()
                )
        ;
        return http.build();
    }
}

 

 

 

참고 및 출처

https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter/

 

Spring Security without the WebSecurityConfigurerAdapter

In Spring Security 5.7.0-M2 we deprecated the WebSecurityConfigurerAdapter, as we encourage users to move towards a component-based security configuration. To assist with the transition to this new style of configuration, we have compiled a list of common

spring.io

https://github.com/SilverNine/spring-boot-jwt-tutorial

 

GitHub - SilverNine/spring-boot-jwt-tutorial

Contribute to SilverNine/spring-boot-jwt-tutorial development by creating an account on GitHub.

github.com