在springboot添加过滤器有两种方式:
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
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= {"/*"})
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 } }
注:经测试,@Order并不能实现过滤器排序功能,在springboot中注解的方式增加过滤器可通过修改过滤器文件名称的方式排序,如上的过滤器TestFilter1在TestFilter2前。