在springboot添加filter有两种方式:

  (1)、通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加cookie可能会存在写不进前端情况)

  (2)、通过注解@WebFilter的方式

一、通过创建FilterRegistrationBean的方式创建多个filter以及设置执行顺序:

   1、创建两个实现Filter接口的类TestFilter1 、TestFilter2

package com.aoxun.core.filter;

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; public class TestFilter1 implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException {} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
} @Override
public void destroy() {} }
package com.aoxun.core.filter;

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; public class TestFilter2 implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException {} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
} @Override
public void destroy() {} }

2、创建配置类

      有多个filter就创建多个FilterRegistrationBean ,若需注明filter的执行顺序,可通过registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE - 1)配置,值越大,执行顺序越靠后

package com.aoxun.config.web;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.aoxun.core.filter.TestFilter1;
import com.aoxun.core.filter.TestFilter2; /**
* filter配置
* @author zcj
*
*/
@Configuration
public class FilterConfig2 { @Bean
public FilterRegistrationBean companyUrlFilterRegister() {
FilterRegistrationBean registration = new FilterRegistrationBean();
//注入过滤器
registration.setFilter(new TestFilter1());
//拦截规则
registration.addUrlPatterns("/*");
//过滤器名称
registration.setName("testFilter1");
//过滤器顺序
registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
return registration;
} @Bean
public FilterRegistrationBean outLinkSecurityFilterRegister() {
FilterRegistrationBean registration = new FilterRegistrationBean();
//注入过滤器
registration.setFilter(new TestFilter2());
//拦截规则
registration.addUrlPatterns("/*");
//过滤器名称
registration.setName("testFilter2");
//过滤器顺序
registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE - 1);
return registration;
} }

二、通过注解@WebFilter的方式

   1、在启动类上增加@ServletComponentScan注解,自动扫描带有过滤器注解的包

   2、在类上使用@WebFilter和@Order组合实现

package com.aoxun.core.filter;

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 javax.servlet.annotation.WebFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.core.annotation.Order; @WebFilter(filterName="testFilter1",urlPatterns= {"/*"})
@Order(FilterRegistrationBean.LOWEST_PRECEDENCE)
public class TestFilter1 implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub } @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException { chain.doFilter(request, response); } @Override
public void destroy() {
// TODO Auto-generated method stub } }

(转自:https://www.cnblogs.com/begin2016/p/8947887.html

05-11 08:34