솔묘
Solmi's Development Blog
솔묘
전체 방문자
오늘
어제
  • 분류 전체보기 (20)
    • Coding (10)
    • Troubleshooting (3)
    • Study (3)
    • Etc. (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

최근 댓글

최근 글

티스토리

skin · Designed By 정상우.
솔묘

Solmi's Development Blog

Troubleshooting

[Spring Security] HandlerMappingIntrospector is required to use MvcRequestMatcher

2023. 6. 15. 13:32

문제

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'A Bean named mvcHandlerMappingIntrospector of type org.springframework.web.servlet.handler.HandlerMappingIntrospector is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.' available

API 호출 시 위와 같은 예외가 발생했다.

 

살펴보니 Security Config의 requestMatchers 라인에서 예외를 던지고 있었다.

이전 프로젝트에서는 antMatchers를 통해 리소스 접근을 허용했는데, Spring Security 5.8 버전에서 antMatchers, mvcMatchers, regexMatchers가 deprecate 되었고, 6 이상부터는 remove 되었다.

나는 Spring Security 6.1 버전을 사용 중이기에 새로운 requestMatchers 메서드를 사용하였다.

http
                .cors(cors -> cors.disable())
                .csrf(csrf -> csrf.disable())
                .authorizeHttpRequests(
                        (request) -> request.requestMatchers("/users", "/auth/**")
                                .permitAll()
                                .anyRequest()
                                .authenticated()
                )
                .httpBasic(Customizer.withDefaults())
                .addFilterBefore(new JwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

 

원인

RequestMatcher 메서드들은 애플리케이션에 가장 적합한 RequestMatcher를 선택하여 구현한다. 예를 들어, Spring MVC가 classpath에 있는 경우 MvcRequestMatcher를 구현하고, Spring MVC가 없는 경우 AntPathRequestMatcher를 구현한다.

 

spring-webmvc 의존성을 가지고 있어 RequestMatcher가 MvcRequestMatche를 구현했으나, MvcHandlerMappingIntrospector Bean을 찾지 못해 HandlerMappingIntrospector is required to use MvcRequestMatcher 예외가 발생한 것으로 보인다. Spring Security는 parent 또는 root context에 있으며, Spring MVC는 child 또는 servlet context에 있기 때문에 Spring Security는 Spring MVC config를 찾을 수 없다.

 

해결

결론적으로 SecurityConfig에 @ConditionalOnWebApplication 애노테이션을 추가해 해결하였다.

아직 명확히 이해되지 않는 부분들이 있어 추후 내용들을 보강할 계획이다.

저작자표시 (새창열림)

'Troubleshooting' 카테고리의 다른 글

[Docker] executor failed running. exit code: 127  (0) 2023.08.30
[JPA] Failed to create query for method public abstract. No property found for type.  (0) 2023.07.27
    'Troubleshooting' 카테고리의 다른 글
    • [Docker] executor failed running. exit code: 127
    • [JPA] Failed to create query for method public abstract. No property found for type.
    솔묘
    솔묘
    Solmi's Development Blog

    티스토리툴바