Spring-Boot使用嵌入式容器,那怎么配置自定义Filter呢 (2)

@WebFilter注解是Servlet中提供的注解,Spring也支持这个注解。@WebFilter能进行细粒度的进行配置,比上面的方式更加灵活。

@Configuration public class WebConfig { //可以自定义url-pattern @WebFilter(urlPatterns="/*") @Order(Ordered.LOWEST_PRECEDENCE - 2) //这边如果不加`@Configuration`,需要通过`@ServletComponentScan`扫描`Listener`、`Filter`和`Servlet`这三个组件 @Configuration public class TimeConsumingCalculationFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //your logic } } @WebFilter(urlPatterns="/*") @Order(Ordered.LOWEST_PRECEDENCE - 2) //这边如果不加`@Configuration`,需要通过`@ServletComponentScan`扫描`Listener`、`Filter`和`Servlet`这三个组件 @Configuration public class TimeConsumingCalculationFilter2 implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //your logic } } }

通过 @WebFilter 注解的方式配置Filter简单,而且能够自定义url-pattern和拦截顺序。

方式三:使用FilterRegistrationBean配置 @Configuration public class WebConfig { private static Logger logger = LoggerFactory.getLogger(WebConfig.class); @Bean public FilterRegistrationBean<Filter> filter1() { FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setName("filter1"); registrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 2); registrationBean.setFilter(new TimeConsumingCalculationFilter()); registrationBean.addUrlPatterns("/foo/*"); return registrationBean; } @Bean public FilterRegistrationBean<Filter> filter2() { FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setName("filter2"); registrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 3); registrationBean.setFilter(new TimeConsumingCalculationFilter1()); registrationBean.addUrlPatterns("/*"); registrationBean.addInitParameter("key1","value1"); registrationBean.addInitParameter("key2","value2"); //通过Servlet name匹配Filter,不建议使用 registrationBean.addServletNames("name1"); return registrationBean; } }

注意点:

FilterRegistrationBean 与 Filter 之间是一对一关系。

如果存在多个 FilterRegistrationBean 需要调用其 setName(String name) 为其声明唯一名称,否则只有第一个注册成功的有效。

如果需要保证调用顺序可通过调用其 setOrder(int order) 方法进行设置。

方式四:使用DelegatingFilterProxyRegistrationBean方式 @Configuration public class WebConfig { @Bean("myFilter") //配置了DelegatingFilterProxyRegistrationBean后,这种方式配置的Filter不会生效了,只会拦截/foo/*的请求 public Filter myFilter(){ return new TimeConsumingCalculationFilter(); } @Bean public DelegatingFilterProxyRegistrationBean delegatingFilterProxyRegistrationBean(){ DelegatingFilterProxyRegistrationBean filterProxy = new DelegatingFilterProxyRegistrationBean("myFilter"); filterProxy.addUrlPatterns("/foo/*"); filterProxy.addInitParameter("targetFilterLifecycle","true"); filterProxy.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico"); filterProxy.setDispatcherTypes(DispatcherType.REQUEST); return filterProxy; } }

FilterRegistrationBean和DelegatingFilterProxyRegistrationBean区别:

FilterRegistrationBean通过onStartup方法直接注册filter。

DelegatingFilterProxyRegistrationBean是将DelegatingFilterProxy注册到Servlet3.0+的容器中,同时实现了ApplicationContextAware接口,实例ApplicationContext通过通过传入自定义filter的名称查找对应的bean,并生成相应bean的代理对象。

触类旁通

添加自定义Servlet 也可采用方法一@WebServlet 或者ServletRegistrationBean

添加自定义Listener也可以采用方法一 @WebListener或者ServletListenerRegistrationBean ,注意监听事件是泛型

其他相关类

ServletComponentRegisteringPostProcessor

ServletComponentHandler

WebListenerHandler

WebFilterHandler

WebServletHandler

参考

https://blog.csdn.net/loveForever_xiang/article/details/101270633

https://www.liangzl.com/get-article-detail-121998.html

https://mp.weixin.qq.com/s/t8WdKEkYJuRApeEEOvXR_A

https://blog.csdn.net/andy_zhang2007/article/details/90399870

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssypw.html