• 重写 HttpServletRequestWrapper


    重写http 请求信息

    
    import com.gome.boot.common.util.JacksonUtils;
    import org.apache.commons.io.IOUtils;
    import org.apache.commons.lang3.ArrayUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.util.StreamUtils;
    
    import javax.servlet.ReadListener;
    import javax.servlet.ServletInputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import java.io.*;
    import java.net.URLDecoder;
    import java.nio.charset.StandardCharsets;
    import java.util.*;
    
    public class GomeHttpServletRequestWrapper extends HttpServletRequestWrapper {
    
        private Map<String, String[]> paramsMap = new HashMap<>();
    
        /**
         * 缓存下来的HTTP body
         */
        private final byte[] body;
    
        public GomeHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
            super(request);
            boolean wwwFormContentType = true;
            if (!"application/x-www-form-urlencoded".equals(request.getContentType())) {
                wwwFormContentType = false;
            }
            body = StreamUtils.copyToByteArray(request.getInputStream());
            // 首先从POST中获取数据
            //paramsMap.putAll(this.parseQueryString(wwwFormContentType,this.getQueryString()));
            this.parseQueryString(wwwFormContentType, IOUtils.toString(this.body, StandardCharsets.UTF_8.name()));
        }
    
        @Override
        public Map<String, String[]> getParameterMap() {
            return paramsMap;
        }
    
        @Override
        public String getParameter(String name) {// 重写getParameter,代表参数从当前类中的map获取
            String[] values = paramsMap.get(name);
            if (ArrayUtils.isEmpty(values)) {
                return null;
            }
            return values[0];
        }
    
        @Override
        public String[] getParameterValues(String name) {// 同上
            return paramsMap.get(name);
        }
    
        @Override
        public Enumeration<String> getParameterNames() {
            return Collections.enumeration(paramsMap.keySet());
        }
    
        private void parseQueryString(boolean wwwFormContentType, String queryString) throws UnsupportedEncodingException {
            if (StringUtils.isBlank(queryString)) {
                return;
            }
    
            queryString = URLDecoder.decode(queryString, StandardCharsets.UTF_8.name());
            if (!wwwFormContentType) {
                queryString = "body=" + queryString;
            }
            String[] splitArray = StringUtils.split(queryString, "\\&");
            for (String split : splitArray) {
                if (!split.contains("body=")) {
                    continue;
                }
    
                String[] kv = split.split("body=");
                if (kv.length < 2 || StringUtils.isBlank(kv[1])) {
                    continue;
                }
    
                String key = "body";
                String val = kv[1];
    
                Map map = JacksonUtils.convertJsonToObject(val, Map.class);
    
                Iterator it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    String mapKey = String.valueOf(entry.getKey());
                    String mapVal = String.valueOf(entry.getValue());
    
                    paramsMap.put(mapKey, new String[]{mapVal});
                }
                paramsMap.put(key, new String[]{val});
    
            }
        }
    
        @Override
        public BufferedReader getReader() throws IOException {
            return new BufferedReader(new InputStreamReader(getInputStream()));
        }
    
        @Override
        public ServletInputStream getInputStream() throws IOException {
            final ByteArrayInputStream bais = new ByteArrayInputStream(body);
            return new ServletInputStream() {
                @Override
                public int read() throws IOException {
                    return bais.read();
                }
    
                @Override
                public boolean isFinished() {
                    return false;
                }
    
                @Override
                public boolean isReady() {
                    return false;
                }
    
                @Override
                public void setReadListener(ReadListener arg0) {
                }
            };
        }
    
        /*@Override
        public String getContentType() {
            return "application/x-www-form-urlencoded";
        }*/
    }
    
    • 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
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    
    import org.springframework.web.servlet.DispatcherServlet;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class GomeDispatcherServlet extends DispatcherServlet {
        /**
         * 包装成我们自定义的request
         * @param request
         * @param response
         * @throws Exception
         */
        @Override
        protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
            super.doDispatch(new GomeHttpServletRequestWrapper(request), response);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.DispatcherServlet;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class IntercepterConfig implements WebMvcConfigurer {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            // 这个必须在第一位
            registry.addInterceptor(new RequestIdInterceptor()).addPathPatterns("/**");
            WebMvcConfigurer.super.addInterceptors(registry);
        }
    
        @Bean
        @Qualifier(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
        public DispatcherServlet dispatcherServlet() {
            return new GomeDispatcherServlet();
        }
    }
    
    
    
    • 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
  • 相关阅读:
    【AI应用】NVIDIA Tesla V100S-PCIE-32GB的详情参数
    【iMessage苹果相册推位置推】“MFI授权计划”是指一个零丁的苹果程序开发
    状态估计|基于 MMSE 的分析估计器的不确定电力系统分析(Matlab代码实现)
    如何写一个合格的API文档
    【数据库】时区及JDBC的时区设置
    三、mysqld程序的运行原理及数据库结构
    软件设计模式详解 UML类图 六大设计原则
    【Tomcat】Tomcat 介绍及使用教程
    联邦学习中的非独立同分布Non-IID
    星环科技数据安全管理平台 Defensor重磅发布
  • 原文地址:https://blog.csdn.net/qq_34789780/article/details/126571571