스프링 시큐리티를 구현할 때 WebSecurityConfigurerAdapter
를 상속받아 사용했다.
하지만 Spring Security 5.7 이상에서는 이를 지원하지 않는다.
SecurityFilterChain
를 이용해 스프링 시큐리티를 구현해야 한다(공식문서 참고).
기존 코드에서 해당 부분의 수정이 필요하다.
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/register").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
SecurityFilterChain
을 이용해 구현한다.
@Configuration
@EnableWebSecurity
public class WebSecurityConfig{
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
// 정적 자원에 스프링 시큐리티 필터 규칙을 적용하지 않도록 설정
return (web) -> web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/register").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
return http.build();
}
}