有2种方式可以实现过滤器
1:通过FilterRegistrationBean实例注册
2:通过@WebFilter注解生效
这里选择第一种,因为第二种不能设置过滤器之间的优先级
为了演示优先级,这里创建2个测试过滤器类:Test1Filter、Test2Filter
通过实现javax.servlet.Filter接口,覆盖其doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)方法,决定拦截或放行
![](https://i-blog.csdnimg.cn/blog_migrate/9a2dd39d7f0c7d5a8f949e84aa085729.gif)
public class Test1Filter implements Filter { @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request=(HttpServletRequest)arg0; System.out.println("自定义过滤器filter1触发,拦截url:"+request.getRequestURI()); arg2.doFilter(arg0,arg1); } }
![](https://i-blog.csdnimg.cn/blog_migrate/fb332ef9e5ae9a4e8566bc8636d2583f.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/191dbc669834e101759a77c6e9eae341.gif)
public class Test2Filter implements Filter { @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request=(HttpServletRequest)arg0; System.out.println("自定义过滤器filter2触发,拦截url:"+request.getRequestURI()); arg2.doFilter(arg0,arg1); } }
![](https://i-blog.csdnimg.cn/blog_migrate/5747a80447e9f272a7c00ff53684e0e1.gif)
通过在springboot的configuration中配置不同的FilterRegistrationBean实例,来注册自定义过滤器
这里创建一个configuration类
![](https://i-blog.csdnimg.cn/blog_migrate/48d354cae2b3b43a006447a3e85fc115.gif)
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.example.demo.filter.Test1Filter; import com.example.demo.filter.Test2Filter; @Configuration public class DemoConfiguration { @Bean public FilterRegistrationBean<Test1Filter> RegistTest1(){ //通过FilterRegistrationBean实例设置优先级可以生效 //通过@WebFilter无效 FilterRegistrationBean<Test1Filter> bean = new FilterRegistrationBean<Test1Filter>(); bean.setFilter(new Test1Filter());//注册自定义过滤器 bean.setName("flilter1");//过滤器名称 bean.addUrlPatterns("/*");//过滤所有路径 bean.setOrder(1);//优先级,最顶级 return bean; } @Bean public FilterRegistrationBean<Test2Filter> RegistTest2(){ //通过FilterRegistrationBean实例设置优先级可以生效 //通过@WebFilter无效 FilterRegistrationBean<Test2Filter> bean = new FilterRegistrationBean<Test2Filter>(); bean.setFilter(new Test2Filter());//注册自定义过滤器 bean.setName("flilter2");//过滤器名称 bean.addUrlPatterns("/test/*");//过滤所有路径 bean.setOrder(6);//优先级,越低越优先 return bean; } }
![](https://i-blog.csdnimg.cn/blog_migrate/2cec5ae02b65de2798905974f771a063.gif)
其中
1第一个bean拦截所有路径,而第二个只拦截/test/*路径
2第一个bean优先级设置了1,而第二个设置了6,越低越优先,所以过滤器1应该在过滤器2前面拦截
运行springboot,访问/test/*请求如下图,再访问/v请求如下图只拦截了过滤器1,而过滤器2路径不匹配
![](https://i-blog.csdnimg.cn/blog_migrate/be270c77708e1f3b60e647f4d1865d12.png)
如果把.addUrlPatterns();中拦截路径改成一样/test/*,则所有请求,都会触发2个过滤器,而且过滤器1永远在最先拦截
![](https://i-blog.csdnimg.cn/blog_migrate/8b0429cbf960425eb6b3ca1b006c5e08.png)
、概念
ShallowEtagHeaderFilter会根据响应的内容生成”shallow etag”,再次请求时,会将该值作为请求头中If-None-Match属性值,并与”shallow etag”的值进行比较,若二者相等返回304, Not Modified。相当于给请求添加缓存的功能。
2、使用
使用shallowEtagHeaderFilter的步骤如下
- 第一步,创建ShallowEtagHeaderFilter对象,
- 第二步,注册ShallowEtagHeaderFilter,拦截DispactherServlet,或者是URL地址都可以。
- 第三步,验证,可以在响应中看到eTag字段。
3、示例
1、第一步:配置ShallowEtagHeaderFilter拦截器拦截DispatcherSerlvet
1 2 3 4 5 6 7 8 9 10 |
|
2、第二步,验证,第一次请求时请求头中无If-None-Match属性,响应码为200。再次请求时,请求头中有If-None-Match属性值,浏览器中的响应码为304
1 2 3 4 |
|
package com.siyuan.ads.cn.papi;
import com.ebay.ecg.it.common.configuration.ExceptionHandlingAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
@SpringBootApplication(exclude = ExceptionHandlingAutoConfiguration.class)
@EnableCircuitBreaker
@EnableDiscoveryClient
public class PapiIt {
@Bean
public FilterRegistrationBean someFilterRegistration(ShallowEtagHeaderFilter shallowEtagHeaderFilter) {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(shallowEtagHeaderFilter);
registration.addUrlPatterns("/*");
return registration;
}
@Bean
public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
return new ShallowEtagHeaderFilter();
}
public static void main(String[] args) {
SpringApplication.run(PapiIt.class, args);
}
}