• java过滤器(Filter)


    原文链接:java过滤器(Filter – 编程屋

    目录

    1 过滤器简介

    2 Filter详细介绍

    3 Filter的用法

    3.1 用法1

     3.2 用法2


    1 过滤器简介

    filter也称之为过滤器,它是javaWeb三大组件之一(Servlet程序、Listener监听器、Filter过滤器)

    作用:既可以对请求进行拦截,也可以对响应进行处理。

    常见场景:权限检查,日记操作、拦截请求、过滤操作、对请求字符设置编码。

    2 Filter详细介绍

    要想介绍filter,就必须介绍Filter中的三个方法。

    1. /**
    2. * web应用启动时,web服务器将创建Filter的实例对象,并调用init方法,读取web.xml的配置,完成对象的初始化功能,
    3. * 从而为后续的用户请求做好拦截的准备工作(filter对象只会创建一次,init方法也只会执行一次,开发人员通过init的参数,
    4. * 可或得代表当前filter配置信息的FilterConfig对象)
    5. * @param filterConfig
    6. * @throws ServletException
    7. */
    8. @Override
    9. public void init(FilterConfig filterConfig) throws ServletException {
    10. }
    11. /**
    12. * 这个方法完成实际的过滤操作,当客户请求访问与过滤器相关联的URL的时候,Servlet过滤器将先执行doFilter方法,FilterChain参数用于访问后续过滤器
    13. * @param servletRequest
    14. * @param servletResponse
    15. * @param filterChain
    16. * @throws IOException
    17. * @throws ServletException
    18. */
    19. @Override
    20. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    21. }
    22. /**
    23. * filter创建后会保存在内存中,当web应用移除或者服务器停止时才销毁,该方法在Filter的生命周期中仅执行一次,在这个方法中,可以释放过滤器使用的资源
    24. */
    25. @Override
    26. public void destroy() {
    27. }

    3 Filter的用法

    3.1 用法1

    1)自定义一个过滤器实现Filter接口、配置@WebFilter注解,配置拦截路径(也可通过web.xml配置)

    1. @WebFilter(urlPatterns = "/*")
    2. public class MyFilterOne implements Filter {
    3. /**
    4. * web应用启动时,web服务器将创建Filter的实例对象,并调用init方法,读取web.xml的配置,完成对象的初始化功能,
    5. * 从而为后续的用户请求做好拦截的准备工作(filter对象只会创建一次,init方法也只会执行一次,开发人员通过init的参数,
    6. * 可或得代表当前filter配置信息的FilterConfig对象)
    7. * @param filterConfig
    8. * @throws ServletException
    9. */
    10. @Override
    11. public void init(FilterConfig filterConfig) throws ServletException {
    12. }
    13. /**
    14. * 这个方法完成实际的过滤操作,当客户请求访问与过滤器相关联的URL的时候,Servlet过滤器将先执行doFilter方法,FilterChain参数用于访问后续过滤器
    15. * @param servletRequest
    16. * @param servletResponse
    17. * @param filterChain
    18. * @throws IOException
    19. * @throws ServletException
    20. */
    21. @Override
    22. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    23. System.out.println("我是过滤器,我进来了");
    24. }
    25. /**
    26. * filter创建后会保存在内存中,当web应用移除或者服务器停止时才销毁,该方法在Filter的生命周期中仅执行一次,在这个方法中,可以释放过滤器使用的资源
    27. */
    28. @Override
    29. public void destroy() {
    30. }
    31. }

    2)在启动类上加上@ServletComponentScan注解

    1. @SpringBootApplication
    2. @ServletComponentScan
    3. public class SpringbootInterceptorApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(SpringbootInterceptorApplication.class, args);
    6. }
    7. }

    3)controller:

    1. @RestController
    2. public class LoginController {
    3. @GetMapping("/test/filter")
    4. public String testFilter(){
    5. return "该请求被拦截了,但是在过滤器中已经放行了";
    6. }
    7. }

    直接浏览器访问:

     控制台输出:发现该请求已经被过滤去拦截

     3.2 用法2

    1)自定义一个过滤器实现Filter接口、配置拦截路径(也可通过web.xml配置)

    1. public class MyFilterOne implements Filter {
    2. /**
    3. * web应用启动时,web服务器将创建Filter的实例对象,并调用init方法,读取web.xml的配置,完成对象的初始化功能,
    4. * 从而为后续的用户请求做好拦截的准备工作(filter对象只会创建一次,init方法也只会执行一次,开发人员通过init的参数,
    5. * 可或得代表当前filter配置信息的FilterConfig对象)
    6. * @param filterConfig
    7. * @throws ServletException
    8. */
    9. @Override
    10. public void init(FilterConfig filterConfig) throws ServletException {
    11. }
    12. /**
    13. * 这个方法完成实际的过滤操作,当客户请求访问与过滤器相关联的URL的时候,Servlet过滤器将先执行doFilter方法,FilterChain参数用于访问后续过滤器
    14. * @param servletRequest
    15. * @param servletResponse
    16. * @param filterChain
    17. * @throws IOException
    18. * @throws ServletException
    19. */
    20. @Override
    21. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    22. System.out.println("我是过滤器,我进来了");
    23. filterChain.doFilter(servletRequest, servletResponse);
    24. }
    25. /**
    26. * filter创建后会保存在内存中,当web应用移除或者服务器停止时才销毁,该方法在Filter的生命周期中仅执行一次,在这个方法中,可以释放过滤器使用的资源
    27. */
    28. @Override
    29. public void destroy() {
    30. }
    31. }

    2)在启动类上注册

    1. @SpringBootApplication
    2. public class SpringbootInterceptorApplication {
    3. public static void main(String[] args) {
    4. SpringApplication.run(SpringbootInterceptorApplication.class, args);
    5. }
    6. /**
    7. * 注册Filter
    8. */
    9. @Bean
    10. public FilterRegistrationBean getFilterRegistrationBean(){
    11. FilterRegistrationBean bean = new FilterRegistrationBean(new MyFilterOne());
    12. //bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
    13. bean.addUrlPatterns("/*");
    14. return bean;
    15. }
    16. }

    测试结果与3.1相同。

    总结:

    要想调用配置一个过滤器:

    1)启动类上增加注解@ServletComponentScan

    2)新建一个类使其实现Filter接口,并实现里面的三个方法

    3)在新建类上加上@WebFilter()注解,配置需要拦截的规则

    4)在doFilter方法中控制filterChain.doFilter(servletRequest, servletResponse)调用

    同样,如果前后端交互有遇到跨域问题的,也可以通过Filter解决(亲测有效),如:

    以上只是部分内容,为了维护方便,本文已迁移到新地址:java过滤器(Filter – 编程屋

  • 相关阅读:
    树莓派底层开发-----交叉编译
    redis缓存恢复-2022新项目
    史上最强C语言教程----万字初识C语言
    2. Executor与SqlSession详解
    Apache Cassandra:分布式NoSQL数据库
    C练习百题之求阶层
    一、XSS加解密编码解码工具
    Cloud Native=Cloud+Native 理解云原生
    Java项目源码基于javaweb的学籍管理系统
    成都瀚网科技有限公司抖音带货正规么
  • 原文地址:https://blog.csdn.net/qq_50652600/article/details/127308348