java 在没有 WebMvcConfigurationSupport 的情况下注册 Spring HandlerInterceptor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28013671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Register Spring HandlerInterceptor Without WebMvcConfigurationSupport
提问by Colin M
I'm trying to register an instance of HandlerInterceptor
in Spring using Java Config withoutextending WebMvcConfigurationSupport
. I'm creating a library with an annotation that, when added to a @Configuration
class, registers an interceptor that handles a security annotation.
我正在尝试HandlerInterceptor
使用 Java Config 在 Spring 中注册一个实例而不扩展WebMvcConfigurationSupport
. 我正在创建一个带有注释的库,当添加到@Configuration
类中时,它会注册一个处理安全注释的拦截器。
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:
我有一个使用 的实现WebMvcConfigurationSupport#addInterceptors
,但这与 spring 中的其他自动工作相冲突,并覆盖了应用程序自己的一些逻辑。对于应该简单的事情来说,它似乎也非常沉重。我现在正在尝试:
@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)
但是,拦截器注册的实例似乎与RequestMappingHandlerMapping
应用程序实际用于 Web 请求的实例完全不同。此外,当作为 a 实现时BeanFactoryPostProcessor
,我NullPointerException
在HealthMvcEndpoint
尝试时获得了beanFactory.getBean(RequestMappingHandlerMapping.class)
采纳答案by Colin M
Edit: This class has since been deprecated. See @bosco answerbelow for the Spring 5 equivalent.
编辑:此课程已被弃用。有关Spring 5 等效项,请参阅下面的@bosco 答案。
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.
在 spring boot 中,所有类型的 bean 都会WebMvcConfigurer
被自动检测并且可以修改 MVC 上下文。
回答by bosco
Just stating @Blauhirn's comment, WebMvcConfigurerAdapter
is deprecated as of version 5.0:
仅说明@Blauhirn 的评论,WebMvcConfigurerAdapter
自 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
从 5.0
WebMvcConfigurer
开始弃用具有默认方法(由 Java 8 基线实现)并且可以直接实现而无需此适配器
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.
另外,如此处所述@EnableWebMvc
,如果您想保留 MVC 的 Spring Boot 自动配置,请不要使用 注释。