Java过滤器Filter详解

时间:2021-08-04 18:07:42

Filter学习

Filter功能

  • 拦截jsp、静态图片文件、静态html资源文件
  • 实现URL级别的权限访问控制
  • 过滤敏感词汇
  • 压缩相应信息

Filter的工作原理

看源码可以看出来,Filter接口中有3个方法:init、doFilter、destory

Java过滤器Filter详解

init方法,初始化Filter,Filter在服务器启动阶段被实例化。并且调用init方法,init方法只执行一次,因为Filter只被实例化一次。

destory方法也好理解,就是销毁Filter,释放内存,在运行结束时调用此方法当。长时间没有用户访问,或者项目重新部署,或者服务器重新启动的时候,会销毁所有的Filter对象,销毁之前会先调用Filter对象的destroy方法,完成销毁前的准备工作。也是只执行一次。

doFilter方法提供给开发者重写过滤逻辑。

多个Filter的实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class AFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("输出filter2的init方法");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        System.out.println("输出filter2的doFilter方法之前");
        filterChain.doFilter(request, response);
        System.out.println("输出filter2的doFilter方法之后");
    }
    @Override
    public void destroy() {
        System.out.println("输出filter2的destroy方法");
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class BFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("输出filter1的init方法");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        System.out.println("输出filter1的doFilter方法之前");
        filterChain.doFilter(request, response);
        System.out.println("输出filter1的doFilter方法之后");
    }
    @Override
    public void destroy() {
        System.out.println("输出filter1的destroy方法");
    }
}

配置Filter的过滤顺序,通过setOrder方法执行FilterChain的顺序,数字越小,优先级越高。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Configuration
public class WebConfig {
    @Bean
    public FilterRegistrationBean filter1() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        AFilter aFilter = new AFilter();
        filterRegistrationBean.setFilter(aFilter);
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean filter2() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        BFilter bFilter = new BFilter();
        filterRegistrationBean.setFilter(bFilter);
        filterRegistrationBean.setOrder(2);
        return filterRegistrationBean;
    }
}

Filter的执行逻辑

Java过滤器Filter详解

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/u011133007/article/details/119880215