Spring Security WebSecurityConfigurerAdapter / cors, csrf Deprecated 해결하기

Tech/Java 2023. 8. 20. 13:06
728x90
728x90

서론

최근 토이 프로젝트 진행중에 간단하게 회원기능을 구현하고자 Security를 사용하는데
이전 사용했던 WebSecurityConfigurerAdapter을 상속받아 구현하던 Config설정에 바뀐 부분이 있고
또한 시큐리티를 통한 권한 확인이나 로그인, 로그아웃 등 기타 작업등은 하지 않을 계획이라
csrf(), cors() disable을 설정하는 과정에서도 Deprecated된 것을 확인하여 기록하고자 함
 
아래는 각각 WebSecurityConfigurerAdapter와, Spring Security 6.1.2버전에서의
Deprecated API를 정리해둔 공식 docs
 
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://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/config/annotation/web/builders/HttpSecurity.html#csrf() 

HttpSecurity (spring-security-docs 6.1.2 API)

securityContext Deprecated, for removal: This API element is subject to removal in a future version. Returns: the SecurityContextConfigurer for further customizations Throws: Exception

docs.spring.io

 
 

본론

구글링을 통한 blog나 stackoverflow도 좋지만
아래처럼 공식문서를 통해 간단하게 해결할 수 있는 부분이다.

 
 
딱히 무언가 권한이나 기타 설정들은 하지 않을 것이기 때문에 아래처럼 Config파일을 구성할 수 있었다.
 

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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
        return http
                .csrf((csrf) -> csrf.disable())
                .cors((cors) -> cors.disable())
                .build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
728x90
300x250
mag1c

mag1c

2년차 주니어 개발자.

방명록