개발 환경
- SpringBoot 2.7.0
- Java11
- SpringSecurity 5.7
문제
Intellij IDE 에서 사진과 같이 WebSecurityConfigurerAdapter extends 상속 부분에 취소선이 생겼다.
이유는 SpringSecurity 5.7에서는 WebSecurityConfigurerAdapter를 더 이상 지원하지 않기 때문이다.
implements 와 다르게 부모의 메소드를 반드시 오버라이딩하지 않아도 되는 extends라 그런지 실행에 있어서는 현재로서는 에러가 발생하지는 않는다.
그래도 뭔가 취소선이 있으면 고쳐주게 생겼으므로.. 어떻게 고치는지 알아보자
해결
공식 홈페이지해당 문서에 기존 코드를 어떻게 바꿔야 하는지 자세히 나와있다.
참고 : Spring Security without the WebSecurityConfigurerAdapter
요약하자면 SpringSecurity 5.4부터 SecurityFilterChain bean을 생성하여 HttpSecurity를 구성할 수 있는 기능이 생겼다고 한다. 그래서 우리는 5.7 버전 이상이므로 코드를 변경해보자.
[변경 코드]
@EnableWebSecurity
public class SecurityConfig {
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().antMatchers("/h2-console/**", "/favicon.ico");
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/hello").permitAll()
.anyRequest().authenticated();
return http.build();
}
}
정리
HttpSecurity, WebSecurity, LDAP Authentication, JDBC Authentication 등의 많은 Config가 bean으로 변경이 되었으므로 변경 코드 및 공식 사이트를 참고하면 된다.