问题描述
如果我像这样做的话
$ $ $ $ $ $ $ $ $ $ $ $ b $ @ $ {
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
$ b我可以通过在Spring的ServletHandler.java中放置一个断点来验证它的位置。
chain = getFilterChain(baseRequest,target,servlet_holder);
我只是想知道不是使用 @Componenent和@Order
,而是定义Filter bean的Application上下文。
如何设置过滤器的顺序?
请参阅示例:
在您的类ServletInitializer :
@Bean
FilterRegistrationBean requestLogFilter(){$ b $ final FilterRegistrationBean reg = new FilterRegistrationBean(createRequestLogFilter ));
reg.addUrlPatterns(/ *);
reg.setOrder(1); //定义过滤器执行顺序
return reg;
$ b $Bean $ b $ public RequestLogFilter createRequestLogFilter(){
return new RequestLogFilter();
}
我的过滤器名称是requestLogFilter
警告:请勿在类Filter中使用@Component注释。
I'm trying to insert (at first position) a simple custom Cors filter inside the spring filter chain.
If I do it like this
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
it works perfectlyI can verify it by putting a breakpoint in Spring's ServletHandler.java where there the line
chain=getFilterChain(baseRequest, target, servlet_holder);
I was just wondering if I wish not to use @Componenent and @Order
and instead defining the Filter bean the Application context. How can I set the order of the filters ?
See example:In your class ServletInitializer:
@Bean
public FilterRegistrationBean requestLogFilter() {
final FilterRegistrationBean reg = new FilterRegistrationBean(createRequestLogFilter());
reg.addUrlPatterns("/*");
reg.setOrder(1); //defines filter execution order
return reg;
}
@Bean
public RequestLogFilter createRequestLogFilter(){
return new RequestLogFilter();
}
the name of my filter is "requestLogFilter"
Warning: Don't use @Component annotation at the class Filter.
这篇关于SpringBoot如何设置没有注释的Filter的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!