Spring

[트러블 슈팅] This method cannot decide whether these patterns are Spring MVC patterns or not.

짱정연 2023. 11. 24. 21:49
반응형

This method cannot decide whether these patterns are Spring MVC patterns or not. If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher); otherwise, please use requestMatchers(AntPathRequestMatcher).

 

새로운 Spring Boot 프로젝트를 만들고 회원가입 API를 만들며 Spring Security와 관련하여 위와 같은 에러가 발생하였다.

Spring Security 버전이 달라지면서 RequestMatchers 작성 방법이 달라지면서 발생한 에러이다.

공식문서 참고

 

이전 프로젝트에서 사용하던 Spring Security 버전은 6.1.1이고, 현재 프로젝트에서는 6.1.5를 사용하고 있다.

 

SecurityConfig 파일에서 RequestMatchers 부분은 아래와 같이 변경해주어 에러를 해결할 수 있다.

 

기존 SecurityConfig 코드 (6.1.1 버전)

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
        // 중략 ...
        // 토큰이 없는 상태에서 요청이 가능한 API는 permitAll 설정
        .authorizeHttpRequests(authorizeRequests ->
            authorizeRequests
                .requestMatchers(
                    "/oauth/**",
                    "/admin/**"
                ).permitAll()
                .requestMatchers(PathRequest.toH2Console()).permitAll() // H2-console 화면을 사용하기 위해
                .anyRequest().authenticated()
        )
        // 중략 ...
}
}

 

변경한 SecurityConfig 코드 (6.1.5 버전)

URL을 new AntPathRequestMatcher()로 감싸주었다.

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
        // 중략 ...
        // 토큰이 없는 상태에서 요청이 가능한 API는 permitAll 설정
        .authorizeHttpRequests(authorizeRequests ->
            authorizeRequests
                .requestMatchers(
                    new AntPathRequestMatcher("/auth/**")
                )
                .permitAll()
                .requestMatchers(PathRequest.toH2Console()).permitAll() // H2-console 화면을 사용하기 위해
                .anyRequest().authenticated()
        )
        // 중략 ...
}
}

 

Reference

 

CVE-2023-34035: Authorization rules can be misconfigured when using multiple servlets

CVE-2023-34035: Authorization rules can be misconfigured when using multiple servlets HIGH | JULY 17, 2023 | CVE-2023-34035 Description Severity is high unless otherwise noted. Spring Security versions 5.8 prior to 5.8.5, 6.0 prior to 6.0.5 and 6.1 prior t

spring.io

 

 

[Spring] This method cannot decide whether these patterns are Spring MVC patterns or not.

Caused by: java.lang.IllegalArgumentException: This method cannot decide whether these patterns are Spring MVC patterns or not. If this endpoint is a

velog.io

 

반응형