[수미수의 개발 브로구]

[Spring] 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) 본문

Language & Framework/Spring

[Spring] 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)

수미수 2023. 11. 4. 20:58
반응형

오류 내용

  스프링 부트 3.1.4 와 스프링 시큐리티 6.1.4 기반의 프로젝트에서 스프링 시큐리티의 securityFilterChain 을 통해서 미인증 요청 URI 패턴을 등록 후 프로젝트를 시작 하니 아래와 같은 오류가 발생 

 

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)

 

해결 방안

  스프링 시큐리티 버전으로 인해 해당 문제가 발생된것을 확인 하였고, securityFilterChain의 인증 미체크 패턴을 String 이 아닌 AntPathRequestMatcher 객체를 사용하여 해결 하였다.

 

변경 전 소스

 @Bean
    @Throws(Exception::class)
    fun securityFilterChain(http: HttpSecurity): SecurityFilterChain? {


        http.csrf { it.disable() }
            .authorizeHttpRequests{ request ->
                request.requestMatchers("/api/**")
                    .permitAll().anyRequest().authenticated()
            }
            .sessionManagement { manager ->
                manager.sessionCreationPolicy(
                    STATELESS
                )
            }
        return http.build()
    }

 

변경 후 소스

@Bean
    @Throws(Exception::class)
    fun securityFilterChain(http: HttpSecurity): SecurityFilterChain? {


        http.csrf { it.disable() }
            .authorizeHttpRequests{ request ->
                request.requestMatchers(
                    AntPathRequestMatcher("/"),
                    AntPathRequestMatcher("/css/**"),
                    AntPathRequestMatcher("/images/**"),
                    AntPathRequestMatcher("/js/**"),
                    AntPathRequestMatcher("/h2-console/**"),
                    AntPathRequestMatcher("/profile")
                ).permitAll().anyRequest().authenticated()
            }
            .sessionManagement { manager ->
                manager.sessionCreationPolicy(
                    STATELESS
                )
            }

        return http.build()
    }
반응형