欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot注册Filter的两种实现方式

程序员文章站 2022-09-25 08:13:13
springboot版本:2.2.5一、filter注册springboot中添加filter有两种方式:1、实现方法一package com.example.demo.filter1;import...

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

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

SpringBoot注册Filter的两种实现方式

标蓝部分详细内容如下:

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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!