Skip to content

Commit feba691

Browse files
authored
Merge pull request #34 from yinjihuan/encrypt1.1
支持忽略接口加解密功能
2 parents 64956a0 + fce2e2e commit feba691

File tree

7 files changed

+157
-10
lines changed

7 files changed

+157
-10
lines changed

encrypt-core/src/main/java/com/cxytiandi/encrypt/core/EncryptionConfig.java

+39
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ public class EncryptionConfig {
3838
* 不支持@PathVariable格式的URI
3939
*/
4040
private List<String> requestDecyptUriList = new ArrayList<String>();
41+
42+
/**
43+
* 忽略加密的接口URI<br>
44+
* 比如:/user/list<br>
45+
* 不支持@PathVariable格式的URI
46+
*/
47+
private List<String> responseEncryptUriIgnoreList = new ArrayList<String>();
48+
49+
/**
50+
* 忽略对请求内容进行解密的接口URI<br>
51+
* 比如:/user/list<br>
52+
* 不支持@PathVariable格式的URI
53+
*/
54+
private List<String> requestDecyptUriIgnoreList = new ArrayList<String>();
4155

4256
/**
4357
* 响应数据编码
@@ -136,4 +150,29 @@ public void setOrder(int order) {
136150
public int getOrder() {
137151
return order;
138152
}
153+
154+
public List<String> getResponseEncryptUriIgnoreList() {
155+
// 配置了注解则用注解获取的URI
156+
if (ApiEncryptDataInit.responseEncryptUriIgnoreList.size() > 0) {
157+
return ApiEncryptDataInit.responseEncryptUriIgnoreList;
158+
}
159+
return responseEncryptUriIgnoreList;
160+
}
161+
162+
public void setResponseEncryptUriIgnoreList(List<String> responseEncryptUriIgnoreList) {
163+
this.responseEncryptUriIgnoreList = responseEncryptUriIgnoreList;
164+
}
165+
166+
public List<String> getRequestDecyptUriIgnoreList() {
167+
// 配置了注解则用注解获取的URI
168+
if (ApiEncryptDataInit.requestDecyptUriIgnoreList.size() > 0) {
169+
return ApiEncryptDataInit.requestDecyptUriIgnoreList;
170+
}
171+
return requestDecyptUriIgnoreList;
172+
}
173+
174+
public void setRequestDecyptUriIgnoreList(List<String> requestDecyptUriIgnoreList) {
175+
this.requestDecyptUriIgnoreList = requestDecyptUriIgnoreList;
176+
}
177+
139178
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
8484

8585
boolean decryptionStatus = this.contains(encryptionConfig.getRequestDecyptUriList(), uri, req.getMethod());
8686
boolean encryptionStatus = this.contains(encryptionConfig.getResponseEncryptUriList(), uri, req.getMethod());
87+
boolean decryptionIgnoreStatus = this.contains(encryptionConfig.getRequestDecyptUriIgnoreList(), uri, req.getMethod());
88+
boolean encryptionIgnoreStatus = this.contains(encryptionConfig.getResponseEncryptUriIgnoreList(), uri, req.getMethod());
8789

8890
// 没有配置具体加解密的URI默认全部都开启加解密
8991
if (encryptionConfig.getRequestDecyptUriList().size() == 0
@@ -92,6 +94,16 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
9294
encryptionStatus = true;
9395
}
9496

97+
// 接口在忽略加密列表中
98+
if (encryptionIgnoreStatus) {
99+
encryptionStatus = false;
100+
}
101+
102+
// 接口在忽略解密列表中
103+
if (decryptionIgnoreStatus) {
104+
decryptionStatus = false;
105+
}
106+
95107
// 没有加解密操作
96108
if (decryptionStatus == false && encryptionStatus == false) {
97109
chain.doFilter(req, resp);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.cxytiandi.encrypt.springboot.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* 忽略解密注解
11+
*
12+
* <p>加了此注解的接口将不进行数据解密操作<p>
13+
* <p>适用于全局开启加解密操作,但是想忽略某些接口场景<p>
14+
*
15+
* @author yinjihuan
16+
*
17+
* @about http://cxytiandi.com/about
18+
*
19+
*/
20+
@Target(ElementType.METHOD)
21+
@Retention(RetentionPolicy.RUNTIME)
22+
@Documented
23+
public @interface DecryptIgnore {
24+
25+
String value() default "";
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.cxytiandi.encrypt.springboot.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* 忽略加密注解
11+
*
12+
* <p>加了此注解的接口将不进行数据加密操作<p>
13+
* <p>适用于全局开启加解密操作,但是想忽略某些接口场景<p>
14+
*
15+
* @author yinjihuan
16+
*
17+
* @about http://cxytiandi.com/about
18+
*/
19+
@Target(ElementType.METHOD)
20+
@Retention(RetentionPolicy.RUNTIME)
21+
@Documented
22+
public @interface EncryptIgnore {
23+
24+
String value() default "";
25+
26+
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import com.cxytiandi.encrypt.springboot.HttpMethodTypePrefixConstant;
2525
import com.cxytiandi.encrypt.springboot.annotation.Decrypt;
26+
import com.cxytiandi.encrypt.springboot.annotation.DecryptIgnore;
2627
import com.cxytiandi.encrypt.springboot.annotation.Encrypt;
28+
import com.cxytiandi.encrypt.springboot.annotation.EncryptIgnore;
2729

2830
public class ApiEncryptDataInit implements ApplicationContextAware {
2931

@@ -43,6 +45,20 @@ public class ApiEncryptDataInit implements ApplicationContextAware {
4345
*/
4446
public static List<String> requestDecyptUriList = new ArrayList<String>();
4547

48+
/**
49+
* 忽略加密的接口URI<br>
50+
* 比如:/user/list<br>
51+
* 不支持@PathVariable格式的URI
52+
*/
53+
public static List<String> responseEncryptUriIgnoreList = new ArrayList<String>();
54+
55+
/**
56+
* 忽略对请求内容进行解密的接口URI<br>
57+
* 比如:/user/list<br>
58+
* 不支持@PathVariable格式的URI
59+
*/
60+
public static List<String> requestDecyptUriIgnoreList = new ArrayList<String>();
61+
4662
private String contextPath;
4763

4864
@Override
@@ -79,6 +95,25 @@ private void initData(Map<String, Object> beanMap) {
7995
logger.debug("Decrypt URI: {}", uri);
8096
requestDecyptUriList.add(uri);
8197
}
98+
EncryptIgnore encryptIgnore = AnnotationUtils.findAnnotation(method, EncryptIgnore.class);
99+
if (encryptIgnore != null) {
100+
// 注解中的URI优先级高
101+
String uri = encryptIgnore.value();
102+
if (!StringUtils.hasText(uri)) {
103+
uri = getApiUri(clz, method);
104+
}
105+
logger.debug("EncryptIgnore URI: {}", uri);
106+
responseEncryptUriIgnoreList.add(uri);
107+
}
108+
DecryptIgnore decryptIgnore = AnnotationUtils.findAnnotation(method, DecryptIgnore.class);
109+
if (decryptIgnore != null) {
110+
String uri = decryptIgnore.value();
111+
if (!StringUtils.hasText(uri)) {
112+
uri = getApiUri(clz, method);
113+
}
114+
logger.debug("DecryptIgnore URI: {}", uri);
115+
requestDecyptUriIgnoreList.add(uri);
116+
}
82117
}
83118
}
84119
}

encrypt-springboot-example/src/main/java/com/cxytiandi/encrypt_springboot_example/controller/UserController.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@
88
import org.springframework.web.bind.annotation.RestController;
99

1010
import com.cxytiandi.encrypt.springboot.annotation.Decrypt;
11+
import com.cxytiandi.encrypt.springboot.annotation.DecryptIgnore;
1112
import com.cxytiandi.encrypt.springboot.annotation.Encrypt;
13+
import com.cxytiandi.encrypt.springboot.annotation.EncryptIgnore;
1214
import com.cxytiandi.encrypt_springboot_example.dto.UserDto;
1315
import com.cxytiandi.encrypt_springboot_example.dto.UserXmlDto;
1416

1517
@RestController
1618
public class UserController {
1719

18-
@Encrypt
20+
//@Encrypt
1921
@GetMapping("/encryptStr")
2022
public String encryptStr() {
2123
return "加密字符串";
2224
}
2325

24-
@Encrypt
26+
//@Encrypt
2527
@GetMapping("/encryptEntity")
2628
public UserDto encryptEntity() {
2729
UserDto dto = new UserDto();
@@ -32,6 +34,8 @@ public UserDto encryptEntity() {
3234

3335
//@Encrypt
3436
//@Decrypt
37+
@DecryptIgnore
38+
@EncryptIgnore
3539
@PostMapping("/save")
3640
public UserDto save(@RequestBody UserDto dto) {
3741
System.err.println(dto.getId() + "\t" + dto.getName());

encrypt-springboot-example/src/main/resources/application.properties

+12-8
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ spring.freemarker.request-context-attribute=request
1212
spring.freemarker.suffix=.html
1313

1414
spring.encrypt.key=abcdef0123456789
15-
spring.encrypt.requestDecyptUriList[0]=/save
16-
spring.encrypt.requestDecyptUriList[1]=/decryptEntityXml
15+
#spring.encrypt.requestDecyptUriList[0]=/save
16+
#spring.encrypt.requestDecyptUriList[1]=/decryptEntityXml
1717

18-
spring.encrypt.responseEncryptUriList[0]=/encryptStr
19-
spring.encrypt.responseEncryptUriList[1]=/encryptEntity
20-
spring.encrypt.responseEncryptUriList[2]=/save
21-
spring.encrypt.responseEncryptUriList[3]=/encryptEntityXml
22-
spring.encrypt.responseEncryptUriList[4]=/decryptEntityXml
18+
#spring.encrypt.responseEncryptUriList[0]=/encryptStr
19+
#spring.encrypt.responseEncryptUriList[1]=/encryptEntity
20+
#spring.encrypt.responseEncryptUriList[2]=/save
21+
#spring.encrypt.responseEncryptUriList[3]=/encryptEntityXml
22+
#spring.encrypt.responseEncryptUriList[4]=/decryptEntityXml
2323
#spring.encrypt.urlPatterns[0]=/user/*
24-
#spring.encrypt.urlPatterns[1]=/save/*
24+
#spring.encrypt.urlPatterns[1]=/save/*
25+
26+
#spring.encrypt.requestDecyptUriIgnoreList[0]=/save
27+
#spring.encrypt.responseEncryptUriIgnoreList[0]=/encryptEntity
28+
#spring.encrypt.responseEncryptUriIgnoreList[1]=/save

0 commit comments

Comments
 (0)