I am trying to register an instance of HandlerInterceptor in Spring using Java Config without the WebMvcConfigurationSupport extension. I am creating a library with annotation that, when added to the @Configuration class @Configuration registers an interceptor that processes the security annotation.
I had an implementation using WebMvcConfigurationSupport#addInterceptors , but this was contrary to other automatic work in spring, and redefined part of the applicationโs own logic. It also seems incredibly heavy for something that should be simple. I'm trying now:
@Configuration public class AnnotationSecurityConfiguration { @Autowired private RequestMappingHandlerMapping requestMappingHandlerMapping; @PostConstruct public void attachInterceptors() { requestMappingHandlerMapping.setInterceptors(new Object[] { new SecurityAnnotationHandlerInterceptor() }); } }
However, it seems that the interceptor is registering in a completely different instance of RequestMappingHandlerMapping than the one that the application actually uses for web requests. Also, when using BeanFactoryPostProcessor I get a NullPointerException in HealthMvcEndpoint when trying to beanFactory.getBean(RequestMappingHandlerMapping.class)
source share