fanxw 2 年之前
父节点
当前提交
21fa8032d5

+ 5 - 0
pom.xml

@@ -127,6 +127,11 @@
127 127
             <artifactId>gson</artifactId>
128 128
             <version>2.8.2</version>
129 129
         </dependency>
130
+        <dependency>
131
+			<groupId>com.alibaba</groupId>
132
+			<artifactId>fastjson</artifactId>
133
+			<version>1.2.37</version>
134
+		</dependency>
130 135
         
131 136
         <dependency>
132 137
 			<groupId>org.springframework.boot</groupId>

+ 61 - 0
src/main/java/com/chinaitop/depot/utils/CustomFilter.java

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

+ 61 - 0
src/main/java/com/chinaitop/depot/utils/DataPolicyEngine.java

@@ -0,0 +1,61 @@
1
+package com.chinaitop.depot.utils;
2
+
3
+import org.apache.commons.logging.Log;
4
+import org.apache.commons.logging.LogFactory;
5
+
6
+
7
+/**
8
+ * 
9
+ * @author hf
10
+ * 
11
+ * @description 数据策略(数据权限控制使用)
12
+ * 
13
+ * 
14
+ */
15
+public class DataPolicyEngine {
16
+	//log信息输出对象
17
+	protected static final Log log = LogFactory.getLog(DataPolicyEngine.class); 
18
+
19
+	//本地线程,用于存储线程公共对象
20
+	private static ThreadLocal threadLocalSession = new ThreadLocal();
21
+	
22
+	
23
+	/**
24
+	 * @description 获取本地执行线程
25
+	 *
26
+	 * @return ThreadLocal 
27
+	 * 
28
+	 */
29
+	public static ThreadLocal getThreadLocalSession() {
30
+		return threadLocalSession;
31
+	}
32
+	
33
+	/**
34
+	 * @description 在线程内设置存储对象
35
+	 * 
36
+	 */
37
+	public static void set(Object obj) {
38
+		threadLocalSession.set(obj);
39
+
40
+	}
41
+	
42
+	/**
43
+	 * @description 获取在线程内设置存储的对象
44
+	 * 
45
+	 */
46
+	public static Object get() {
47
+		return threadLocalSession.get();
48
+
49
+	}
50
+
51
+	/**
52
+	 * @description 移除在线程内设置存储的对象
53
+	 * 
54
+	 */
55
+	public static void remove() {
56
+		threadLocalSession.remove();
57
+
58
+	}
59
+	
60
+	
61
+}

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

@@ -0,0 +1,233 @@
1
+package com.chinaitop.depot.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
+}

+ 39 - 0
src/main/java/com/chinaitop/depot/utils/WebConfig.java

@@ -0,0 +1,39 @@
1
+package com.chinaitop.depot.utils;
2
+
3
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
4
+import org.springframework.context.annotation.Bean;
5
+import org.springframework.context.annotation.Configuration;
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+
9
+/**
10
+ * @desc 注册bean, 将自定义过滤器添加到过滤器链中
11
+ * @Author fxw
12
+ * @date 2019/3/11
13
+ * @return
14
+ */
15
+@Configuration
16
+public class WebConfig {
17
+
18
+   /**
19
+    * 注册过滤器,有两种方式:
20
+    * 1) 使用 @Component 注解<br>
21
+    * 2) 添加到过滤器链中,此方式适用于使用第三方的过滤器。将过滤器写到 WebConfig 类中,如下:
22
+    */
23
+   @Bean
24
+   public FilterRegistrationBean filterRegistrationBean() {
25
+
26
+      FilterRegistrationBean registrationBean = new FilterRegistrationBean();
27
+
28
+      CustomFilter filter = new CustomFilter();
29
+      registrationBean.setFilter(filter);
30
+
31
+      //设置过滤器拦截请求
32
+      List<String> urls = new ArrayList<>();
33
+      urls.add("/*");
34
+      registrationBean.setUrlPatterns(urls);
35
+
36
+      return registrationBean;
37
+   }
38
+
39
+}