두 손끝의 창조자

HandlerMappingIntrospector is required 본문

Spring

HandlerMappingIntrospector is required

codinglog 2021. 1. 11. 15:01

Spring MVC를 사용하고 보안을 위해 스프링 시큐리트를 도입했다면 볼 수 있는 예외

스프링 시큐리티의 HttpSecurity 설정 중 matcher로 MvcRequestMatcher를 사용하면 발생한다.

http.authorizeRequests(
    authorize -> authorize
        .mvcMatchers("/api/").permitAll()
        .mvcMatchers("/hello").permitAll()
        .mvcMatchers("/admin").authenticated()
)

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

예외를 잘 살펴보면 MvcRequestMatcher 를 사용하기 위해서는 HandlerMappingIntrospector 타입의 빈이 있어야 한다는 이야기 이다. 스프링 시큐리티와 스프링 MVC를 공유하고 있는 ApplicationContext에 설정되어야 한다는 이야기도 있다.

왜냐? 스프링 시큐리티에서 스프링 MVC의 패턴을 사용해서 mvc의 mapping 패턴과 시큐리티의 패턴을 일치시켜서 혼돈되지 않게 하기 위해서이다.

스프링 시큐리티는 root 컨텍스트에서 초기화 되고 mvc는 자식 컨텍스트에서 초기화 되므로 스프링 시큐리티가 자식놈인 mvc를 알 수 있는 방법이 없다.그래서 중간다리 역할을 해주는 HandlerMappingIntrospector 라는 놈이 필용하다.

@Configuration
public class AppConfig {
    @Bean(name = "mvcHandlerMappingIntrospector")
    public HandlerMappingIntrospector mvcHandlerMappingIntrospector() {
        return new HandlerMappingIntrospector();
    }
}

 

반응형
Comments