Register Spring HandlerInterceptor Without WebMvcConfigurationSupport

前端 未结 2 2009
清酒与你
清酒与你 2021-01-11 20:15

I\'m trying to register an instance of HandlerInterceptor in Spring using Java Config without extending WebMvcConfigurationSupport. I\'m c

相关标签:
2条回答
  • 2021-01-11 21:14

    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.

    0 讨论(0)
  • 2021-01-11 21:14

    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.

    0 讨论(0)
提交回复
热议问题