SpringBoot 使用过滤器修改请求参数

  1. 实现Filter接口
  2. @WebFilter 指定过滤器起作用的条件
  3. 继承HttpServletRequestWrapper,修改参数
  4. 主启动类加@ServletComponentScan
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig)  {
        System.out.println(("MyFilter过滤器初始化"));
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //获取HttpServletRequest对象
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String ipAddr = httpServletRequest.getRemoteAddr();

        Map paramMap = new HashMap<>();
        paramMap.put("ipAddr", ipAddr);
        paramMap.put("name", "ls");
        MyRequestWrapper myRequestWrapper = new MyRequestWrapper(httpServletRequest, paramMap);
        chain.doFilter(myRequestWrapper, response);
    }

    @Override
    public void destroy() {
        System.out.println(("MyFilter过滤器被销毁"));
    }

}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class MyRequestWrapper extends HttpServletRequestWrapper {

    private Map params = new HashMap<>();

    public MyRequestWrapper(HttpServletRequest request, Map newParams) {
        super(request);
        if(request.getParameterMap() != null){
            this.params.putAll(request.getParameterMap());
        }
        if(newParams != null){
            this.params.putAll(newParams);
        }
    }

    //主要覆盖这个方法来获取新的参数对象
    @Override
    public Map getParameterMap() {
        return params;
    }

    @Override
    public String[] getParameterValues(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            return (String[]) v;
        } else if (v instanceof String) {
            return new String[]{(String) v};
        } else {
            return new String[]{v.toString()};
        }
    }

    /**
     * 根据参数的key获取参数
     * @param name
     * @return
     */
    @Override
    public String getParameter(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            String[] strArr = (String[]) v;
            if (strArr.length > 0) {
                return strArr[0];
            } else {
                return null;
            }
        } else if (v instanceof String) {
            return (String) v;
        } else {
            return v.toString();
        }
    }

    public Enumeration getParameterNames() {
        Vector l = new Vector(params.keySet());
        return l.elements();
    }
}

你可能感兴趣的:(spring,boot)