• 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过滤器被销毁"));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
  • 相关阅读:
    土巴兔面试题
    飞行动力学 - 第17节-part3-垂尾和推进系统对航向的影响 之 基础点摘要
    insufficient permissions for device
    Lab_1:练习1——理解通过make生成执行文件的过程
    Uni-App 快捷登录
    CLIP微调方式
    安全基础 --- 过滤情况下实现弹窗展现
    Java多线程(三)
    binding 里面local的用法
    Python基础知识从hello world 开始(第二天)
  • 原文地址:https://blog.csdn.net/weixin_51681634/article/details/126348433