Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register Spring HandlerInterceptor Without WebMvcConfigurationSupport

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)

like image 599
Colin M Avatar asked Jan 18 '15 19:01

Colin M


People also ask

Why is Webmvcconfigureradapter deprecated?

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.

What is interceptor registry?

an InterceptorRegistration that allows you optionally configure the registered interceptor further for example adding URL patterns it should apply to.

How can we intercept every API call in a spring boot application?

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.


2 Answers

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.

like image 136
diogo Avatar answered Sep 22 '22 09:09

diogo


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.

like image 24
Colin M Avatar answered Sep 24 '22 09:09

Colin M