业务背景
项目中有一个jar包里面有别人封装好的一个Filter用来校验token , 但是新功能不需要校验token, 但是又没有办法跳过 , 因此需要我在这个jar包Filter执行之前 , 在request中放入一个固定的token用来通过校验 。
最开始使用 @Order(0) 注解 试图将我写的Filter在jar包中的Filter之前执行 , 但是经过实验@Order(0) 并没有作用。需要在FilterRegistrationBean的时候设置order排序 。
package cn.com.baidu.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfig{
@Bean
public FilterRegistrationBean Filter0(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(new PageFilter());//设置过滤器名称
filterRegistrationBean.addUrlPatterns("/app/page/*");//配置过滤规则
filterRegistrationBean.setOrder(0); //order的数值越小 则优先级越高
return filterRegistrationBean;
}
}
package cn.com.baidu.filter;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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 javax.servlet.http.HttpServletRequest;
@WebFilter(urlPatterns = "/app/page/*", filterName = "pageFilter")
@Component
public class PageFilter implements Filter {
public PageFilter() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
// 因为request无法直接设置heafer , 因此需要对HttpServletRequest进行再封装
MutableHttpServletRequest mutableRequest = new MutableHttpServletRequest(req);
if(req.getHeader("token") == null) {
// 如果请求头里面没有token , 就放入一个固定的token , 用来跳过jar包中的校验
mutableRequest.putHeader("token", "固定的token");
}
chain.doFilter(mutableRequest, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("----pageFilter过滤器初始化----");
}
public void destroy() {
System.out.println("----pageFilter过滤器销毁----");
}
}
package cn.com.baidu.filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;
class MutableHttpServletRequest extends HttpServletRequestWrapper {
private final Map<String, String> customHeaders;
public MutableHttpServletRequest(HttpServletRequest request){
super(request);
this.customHeaders = new HashMap<String, String>();
}
public void putHeader(String name, String value){
this.customHeaders.put(name, value);
}
public String getHeader(String name) {
String headerValue = customHeaders.get(name);
if (headerValue != null){
return headerValue;
}
return ((HttpServletRequest) getRequest()).getHeader(name);
}
public Enumeration<String> getHeaderNames() {
Set<String> set = new HashSet<String>(customHeaders.keySet());
Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames();
while (e.hasMoreElements()) {
String n = e.nextElement();
set.add(n);
}
return Collections.enumeration(set);
}
}