fanxw 2 vuotta sitten
vanhempi
commit
316e2a595b

+ 10 - 11
src/main/java/com/chinaitop/depot/storage/controller/VentilationOperationController.java

@@ -1,24 +1,23 @@
1 1
 package com.chinaitop.depot.storage.controller;
2 2
 
3
+import java.util.List;
4
+
5
+import org.apache.commons.lang.StringUtils;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.http.MediaType;
8
+import org.springframework.web.bind.annotation.RequestMapping;
9
+import org.springframework.web.bind.annotation.RequestMethod;
10
+import org.springframework.web.bind.annotation.RestController;
11
+
3 12
 import com.chinaitop.depot.storage.model.TVentilationOperation;
4 13
 import com.chinaitop.depot.storage.service.VentilationOperationService;
5 14
 import com.chinaitop.depot.unissoft.model.ResponseEntity;
6
-import com.github.pagehelper.PageHelper;
7 15
 import com.github.pagehelper.PageInfo;
16
+
8 17
 import io.swagger.annotations.Api;
9 18
 import io.swagger.annotations.ApiImplicitParam;
10 19
 import io.swagger.annotations.ApiImplicitParams;
11 20
 import io.swagger.annotations.ApiOperation;
12
-import org.apache.commons.lang.StringUtils;
13
-import org.springframework.beans.factory.annotation.Autowired;
14
-import org.springframework.http.MediaType;
15
-import org.springframework.web.bind.annotation.RequestMapping;
16
-import org.springframework.web.bind.annotation.RequestMethod;
17
-import org.springframework.web.bind.annotation.RestController;
18
-
19
-import java.util.HashMap;
20
-import java.util.List;
21
-import java.util.Map;
22 21
 
23 22
 @RestController
24 23
 @RequestMapping(value = "/intelligents/VentilationOperationController")

+ 20 - 3
src/main/java/com/chinaitop/depot/storage/utils/CustomFilter.java

@@ -1,6 +1,10 @@
1 1
 package com.chinaitop.depot.storage.utils;
2 2
 
3 3
 import org.apache.commons.lang.ObjectUtils;
4
+import org.apache.commons.lang3.StringUtils;
5
+
6
+import com.chinaitop.depot.storage.utils.DataPolicyEngine;
7
+import com.chinaitop.depot.storage.utils.ParameterRequestWrapper;
4 8
 
5 9
 import javax.servlet.*;
6 10
 import javax.servlet.http.HttpServletRequest;
@@ -17,16 +21,29 @@ public class CustomFilter implements Filter {
17 21
 	@Override
18 22
 	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
19 23
 			throws IOException, ServletException {
24
+		ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper((HttpServletRequest) arg0);
20 25
 		HttpServletRequest request = (HttpServletRequest) arg0;
21 26
 		HttpSession session = request.getSession();
22 27
 		String orgId = "";
23 28
 		if (session != null){
24
-			orgId = ObjectUtils.toString(session.getAttribute("orgId"),"");
29
+			orgId = ObjectUtils.toString(session.getAttribute("orgId"), "");
25 30
 		}
26
-		if (!orgId.isEmpty()) {
31
+		if (StringUtils.isNotBlank(orgId)) {
27 32
 			DataPolicyEngine.set(orgId);
28 33
 		}
29
-		arg2.doFilter(arg0, arg1);
34
+
35
+		if (StringUtils.isNotBlank(orgId)) {
36
+			// 将参数追加到request url 之后
37
+			requestWrapper.addParameter("orgId", orgId);
38
+			requestWrapper.addParameter("unitid", orgId);
39
+			requestWrapper.addParameter("unitId", orgId);
40
+			// 将参数追加到request 的body 中
41
+			requestWrapper.addParameterToBody("orgId", orgId);
42
+			requestWrapper.addParameterToBody("unitid", orgId);
43
+			requestWrapper.addParameterToBody("unitId", orgId);
44
+		}
45
+
46
+		arg2.doFilter(requestWrapper, arg1);
30 47
 		DataPolicyEngine.remove();
31 48
 	}
32 49
 	

+ 233 - 0
src/main/java/com/chinaitop/depot/storage/utils/ParameterRequestWrapper.java

@@ -0,0 +1,233 @@
1
+package com.chinaitop.depot.storage.utils;
2
+
3
+import com.alibaba.fastjson.JSONObject;
4
+import org.springframework.util.StringUtils;
5
+
6
+import javax.servlet.ReadListener;
7
+import javax.servlet.ServletInputStream;
8
+import javax.servlet.http.HttpServletRequest;
9
+import javax.servlet.http.HttpServletRequestWrapper;
10
+import java.io.BufferedReader;
11
+import java.io.ByteArrayInputStream;
12
+import java.io.IOException;
13
+import java.io.InputStreamReader;
14
+import java.util.HashMap;
15
+import java.util.Map;
16
+
17
+/**
18
+ * request 请求参数添加
19
+ *
20
+ * @Description TODO
21
+ * @Date 2022/12/13 09:41
22
+ * @Author fxw
23
+ * @Version 1.0
24
+ */
25
+public class ParameterRequestWrapper extends HttpServletRequestWrapper {
26
+
27
+    private Map<String, String[]> params = new HashMap<String, String[]>();
28
+    private byte[] body;
29
+
30
+    public ParameterRequestWrapper(HttpServletRequest request) {
31
+        // 将request交给父类,以便于调用对应方法的时候,将其输出,
32
+        // 其实父亲类的实现方式和第一种new的方式类似
33
+        super(request);
34
+        //将参数表,赋予给当前的Map以便于持有request中的参数
35
+        //由于request并没有提供现成的获取json字符串的方法,所以我们需要将body中的流转为字符串
36
+        String json = getPostData(request);
37
+        if (!StringUtils.isEmpty(json)) {
38
+            // body 赋值
39
+            this.body = getData(json).getBytes();
40
+        }
41
+        // 请求参数赋值
42
+        this.params.putAll(request.getParameterMap());
43
+    }
44
+
45
+    /**
46
+     * 重载一个构造方法-- 扩展参数
47
+     *
48
+     * @param request
49
+     * @param extendParams
50
+     */
51
+    public ParameterRequestWrapper(HttpServletRequest request, Map<String, Object> extendParams) {
52
+        this(request);
53
+        //这里将扩展参数写入参数表
54
+        addAllParameters(extendParams);
55
+    }
56
+
57
+    /**
58
+     * 增加多个参数
59
+     *
60
+     * @param otherParams
61
+     */
62
+    public void addAllParameters(Map<String, Object> otherParams) {
63
+        for (Map.Entry<String, Object> entry : otherParams.entrySet()) {
64
+            addParameter(entry.getKey(), entry.getValue());
65
+        }
66
+    }
67
+
68
+    /**
69
+     * 增加参数
70
+     */
71
+    public void addParameter(String name, Object value) {
72
+        if (value != null) {
73
+            if (value instanceof String[]) {
74
+                params.put(name, (String[]) value);
75
+            } else if (value instanceof String) {
76
+                params.put(name, new String[]{(String) value});
77
+            } else {
78
+                params.put(name, new String[]{String.valueOf(value)});
79
+            }
80
+        }
81
+    }
82
+    
83
+    /**
84
+     * 删除参数
85
+     */
86
+    public void deleteParameter(String key) {
87
+    	params.remove(key);
88
+    }
89
+
90
+    /**
91
+     * 增加body 参数
92
+     *
93
+     * @param name
94
+     * @param value
95
+     */
96
+    public void addParameterToBody(String name, Object value) {
97
+        byte[] json = this.body;
98
+        if (null == json) {
99
+            return;
100
+        }
101
+        String jsonStr = new String(json);
102
+        try {
103
+            Map<String, Object> mapData = JSONObject.parseObject(jsonStr, Map.class);
104
+            if (value != null) {
105
+                mapData.put(name, value);
106
+                this.body = JSONObject.toJSONString(mapData).getBytes();
107
+            }
108
+        } catch (Exception ex) {
109
+            // 转换异常
110
+        }
111
+
112
+    }
113
+
114
+    /**
115
+     * 删除body 参数
116
+     *
117
+     * @param name
118
+     * @param value
119
+     */
120
+    public void deleteParameterToBody(String key) {
121
+        byte[] json = this.body;
122
+        if (null == json) {
123
+            return;
124
+        }
125
+        String jsonStr = new String(json);
126
+        try {
127
+            Map<String, Object> mapData = JSONObject.parseObject(jsonStr, Map.class);
128
+            mapData.remove(key);
129
+            this.body = JSONObject.toJSONString(mapData).getBytes();
130
+        } catch (Exception ex) {
131
+            // 转换异常
132
+        }
133
+
134
+    }
135
+
136
+
137
+    /**
138
+     * body中参数解密
139
+     *
140
+     * @param json
141
+     * @return
142
+     */
143
+    private String getData(String json) {
144
+        //加密,如果传过来的是加密数据,先解密,未加密直接返回原json
145
+//        if(StringUtils.isNotEmpty(json)){
146
+//            json = AES256Util.decode(json);
147
+//            if(StringUtils.isEmpty(json)){
148
+//                return "";
149
+//            }
150
+//            JSONObject object = JSONUtil.parseObj(json);
151
+//            return JSONUtil.toJsonStr(object);
152
+//        }
153
+        //不加密
154
+        return json;
155
+
156
+    }
157
+
158
+    /**
159
+     * 获取body 参数
160
+     *
161
+     * @param request
162
+     * @return
163
+     */
164
+    public static String getPostData(HttpServletRequest request) {
165
+        StringBuilder data = new StringBuilder();
166
+        String line;
167
+        BufferedReader reader;
168
+        try {
169
+            reader = request.getReader();
170
+            while (null != (line = reader.readLine())) {
171
+                data.append(line);
172
+            }
173
+        } catch (IOException e) {
174
+            return null;
175
+        }
176
+        return data.toString();
177
+    }
178
+
179
+    @Override
180
+    public String getParameter(String name) {//重写getParameter,代表参数从当前类中的map获取
181
+        String[] values = params.get(name);
182
+        if (values == null || values.length == 0) {
183
+            return null;
184
+        }
185
+        return values[0];
186
+    }
187
+
188
+    @Override
189
+    public String[] getParameterValues(String name) {//同上
190
+        return params.get(name);
191
+    }
192
+
193
+    @Override
194
+    public BufferedReader getReader() throws IOException {
195
+        return new BufferedReader(new InputStreamReader(getInputStream()));
196
+    }
197
+
198
+    /**
199
+     * 在使用@RequestBody注解的时候,其实框架是调用了getInputStream()方法,所以我们要重写这个方法
200
+     *
201
+     * @return
202
+     * @throws IOException
203
+     */
204
+    @Override
205
+    public ServletInputStream getInputStream() throws IOException {
206
+        if (body == null) {
207
+            body = new byte[0];
208
+        }
209
+        final ByteArrayInputStream bais = new ByteArrayInputStream(body);
210
+        return new ServletInputStream() {
211
+            @Override
212
+            public boolean isFinished() {
213
+                return false;
214
+            }
215
+
216
+            @Override
217
+            public boolean isReady() {
218
+                return false;
219
+            }
220
+
221
+            @Override
222
+            public void setReadListener(ReadListener readListener) {
223
+
224
+            }
225
+
226
+            @Override
227
+            public int read() throws IOException {
228
+                return bais.read();
229
+            }
230
+        };
231
+    }
232
+
233
+}