책을 이용해 Spring Security를 공부했는데 버전 차이로인해 많은 어려움을 겪엇다.
공부했던 책은 2020년 11월에 출판된 책이였고 이 당시 부트는 버전 2버전대 시큐리티 버전은 5버전대를 사용하여 내가 학습할 당시 버전과 차이가 많이나 책을 읽고 공부하는데 많은 어려움이 있었다.
이 글은 그중에서도 WebSecurityConfigurerAdapter로 인해 겪은 문제과 해결법을 공유하기 위해 작성했다.
개발 환경
Spring Boot 3.2.1
Spring Security 6.2.1
WebSecurityConfigurerAdapter는 어디에?
책에서는 http security 설정 하기 위해서 WebSecurityConfigurerAdapter를 상속 cofigure 메소드를 오버라이딩해 http 인증, 인가 설정하라고 적혀있지만 내 환경에서는 WebSecurityConfigurerAdapter 파일이 존재하지 않아 상속을 할 수 없었다.
문제를 해결 하기 위해 검색을 한 결과 Spring blog에서 해답을 찾을 수 있었는데, Spring Security 5.7.0-M2 부터 사용자가 컴포넌트 기반 보안 구성으로 전환 하도록 권장하기 때문에 더이상 WebSecurityConfigureAdapter를 사용하지 않는다고 적혀있다.
SecurityFilterChain Bean을 생성하여 Http Security 설정하기
http security 설정을 하기위해서는 이제부터 Spring Security 5.4에 도입된 SecurityFilterChain 빈을 생성하여 설정하라고 명시 되어있다.
이전 방식의 Http Security 설정 코드
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());
}
}
새로운 방식의 Http Security 설정 코드
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());
return http.build();
}
}
WebSecurityCustomizer Bean을 생성하여 WebSecurity 설정하기
WebSecurityConfigurerAdapter가 사라지면서 위 처럼 Web Security 설정법도 바뀌었는데,
WebSecurityCustomizer Bean을 생성하여 설정하라고 명시되어있다.
이전 방식의 WebSecurity 설정 코드
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/ignore1", "/ignore2");
}
}
새로운 방식의 WebSecurity 설정 코드
@Configuration
public class SecurityConfiguration {
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
}
}
참고한 자료
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
Configuration Migrations :: Spring Security
In 6.0, @Configuration is removed from @EnableWebSecurity, @EnableMethodSecurity, @EnableGlobalMethodSecurity, and @EnableGlobalAuthentication. To prepare for this, wherever you are using one of these annotations, you may need to add @Configuration. For ex
docs.spring.io
'Spring > Security' 카테고리의 다른 글
Spring Security Path별 Filter Chain 적용 (0) | 2024.02.06 |
---|