springboot|springboot配置Filter过滤器

在阅读本文前,强烈建议大家先阅读前一篇
springboot|springboot配置拦截器

同样强烈建议先阅读官方文档:
https://docs.spring.io/spring/docs/5.2.1.RELEASE/spring-framework-reference/web.html#filters

https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/html/howto.html#howto-add-a-servlet-filter-or-listener

https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/html/spring-boot-features.html#boot-features-embedded-container

配置过滤器需要以下几步:

  1. 添加相关的依赖

  2. 配置过滤器Bean及相应的配置

  3. 编写相关的依赖

添加相关的依赖


同样,只需要引入web依赖就可以了

implementation 'org.springframework.boot:spring-boot-starter-web'

配置过滤器Bean及相应配置


添加过滤器的Bean代码如下

/**
 * 这里要注意两点,一个是 @WebFilter注解, 一个是实现了Filter接口
 * @Author https://www.javastudy.cloud
 * @CreateTime 2019/11/8
 **/
@WebFilter("/test/*")
public class TestFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 进行逻辑处理
        System.out.println("in doFilter");
        // 一定要记得这个,逻辑处理完之后,调用过滤器链,去进入到下一个过滤器
        chain.doFilter(request,response);
    }
}

在Main函数类上的配置如下:

/**
 * 只需要在这里面加上一个@ServletComponentScan这个注解就可以了
 */
@SpringBootApplication
@ServletComponentScan
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
  }

}

用于测试的Controller代码:

/**
 * @Author https://www.javastudy.cloud
 * @CreateTime 2019/11/8
 **/
@RestController
public class TestController {
    @RequestMapping("test/testFilter")
    public String testFilter(){
        System.out.println("in test method");
        return "";
    }
    @RequestMapping("pass/testPass")
    public String testPass(){
        System.out.println("in pass method");
        return "";
    }
}

输出结果


springboot|springboot配置Filter过滤器
springboot|springboot配置Filter过滤器

引用第三方的过滤器


有种情况是jar包里面提供了第三方的过滤器,我们没有办法加WebFilter注解,这时候就要用另外一种方式添加了,如这里有个ThirdFilter:

/**
 * 这是一个第三方的Filter,只实现了Filter,没有添加WebFilter注解
 * @Author https://www.javastudy.cloud
 * @CreateTime 2019/11/8
 **/
public class ThirdFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 进行逻辑处理
        System.out.println("in ThirdFilter");
        // 一定要记得这个,逻辑处理完之后,调用过滤器链,去进入到下一个过滤器
        chain.doFilter(request,response);
    }
}

需要在Main函数的类里面添另一个Bean,把这个过滤器包装成一个Bean


/**
 * 只需要在这里面加上一个@ServletComponentScan这个注解就可以了
 */
@SpringBootApplication
@ServletComponentScan
public class DemoApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

/**
   * 把第三方的过滤器包装成一个Spring的Bean
   * @return
   */
@Bean
public FilterRegistrationBean thirdFilter() {
    FilterRegistrationBean bean = new FilterRegistrationBean<>(new ThirdFilter());
    bean.setName("ThirdFilter");
    bean.addUrlPatterns("/test/*");
    bean.setOrder(0);
return bean;
  }
}

结果输出


springboot|springboot配置Filter过滤器

DEMO总评


过滤器是WEB中的一大重要组件,和拦载器不同的是,过滤器是Servlet的组件,而拦截器是spring的组件. 过滤器会对所有的请求有效,如js,css这些,都会进到过滤器里面,所以我们常用过滤器进行字符转码,或者监控请求时间,登录等等. 加油吧,少年!!

猜你喜欢

转载自blog.51cto.com/15082395/2645946