Springboot笔记~filter-阿里云开发者社区

开发者社区> 开发与运维> 正文

Springboot笔记~filter

简介: 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> filterSample2() {
    FilterRegistrationBean<FilterSample2> registrationBean = new FilterRegistrationBean<FilterSample2>();
    FilterSample2 myFilter = new FilterSample2();
    registrationBean.setFilter(myFilter);
    ArrayList<String> 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
1584455635526-3df2a7d6eb192cac.png

如上图,启用debug模式,查看日志可得:

标蓝部分详细内容如下:

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决定

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章