SpringBoot注册Filter的方法有哪些
SpringBoot注册Filter的方法有哪些
小编给大家分享一下SpringBoot注册Filter的方法有哪些,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!
springboot版本:2.2.5
一、filter注册
springboot中添加filter有两种方式:
1、实现方法一
package com.example.demo.filter1; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.springframework.stereotype.Component; /** * Springboot实现filter的方式一: * 实现Filter接口,并使用@Component注解 * 需要注意的是:这个方式注册的filter的执行顺序优先级较低 * @author Administrator * */ @Component public class FilterSample1 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("这里是"+this.getClass().getName()+"的init"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("这里是"+this.getClass().getName()+"的doFilter"); chain.doFilter(request,response); } @Override public void destroy() { System.out.println("这里是"+this.getClass().getName()+"的doFilter"); } }
2、实现方法二
package com.example.demo.filter2; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class FilterSample2 implements Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("这里是"+this.getClass().getName()+"的doFilter"); chain.doFilter(request,response); } }
package com.example.demo.filter2; import java.util.ArrayList; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Springboot注册filter方法二: * 同样要实现filter接口,但filter注册使用@Bean的方式进行注册,可以设置filter顺序 * @author Administrator * */ @Configuration public class WebConfig { @Bean public FilterRegistrationBean<FilterSample2> filterSample2() { FilterRegistrationBean<FilterSample2> registrationBean = new FilterRegistrationBean<FilterSample2>(); FilterSample2 myFilter = new FilterSample2(); registrationBean.setFilter(myFilter); ArrayList<String> urls = new ArrayList<>(); urls.add("/*");//配置过滤规则 registrationBean.setUrlPatterns(urls); registrationBean.setOrder(3); return registrationBean; } }
二、Springboot自带filter
Springboot默认启用了三个filter,他们是requestContextFilter,characterEncodingFilter,formContentFilter,可以通过在application.yml中设置debug: true来验证如下:
2020-03-17 22:13:17.254 DEBUG 2700 — [ main] o.s.b.w.s.f.OrderedRequestContextFilter : Filter 'requestContextFilter' configured for use
2020-03-17 22:13:17.255 DEBUG 2700 — [ main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2020-03-17 22:13:17.255 DEBUG 2700 — [ main] o.s.b.w.s.f.OrderedFormContentFilter : Filter 'formContentFilter' configured for use
三、查看应用中注册了哪些filter
#application.yml debug: true
如上图,启用debug模式,查看日志可得:
标蓝部分详细内容如下:
2020-03-17 22:13:17.244 DEBUG 2700 — [ main] o.s.b.w.s.ServletContextInitializerBeans :
Mapping filters:
filterRegistrationBean urls=[/*] order=3,
characterEncodingFilter urls=[/*] order=-2147483648,
formContentFilter urls=[/*] order=-9900,
requestContextFilter urls=[/*] order=-105,
filterSample1 urls=[/*] order=2147483647
可以看出,该应用注册了五个filter,执行顺序由order决定
看完了这篇文章,相信你对SpringBoot注册Filter的方法有哪些有了一定的了解,想了解更多相关知识,欢迎关注高防服务器网行业资讯频道,感谢各位的阅读!
[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。
[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[