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() {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    FilterSample2 myFilter = new FilterSample2();
    registrationBean.setFilter(myFilter);
    ArrayList 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模式,查看日志可得:

SpringBoot注册Filter的两种实现方式_第1张图片

标蓝部分详细内容如下:

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的两种实现方式的文章就介绍到这了,更多相关SpringBoot注册Filter内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(SpringBoot注册Filter的两种实现方式)