Skip to content

Commit ed537ff

Browse files
authored
Merge pull request #72 from yinjihuan/encrypt1.2.2
Encrypt1.2.2
2 parents f8cbd3d + 15ababe commit ed537ff

File tree

9 files changed

+204
-86
lines changed

9 files changed

+204
-86
lines changed

monkey-api-encrypt-core/pom.xml

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.cxytiandi</groupId>
77
<artifactId>monkey-api-encrypt-core</artifactId>
8-
<version>1.2.1.RELEASE</version>
8+
<version>1.2.2.RELEASE</version>
99
<packaging>jar</packaging>
1010

1111
<name>monkey-api-encrypt-core</name>
@@ -109,7 +109,13 @@
109109
<version>2.0.6.RELEASE</version>
110110
<scope>provided</scope>
111111
</dependency>
112-
</dependencies>
112+
<dependency>
113+
<groupId>org.springframework</groupId>
114+
<artifactId>spring-webmvc</artifactId>
115+
<version>5.3.4</version>
116+
<scope>provided</scope>
117+
</dependency>
118+
</dependencies>
113119

114120
<build>
115121
<plugins>

monkey-api-encrypt-core/src/main/java/com/cxytiandi/encrypt/core/EncryptionFilter.java

+65-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.cxytiandi.encrypt.core;
22

33
import java.io.IOException;
4-
import java.util.Enumeration;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
4+
import java.util.*;
85

96
import javax.servlet.Filter;
107
import javax.servlet.FilterChain;
@@ -16,14 +13,20 @@
1613
import javax.servlet.http.HttpServletRequest;
1714
import javax.servlet.http.HttpServletResponse;
1815

16+
import com.cxytiandi.encrypt.util.RequestUriUtils;
1917
import org.slf4j.Logger;
2018
import org.slf4j.LoggerFactory;
2119

2220
import com.cxytiandi.encrypt.algorithm.AesEncryptAlgorithm;
2321
import com.cxytiandi.encrypt.algorithm.EncryptAlgorithm;
22+
import org.springframework.util.AntPathMatcher;
2423
import org.springframework.util.CollectionUtils;
2524
import org.springframework.util.StringUtils;
2625
import org.springframework.web.bind.annotation.RequestMethod;
26+
import org.springframework.web.method.HandlerMethod;
27+
import org.springframework.web.servlet.DispatcherServlet;
28+
import org.springframework.web.servlet.HandlerExecutionChain;
29+
import org.springframework.web.servlet.HandlerMapping;
2730

2831
/**
2932
* 数据加解密过滤器
@@ -38,6 +41,8 @@ public class EncryptionFilter implements Filter {
3841

3942
private EncryptAlgorithm encryptAlgorithm = new AesEncryptAlgorithm();
4043

44+
private DispatcherServlet dispatcherServlet;
45+
4146
public EncryptionFilter() {
4247
this.encryptionConfig = new EncryptionConfig();
4348
}
@@ -46,9 +51,15 @@ public EncryptionFilter(EncryptionConfig config) {
4651
this.encryptionConfig = config;
4752
}
4853

49-
public EncryptionFilter(EncryptionConfig config, EncryptAlgorithm encryptAlgorithm) {
54+
public EncryptionFilter(EncryptionConfig config, DispatcherServlet dispatcherServlet) {
55+
this.encryptionConfig = config;
56+
this.dispatcherServlet = dispatcherServlet;
57+
}
58+
59+
public EncryptionFilter(EncryptionConfig config, EncryptAlgorithm encryptAlgorithm, DispatcherServlet dispatcherServlet) {
5060
this.encryptionConfig = config;
5161
this.encryptAlgorithm = encryptAlgorithm;
62+
this.dispatcherServlet = dispatcherServlet;
5263
}
5364

5465
public EncryptionFilter(String key) {
@@ -62,6 +73,8 @@ public EncryptionFilter(String key, List<String> responseEncryptUriList, List<St
6273
this.encryptionConfig = new EncryptionConfig(key, responseEncryptUriList, requestDecryptUriList, responseCharset, debug);
6374
}
6475

76+
private AntPathMatcher antPathMatcher = new AntPathMatcher();
77+
6578
@Override
6679
public void init(FilterConfig filterConfig) throws ServletException {
6780

@@ -82,10 +95,10 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
8295
return;
8396
}
8497

85-
boolean decryptionStatus = this.contains(encryptionConfig.getRequestDecryptUriList(), uri, req.getMethod());
86-
boolean encryptionStatus = this.contains(encryptionConfig.getResponseEncryptUriList(), uri, req.getMethod());
87-
boolean decryptionIgnoreStatus = this.contains(encryptionConfig.getRequestDecryptUriIgnoreList(), uri, req.getMethod());
88-
boolean encryptionIgnoreStatus = this.contains(encryptionConfig.getResponseEncryptUriIgnoreList(), uri, req.getMethod());
98+
boolean decryptionStatus = this.contains(encryptionConfig.getRequestDecryptUriList(), uri, req.getMethod(), req);
99+
boolean encryptionStatus = this.contains(encryptionConfig.getResponseEncryptUriList(), uri, req.getMethod(), req);
100+
boolean decryptionIgnoreStatus = this.contains(encryptionConfig.getRequestDecryptUriIgnoreList(), uri, req.getMethod(), req);
101+
boolean encryptionIgnoreStatus = this.contains(encryptionConfig.getResponseEncryptUriIgnoreList(), uri, req.getMethod(), req);
89102

90103
// 没有配置具体加解密的URI默认全部都开启加解密
91104
if (CollectionUtils.isEmpty(encryptionConfig.getRequestDecryptUriList())
@@ -205,7 +218,7 @@ private void writeEncryptContent(String responseData, ServletResponse response)
205218
}
206219
}
207220

208-
private boolean contains(List<String> list, String uri, String methodType) {
221+
private boolean contains(List<String> list, String uri, String methodType, HttpServletRequest request) {
209222
if (list.contains(uri)) {
210223
return true;
211224
}
@@ -214,9 +227,51 @@ private boolean contains(List<String> list, String uri, String methodType) {
214227
if (list.contains(prefixUri)) {
215228
return true;
216229
}
230+
231+
// 优先用AntPathMatcher,其实用这个也够了,底层是一样的,下面用的方式兜底
232+
for (String u : list) {
233+
boolean match = antPathMatcher.match(u, prefixUri);
234+
if (match) {
235+
return true;
236+
}
237+
}
238+
239+
try {
240+
// 支持RestFul风格API
241+
// 采用Spring MVC内置的匹配方式将当前请求匹配到对应的Controller Method上,获取注解进行匹配是否要加解密
242+
HandlerExecutionChain handler = getHandler(request);
243+
if (Objects.isNull(handler)) {
244+
return false;
245+
}
246+
247+
if (Objects.nonNull(handler.getHandler()) && handler.getHandler() instanceof HandlerMethod) {
248+
HandlerMethod handlerMethod = (HandlerMethod) handler.getHandler();
249+
String apiUri = RequestUriUtils.getApiUri(handlerMethod.getClass(), handlerMethod.getMethod(), request.getContextPath());
250+
if (list.contains(apiUri)) {
251+
return true;
252+
}
253+
}
254+
} catch (Exception e) {
255+
throw new RuntimeException(e);
256+
}
217257
return false;
218258
}
219259

260+
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
261+
if (Objects.isNull(dispatcherServlet)) {
262+
return null;
263+
}
264+
if (dispatcherServlet.getHandlerMappings() != null) {
265+
for (HandlerMapping mapping : dispatcherServlet.getHandlerMappings()) {
266+
HandlerExecutionChain handler = mapping.getHandler(request);
267+
if (handler != null) {
268+
return handler;
269+
}
270+
}
271+
}
272+
return null;
273+
}
274+
220275
@Override
221276
public void destroy() {
222277

monkey-api-encrypt-core/src/main/java/com/cxytiandi/encrypt/springboot/autoconfigure/EncryptAutoConfiguration.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.cxytiandi.encrypt.core.EncryptionConfig;
1313
import com.cxytiandi.encrypt.core.EncryptionFilter;
1414
import com.cxytiandi.encrypt.springboot.init.ApiEncryptDataInit;
15+
import org.springframework.web.servlet.DispatcherServlet;
1516

1617

1718
/**
@@ -30,6 +31,12 @@ public class EncryptAutoConfiguration {
3031
@Autowired(required = false)
3132
private EncryptAlgorithm encryptAlgorithm;
3233

34+
/**
35+
* 用于解决@PathVariable风格的URI
36+
*/
37+
@Autowired(required = false)
38+
private DispatcherServlet dispatcherServlet;
39+
3340
/**
3441
* 不要用泛型注册Filter,泛型在Spring Boot 2.x版本中才有
3542
*
@@ -40,9 +47,9 @@ public class EncryptAutoConfiguration {
4047
public FilterRegistrationBean filterRegistration() {
4148
FilterRegistrationBean registration = new FilterRegistrationBean();
4249
if (encryptAlgorithm != null) {
43-
registration.setFilter(new EncryptionFilter(encryptionConfig, encryptAlgorithm));
50+
registration.setFilter(new EncryptionFilter(encryptionConfig, encryptAlgorithm, dispatcherServlet));
4451
} else {
45-
registration.setFilter(new EncryptionFilter(encryptionConfig));
52+
registration.setFilter(new EncryptionFilter(encryptionConfig, dispatcherServlet));
4653
}
4754
registration.addUrlPatterns(encryptionConfig.getUrlPatterns());
4855
registration.setName("EncryptionFilter");

monkey-api-encrypt-core/src/main/java/com/cxytiandi/encrypt/springboot/init/ApiEncryptDataInit.java

+5-61
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.lang.reflect.Method;
44
import java.util.*;
55

6+
import com.cxytiandi.encrypt.util.RequestUriUtils;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
89
import org.springframework.beans.BeansException;
@@ -108,7 +109,7 @@ private void initData(Map<String, Object> beanMap) {
108109
// 注解中的URI优先级高
109110
String uri = encrypt.value();
110111
if (!StringUtils.hasText(uri)) {
111-
uri = getApiUri(clz, method);
112+
uri = RequestUriUtils.getApiUri(clz, method, contextPath);
112113
}
113114
logger.debug("Encrypt URI: {}", uri);
114115
responseEncryptUriList.add(uri);
@@ -117,7 +118,7 @@ private void initData(Map<String, Object> beanMap) {
117118
if (decrypt != null) {
118119
String uri = decrypt.value();
119120
if (!StringUtils.hasText(uri)) {
120-
uri = getApiUri(clz, method);
121+
uri = RequestUriUtils.getApiUri(clz, method, contextPath);
121122
}
122123

123124
String decyptParam = decrypt.decyptParam();
@@ -133,7 +134,7 @@ private void initData(Map<String, Object> beanMap) {
133134
// 注解中的URI优先级高
134135
String uri = encryptIgnore.value();
135136
if (!StringUtils.hasText(uri)) {
136-
uri = getApiUri(clz, method);
137+
uri = RequestUriUtils.getApiUri(clz, method, contextPath);
137138
}
138139
logger.debug("EncryptIgnore URI: {}", uri);
139140
responseEncryptUriIgnoreList.add(uri);
@@ -142,7 +143,7 @@ private void initData(Map<String, Object> beanMap) {
142143
if (decryptIgnore != null) {
143144
String uri = decryptIgnore.value();
144145
if (!StringUtils.hasText(uri)) {
145-
uri = getApiUri(clz, method);
146+
uri = RequestUriUtils.getApiUri(clz, method, contextPath);
146147
}
147148
logger.debug("DecryptIgnore URI: {}", uri);
148149
requestDecryptUriIgnoreList.add(uri);
@@ -152,62 +153,5 @@ private void initData(Map<String, Object> beanMap) {
152153
}
153154
}
154155

155-
private String getApiUri(Class<?> clz, Method method) {
156-
String methodType = "";
157-
StringBuilder uri = new StringBuilder();
158-
159-
RequestMapping reqMapping = AnnotationUtils.findAnnotation(clz, RequestMapping.class);
160-
if (reqMapping != null) {
161-
uri.append(formatUri(reqMapping.value()[0]));
162-
}
163-
164-
GetMapping getMapping = AnnotationUtils.findAnnotation(method, GetMapping.class);
165-
PostMapping postMapping = AnnotationUtils.findAnnotation(method, PostMapping.class);
166-
RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
167-
PutMapping putMapping = AnnotationUtils.findAnnotation(method, PutMapping.class);
168-
DeleteMapping deleteMapping = AnnotationUtils.findAnnotation(method, DeleteMapping.class);
169-
170-
if (getMapping != null) {
171-
methodType = HttpMethodTypePrefixConstant.GET;
172-
uri.append(formatUri(getMapping.value()[0]));
173-
174-
} else if (postMapping != null) {
175-
methodType = HttpMethodTypePrefixConstant.POST;
176-
uri.append(formatUri(postMapping.value()[0]));
177-
178-
} else if (putMapping != null) {
179-
methodType = HttpMethodTypePrefixConstant.PUT;
180-
uri.append(formatUri(putMapping.value()[0]));
181-
182-
} else if (deleteMapping != null) {
183-
methodType = HttpMethodTypePrefixConstant.DELETE;
184-
uri.append(formatUri(deleteMapping.value()[0]));
185-
186-
} else if (requestMapping != null) {
187-
RequestMethod requestMethod = RequestMethod.GET;
188-
if (requestMapping.method().length > 0) {
189-
requestMethod = requestMapping.method()[0];
190-
}
191156

192-
methodType = requestMethod.name().toLowerCase() + ":";
193-
uri.append(formatUri(requestMapping.value()[0]));
194-
195-
}
196-
197-
if (StringUtils.hasText(this.contextPath) && !"/".equals(this.contextPath)) {
198-
if (this.contextPath.endsWith("/")) {
199-
this.contextPath = this.contextPath.substring(0, this.contextPath.length() - 1);
200-
}
201-
return methodType + this.contextPath + uri.toString();
202-
}
203-
204-
return methodType + uri.toString();
205-
}
206-
207-
private String formatUri(String uri) {
208-
if (uri.startsWith("/")) {
209-
return uri;
210-
}
211-
return "/" + uri;
212-
}
213157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.cxytiandi.encrypt.util;
2+
3+
4+
import com.cxytiandi.encrypt.springboot.HttpMethodTypePrefixConstant;
5+
import org.springframework.core.annotation.AnnotationUtils;
6+
import org.springframework.util.StringUtils;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.lang.reflect.Method;
10+
11+
public class RequestUriUtils {
12+
13+
private static String separator = "/";
14+
15+
public static String getApiUri(Class<?> clz, Method method, String contextPath) {
16+
String methodType = "";
17+
StringBuilder uri = new StringBuilder();
18+
19+
RequestMapping reqMapping = AnnotationUtils.findAnnotation(clz, RequestMapping.class);
20+
if (reqMapping != null && reqMapping.value() != null && reqMapping.value().length > 0) {
21+
uri.append(formatUri(reqMapping.value()[0]));
22+
}
23+
24+
25+
GetMapping getMapping = AnnotationUtils.findAnnotation(method, GetMapping.class);
26+
PostMapping postMapping = AnnotationUtils.findAnnotation(method, PostMapping.class);
27+
RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
28+
PutMapping putMapping = AnnotationUtils.findAnnotation(method, PutMapping.class);
29+
DeleteMapping deleteMapping = AnnotationUtils.findAnnotation(method, DeleteMapping.class);
30+
31+
if (getMapping != null && getMapping.value() != null && getMapping.value().length > 0) {
32+
methodType = HttpMethodTypePrefixConstant.GET;
33+
uri.append(formatUri(getMapping.value()[0]));
34+
35+
} else if (postMapping != null && postMapping.value() != null && postMapping.value().length > 0) {
36+
methodType = HttpMethodTypePrefixConstant.POST;
37+
uri.append(formatUri(postMapping.value()[0]));
38+
39+
} else if (putMapping != null && putMapping.value() != null && putMapping.value().length > 0) {
40+
methodType = HttpMethodTypePrefixConstant.PUT;
41+
uri.append(formatUri(putMapping.value()[0]));
42+
43+
} else if (deleteMapping != null && deleteMapping.value() != null && deleteMapping.value().length > 0) {
44+
methodType = HttpMethodTypePrefixConstant.DELETE;
45+
uri.append(formatUri(deleteMapping.value()[0]));
46+
47+
} else if (requestMapping != null && requestMapping.value() != null && requestMapping.value().length > 0) {
48+
RequestMethod requestMethod = RequestMethod.GET;
49+
if (requestMapping.method().length > 0) {
50+
requestMethod = requestMapping.method()[0];
51+
}
52+
53+
methodType = requestMethod.name().toLowerCase() + ":";
54+
uri.append(formatUri(requestMapping.value()[0]));
55+
56+
}
57+
58+
if (StringUtils.hasText(contextPath) && !separator.equals(contextPath)) {
59+
if (contextPath.endsWith(separator)) {
60+
contextPath = contextPath.substring(0, contextPath.length() - 1);
61+
}
62+
return methodType + contextPath + uri.toString();
63+
}
64+
65+
return methodType + uri.toString();
66+
}
67+
68+
private static String formatUri(String uri) {
69+
if (uri.startsWith(separator)) {
70+
return uri;
71+
}
72+
return separator + uri;
73+
}
74+
}

0 commit comments

Comments
 (0)