I'm trying to register an instance of HandlerInterceptor
in Spring using Java Config without extending WebMvcConfigurationSupport
. I'm creating a library with an annotation that, when added to a @Configuration
class, registers an interceptor that handles a security annotation.
I had an implementation using WebMvcConfigurationSupport#addInterceptors
, but that conflicted with other automatic workings in spring, and overrode some of the application's own logic. It also seems incredibly heavy for something that should be simple. I'm now trying:
@Configuration
public class AnnotationSecurityConfiguration {
@Autowired private RequestMappingHandlerMapping requestMappingHandlerMapping;
@PostConstruct
public void attachInterceptors() {
requestMappingHandlerMapping.setInterceptors(new Object[] {
new SecurityAnnotationHandlerInterceptor()
});
}
}
However, it appears that the interceptor gets registered with a completely different instance of RequestMappingHandlerMapping
than the one the application actually uses for web requests. Additionally, when implemeted as a BeanFactoryPostProcessor
, I get a NullPointerException
in HealthMvcEndpoint
when I try beanFactory.getBean(RequestMappingHandlerMapping.class)
Solution. As mentioned, the WebMvcConfigurer interface, starting with Spring 5, contains default implementations for all its methods. As a result, the abstract adapter class was marked as deprecated.
an InterceptorRegistration that allows you optionally configure the registered interceptor further for example adding URL patterns it should apply to.
To work with interceptor, you need to create @Component class that supports it and it should implement the HandlerInterceptor interface. preHandle() method − This is used to perform operations before sending the request to the controller. This method should return true to return the response to the client.
Just stating @Blauhirn's comment, WebMvcConfigurerAdapter
is deprecated as of version 5.0:
Deprecated as of 5.0
WebMvcConfigurer
has default methods (made possible by a Java 8 baseline) and can be implemented directly without the need for this adapter
Refer to the new way to do it:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyCustomInterceptor())
// Optional
.addPathPatterns("/myendpoint");
}
}
Plus, as stated here, do not annotate this with @EnableWebMvc
, if you want to keep Spring Boot auto configuration for MVC.
Edit: This class has since been deprecated. See @bosco answer below for the Spring 5 equivalent.
Figured it out, the solution is to use, simply:
@Configuration
public class AnnotationSecurityConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SecurityAnnotationHandlerInterceptor());
}
}
In spring boot, all beans of type WebMvcConfigurer
are automatically detected and can modify the MVC context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With