@WebFilter
- its a java anno, not Spring one
- only work with embedded servlet container
- can not work with @Order, cause its not Spring Controlled
- need @ServletComponentScan with BootStrap Class
- not suggest
Use FilterRegistrationBean
- Its a Spring way
- Seperate Configuration Class
- each filter is a bean
- set Order(min---max), set Url Pattern
- suggest
Filter with Autowire
- can not use constructor autowire
- need to add one more init FilterBean in FilterRegistrationBean
- Filter no need annos
Code Piece
WebFilterConfiguration
@SpringBootConfiguration
public class WebFilterConfiguration {
@Bean
public FilterRegistrationBean<BasicAuthFilter> basicAuthFilterFilterRegistrationBean() {
FilterRegistrationBean<BasicAuthFilter> filterFilterRegistrationBean =
new FilterRegistrationBean<>();
filterFilterRegistrationBean.setFilter(getBasicAuthFilter());
filterFilterRegistrationBean.addUrlPatterns("/open/*");
filterFilterRegistrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 1);
return filterFilterRegistrationBean;
}
@Bean
public BasicAuthFilter getBasicAuthFilter() {
return new BasicAuthFilter();
}
}
Filter
public class BasicAuthFilter implements Filter {
@Autowired private PathMatcher pathMatcher;
@Autowired private ResourceService resourceService;
@Autowired private UrlPathHelper urlPathHelper;
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(
......
}
Cont.
- use proxy to create/fetch filter bean
- use custom anno to combine @WebFilter
本文由 Ivan Dong 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jul 7, 2023 at 04:09 am