Skip to content

Commit 9a04482

Browse files
committed
完善整体加密逻辑
1 parent 6a18f3d commit 9a04482

File tree

9 files changed

+240
-24
lines changed

9 files changed

+240
-24
lines changed

src/main/java/cn/licoy/encryptbody/advice/EncryptBodyAdvice.java

Lines changed: 146 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package cn.licoy.encryptbody.advice;
22

33
import cn.licoy.encryptbody.annotation.*;
4+
import cn.licoy.encryptbody.bean.EncryptAnnotationInfoBean;
45
import cn.licoy.encryptbody.enums.EncryptBodyMethod;
56
import cn.licoy.encryptbody.enums.SHAEncryptType;
7+
import cn.licoy.encryptbody.exception.EncryptBodyFailException;
8+
import cn.licoy.encryptbody.exception.EncryptMethodNotFoundException;
9+
import cn.licoy.encryptbody.exception.KeyNotConfiguredException;
610
import cn.licoy.encryptbody.util.*;
711
import com.fasterxml.jackson.core.JsonProcessingException;
812
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -17,10 +21,13 @@
1721
import org.springframework.web.bind.annotation.ControllerAdvice;
1822
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
1923

24+
import java.lang.annotation.Annotation;
25+
2026

2127
/**
2228
* @author licoy.cn
2329
* @version 2018/9/4
30+
* ResponseBodyAdvice实现类
2431
*/
2532
@Order(1)
2633
@ControllerAdvice
@@ -40,56 +47,172 @@ public EncryptBodyAdvice(ObjectMapper objectMapper,EncryptBodyConfig config) {
4047

4148
@Override
4249
public boolean supports(MethodParameter returnType, Class converterType) {
50+
Annotation[] annotations = returnType.getDeclaringClass().getAnnotations();
51+
if(annotations!=null && annotations.length>0){
52+
for (Annotation annotation : annotations) {
53+
if(annotation instanceof EncryptBody ||
54+
annotation instanceof AESEncryptBody ||
55+
annotation instanceof DESEncryptBody ||
56+
annotation instanceof RSAEncryptBody ||
57+
annotation instanceof MD5EncryptBody ||
58+
annotation instanceof SHAEncryptBody){
59+
return true;
60+
}
61+
}
62+
}
4363
return returnType.getMethod().isAnnotationPresent(EncryptBody.class) ||
44-
returnType.getMethod().isAnnotationPresent(AESEncryptBody.class) ||
45-
returnType.getMethod().isAnnotationPresent(DESEncryptBody.class) ||
46-
returnType.getMethod().isAnnotationPresent(RSAEncryptBody.class) ||
47-
returnType.getMethod().isAnnotationPresent(MD5EncryptBody.class) ||
48-
returnType.getMethod().isAnnotationPresent(SHAEncryptBody.class)
49-
;
64+
returnType.getMethod().isAnnotationPresent(AESEncryptBody.class) ||
65+
returnType.getMethod().isAnnotationPresent(DESEncryptBody.class) ||
66+
returnType.getMethod().isAnnotationPresent(RSAEncryptBody.class) ||
67+
returnType.getMethod().isAnnotationPresent(MD5EncryptBody.class) ||
68+
returnType.getMethod().isAnnotationPresent(SHAEncryptBody.class);
5069
}
5170

5271
@Override
5372
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
5473
Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
5574
if(body==null) return null;
56-
EncryptBodyMethod encryptBodyMethod = null;
57-
if(returnType.getMethod().isAnnotationPresent(EncryptBody.class)){
58-
EncryptBody encryptBody = returnType.getMethodAnnotation(EncryptBody.class);
59-
encryptBodyMethod = encryptBody.value();
60-
}
6175
response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
62-
String s = null;
76+
String str = null;
6377
try {
64-
s = objectMapper.writeValueAsString(body);
78+
str = objectMapper.writeValueAsString(body);
6579
} catch (JsonProcessingException e) {
6680
e.printStackTrace();
6781
}
68-
return switchEncrypt(s, encryptBodyMethod);
82+
EncryptAnnotationInfoBean classAnnotation = getClassAnnotation(returnType.getDeclaringClass());
83+
if(classAnnotation!=null){
84+
return switchEncrypt(str, classAnnotation);
85+
}
86+
EncryptAnnotationInfoBean methodAnnotation = getMethodAnnotation(returnType);
87+
if(methodAnnotation!=null){
88+
return switchEncrypt(str, methodAnnotation);
89+
}
90+
throw new EncryptBodyFailException();
6991
}
7092

71-
private String switchEncrypt(String formatStringBody,EncryptBodyMethod method){
93+
/**
94+
* 获取方法控制器上的加密注解信息
95+
* @param methodParameter 控制器方法
96+
* @return 加密注解信息
97+
*/
98+
private EncryptAnnotationInfoBean getMethodAnnotation(MethodParameter methodParameter){
99+
if(methodParameter.getMethod().isAnnotationPresent(EncryptBody.class)){
100+
EncryptBody encryptBody = methodParameter.getMethodAnnotation(EncryptBody.class);
101+
return EncryptAnnotationInfoBean.builder()
102+
.encryptBodyMethod(encryptBody.value())
103+
.key(encryptBody.otherKey())
104+
.shaEncryptType(encryptBody.shaType())
105+
.build();
106+
}
107+
if(methodParameter.getMethod().isAnnotationPresent(MD5EncryptBody.class)){
108+
return EncryptAnnotationInfoBean.builder()
109+
.encryptBodyMethod(EncryptBodyMethod.MD5)
110+
.build();
111+
}
112+
if(methodParameter.getMethod().isAnnotationPresent(SHAEncryptBody.class)){
113+
return EncryptAnnotationInfoBean.builder()
114+
.encryptBodyMethod(EncryptBodyMethod.SHA)
115+
.shaEncryptType(methodParameter.getMethodAnnotation(SHAEncryptBody.class).value())
116+
.build();
117+
}
118+
if(methodParameter.getMethod().isAnnotationPresent(DESEncryptBody.class)){
119+
return EncryptAnnotationInfoBean.builder()
120+
.encryptBodyMethod(EncryptBodyMethod.DES)
121+
.key(methodParameter.getMethodAnnotation(DESEncryptBody.class).otherKey())
122+
.build();
123+
}
124+
if(methodParameter.getMethod().isAnnotationPresent(AESEncryptBody.class)){
125+
return EncryptAnnotationInfoBean.builder()
126+
.encryptBodyMethod(EncryptBodyMethod.AES)
127+
.key(methodParameter.getMethodAnnotation(AESEncryptBody.class).otherKey())
128+
.build();
129+
}
130+
return null;
131+
}
132+
133+
/**
134+
* 获取类控制器上的加密注解信息
135+
* @param clazz 控制器类
136+
* @return 加密注解信息
137+
*/
138+
private EncryptAnnotationInfoBean getClassAnnotation(Class clazz){
139+
Annotation[] annotations = clazz.getDeclaredAnnotations();
140+
if(annotations!=null && annotations.length>0){
141+
for (Annotation annotation : annotations) {
142+
if(annotation instanceof EncryptBody){
143+
EncryptBody encryptBody = (EncryptBody) annotation;
144+
return EncryptAnnotationInfoBean.builder()
145+
.encryptBodyMethod(encryptBody.value())
146+
.key(encryptBody.otherKey())
147+
.shaEncryptType(encryptBody.shaType())
148+
.build();
149+
}
150+
if(annotation instanceof MD5EncryptBody){
151+
return EncryptAnnotationInfoBean.builder()
152+
.encryptBodyMethod(EncryptBodyMethod.MD5)
153+
.build();
154+
}
155+
if(annotation instanceof SHAEncryptBody){
156+
return EncryptAnnotationInfoBean.builder()
157+
.encryptBodyMethod(EncryptBodyMethod.SHA)
158+
.shaEncryptType(((SHAEncryptBody) annotation).value())
159+
.build();
160+
}
161+
if(annotation instanceof DESEncryptBody){
162+
return EncryptAnnotationInfoBean.builder()
163+
.encryptBodyMethod(EncryptBodyMethod.DES)
164+
.key(((DESEncryptBody) annotation).otherKey())
165+
.build();
166+
}
167+
if(annotation instanceof AESEncryptBody){
168+
return EncryptAnnotationInfoBean.builder()
169+
.encryptBodyMethod(EncryptBodyMethod.AES)
170+
.key(((AESEncryptBody) annotation).otherKey())
171+
.build();
172+
}
173+
}
174+
}
175+
return null;
176+
}
177+
178+
179+
/**
180+
* 选择加密方式并进行加密
181+
* @param formatStringBody 目标加密字符串
182+
* @param infoBean 加密信息
183+
* @return 加密结果
184+
*/
185+
private String switchEncrypt(String formatStringBody,EncryptAnnotationInfoBean infoBean){
186+
EncryptBodyMethod method = infoBean.getEncryptBodyMethod();
187+
if(method==null){
188+
throw new EncryptMethodNotFoundException();
189+
}
72190
if(method == EncryptBodyMethod.MD5){
73191
return MD5EncryptUtil.encrypt(formatStringBody);
74192
}
75193
if(method == EncryptBodyMethod.SHA){
76-
return SHAEncryptUtil.encrypt(formatStringBody,SHAEncryptType.SHA256);
194+
SHAEncryptType shaEncryptType = infoBean.getShaEncryptType();
195+
if(shaEncryptType==null) shaEncryptType = SHAEncryptType.SHA256;
196+
return SHAEncryptUtil.encrypt(formatStringBody,shaEncryptType);
77197
}
198+
String key = infoBean.getKey();
78199
if(method == EncryptBodyMethod.DES){
79-
if(StringUtils.isNullOrEmpty(config.getDesKey())){
200+
if(StringUtils.isNullOrEmpty(config.getDesKey()) && StringUtils.isNullOrEmpty(key)){
80201
log.error("未配置des-key / Des-key not configured");
81-
throw new RuntimeException("未配置des-key / Des-key not configured");
202+
throw new KeyNotConfiguredException("未配置des-key / Des-key not configured");
82203
}
83-
return DESEncryptUtil.encrypt(formatStringBody,config.getDesKey());
204+
if(key==null) key=config.getDesKey();
205+
return DESEncryptUtil.encrypt(formatStringBody,key);
84206
}
85207
if(method == EncryptBodyMethod.AES){
86-
if(StringUtils.isNullOrEmpty(config.getAesKey())){
208+
if(StringUtils.isNullOrEmpty(config.getAesKey()) && StringUtils.isNullOrEmpty(key)){
87209
log.error("未配置aes-key / AES-key not configured");
88-
throw new RuntimeException("未配置aes-key / AES-key not configured");
210+
throw new KeyNotConfiguredException("未配置aes-key / AES-key not configured");
89211
}
90-
return AESEncryptUtil.encrypt(formatStringBody,config.getAesKey());
212+
if(key==null) key=config.getAesKey();
213+
return AESEncryptUtil.encrypt(formatStringBody,key);
91214
}
92-
return "encrypt data fail";
215+
throw new EncryptBodyFailException();
93216
}
94217

95218

src/main/java/cn/licoy/encryptbody/annotation/AESEncryptBody.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
@Retention(RetentionPolicy.RUNTIME)
1212
@Documented
1313
public @interface AESEncryptBody {
14+
15+
String otherKey() default "";
16+
1417
}

src/main/java/cn/licoy/encryptbody/annotation/DESEncryptBody.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
@Retention(RetentionPolicy.RUNTIME)
1212
@Documented
1313
public @interface DESEncryptBody {
14+
15+
String otherKey() default "";
16+
1417
}

src/main/java/cn/licoy/encryptbody/annotation/EncryptBody.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import cn.licoy.encryptbody.enums.EncryptBodyMethod;
5+
import cn.licoy.encryptbody.enums.SHAEncryptType;
56

67
import java.lang.annotation.*;
78

@@ -15,6 +16,10 @@
1516
@Documented
1617
public @interface EncryptBody {
1718

18-
EncryptBodyMethod value() default EncryptBodyMethod.AES;
19+
EncryptBodyMethod value() default EncryptBodyMethod.MD5;
20+
21+
String otherKey() default "";
22+
23+
SHAEncryptType shaType() default SHAEncryptType.SHA256;
1924

2025
}

src/main/java/cn/licoy/encryptbody/annotation/SHAEncryptBody.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.licoy.encryptbody.annotation;
22

3+
import cn.licoy.encryptbody.enums.SHAEncryptType;
4+
35
import java.lang.annotation.*;
46

57
/**
@@ -11,4 +13,7 @@
1113
@Retention(RetentionPolicy.RUNTIME)
1214
@Documented
1315
public @interface SHAEncryptBody {
16+
17+
SHAEncryptType value() default SHAEncryptType.SHA256;
18+
1419
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.licoy.encryptbody.bean;
2+
3+
import cn.licoy.encryptbody.enums.EncryptBodyMethod;
4+
import cn.licoy.encryptbody.enums.SHAEncryptType;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
/**
11+
* @author licoy.cn
12+
* @version 2018/9/6
13+
* 加密注解信息
14+
*/
15+
@Data
16+
@NoArgsConstructor
17+
@AllArgsConstructor
18+
@Builder
19+
public class EncryptAnnotationInfoBean {
20+
21+
private EncryptBodyMethod encryptBodyMethod;
22+
23+
private String key;
24+
25+
private SHAEncryptType shaEncryptType;
26+
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.licoy.encryptbody.exception;
2+
3+
/**
4+
* @author licoy.cn
5+
* @version 2018/9/6
6+
*/
7+
public class EncryptBodyFailException extends RuntimeException {
8+
9+
public EncryptBodyFailException() {
10+
super("encrypt body fail!");
11+
}
12+
13+
public EncryptBodyFailException(String message) {
14+
super(message);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.licoy.encryptbody.exception;
2+
3+
/**
4+
* @author licoy.cn
5+
* @version 2018/9/6
6+
*/
7+
public class EncryptMethodNotFoundException extends RuntimeException {
8+
9+
public EncryptMethodNotFoundException() {
10+
super("encrypt method not found!");
11+
}
12+
13+
public EncryptMethodNotFoundException(String message) {
14+
super(message);
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.licoy.encryptbody.exception;
2+
3+
4+
/**
5+
* @author licoy.cn
6+
* @version 2018/9/6
7+
* 未配置KEY运行时异常
8+
*/
9+
public class KeyNotConfiguredException extends RuntimeException {
10+
11+
public KeyNotConfiguredException() {
12+
super();
13+
}
14+
15+
public KeyNotConfiguredException(String message) {
16+
super(message);
17+
}
18+
}

0 commit comments

Comments
 (0)