오랜만에 스프링 시큐리티 설정을 건드리는데 원래 쓰던 mvcMatchers 메소드가 가버렸다.
Spring Security 5.8부터 antMatchers, mvcMatchers, and regexMatchers 가 deprecated되고 6부터는 사라질 예정이다.
위의 메소드 대신 새롭게 사용될 requestMatchers 메소드는 authorizeHttpRequests, authorizeRequests, CSRF configuration, WebSecurityCustomizer와 RequstMatcher 메서드가 있는 위치에 추가되었다.
requestMatchers는 애플리케이션의 클래스 경로에 Spring MVC가 있는 경우 mvcRequestMatcher 구현을 선택하고 없는 경우 AntPathRequestMatcher 구현을 선택해 더 안전한 기본값을 가진다.
기존 코드
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final UserDetailsService userDetailsService;
private final DataSource dataSource;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeRequests()
.mvcMatchers("/", "sign-up", "check-email-token",
"/email-login", "/check-email-login", "/login-link", "/login-by-email","/search/team").permitAll()
.mvcMatchers(HttpMethod.GET, "/profile/*").permitAll()
.anyRequest().authenticated();
// ...
return http.build();
}
변경 코드
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector).servletPath("/path");
http
.authorizeHttpRequests(authorize -> authorize
.requestMatchers(antMatcher("/"), antMatcher("/sign-up")).permitAll()
.anyRequest().authenticated());
return http.build();
}
}
antMatcher 없이 쓰면 mvcMatcher가 잡혀야되는데 H2 console에 접근하는 웹주소인 JakartaWebServlet=[ /my-h2-console/ *]와 DispatcherServlet=[ / ]가 일치한다고 최근 스프링 시큐리티부터 servlet을 하나만 보호해주도록 바껴서 더 알아보고 적용하도록 해야겠다 ;;
참고
https://docs.spring.io/spring-security/reference/5.8/migration/servlet/config.html