PhoneEncryptionAspect.java
3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.bs.common.aop;
import cn.hutool.core.util.DesensitizedUtil;
import com.bs.common.annotation.PhoneEncryptField;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Objects;
/**
* 手机号加密切面
*
* @author danmo
* @date 2022年09月15日 14:26
*/
@Slf4j
@Aspect
@Component
public class PhoneEncryptionAspect {
private Object responseObj;
@Pointcut("@annotation(com.bs.common.annotation.PhoneEncryptMethod)")
public void annotationPointCut() {
}
@Around("annotationPointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Exception {
Object responseObj = new Object();
try {
Object requestObj = joinPoint.getArgs()[0];
responseObj = joinPoint.proceed();
handleEncrypt(responseObj);
} catch (NoSuchMethodException e) {
e.printStackTrace();
log.error("PhoneEncryptionAspect处理出现异常", e);
throw new RuntimeException("手机号加解密服务异常");
} catch (Throwable e) {
log.error("PhoneEncryptionAspect处理出现异常", e);
throw new RuntimeException("手机号加解密服务异常");
}
return responseObj;
}
/**
* 处理加密
*
* @param responseObj
*/
private void handleEncrypt(Object responseObj) throws Exception {
if (Objects.isNull(responseObj)) {
return;
}
//获取传入参数的字段list
if (responseObj instanceof Collection) {
((Collection<?>) responseObj).forEach(item -> {
Field[] dFields = item.getClass().getDeclaredFields();
extracted(item, dFields);
});
} else {
Field[] dFields = responseObj.getClass().getDeclaredFields();
extracted(responseObj, dFields);
}
}
private void extracted(Object item, Field[] dFields) {
for (Field field : dFields) {
//获取参数中加过注解的字段
boolean hasSecureField = field.isAnnotationPresent(PhoneEncryptField.class);
//判断有没有加过注解的字段
if (hasSecureField) {
field.setAccessible(true);
String plaintextValue;
String encryptValue = null;
try {
if (field.get(item) != null) {
plaintextValue = (String) field.get(item);
//如果有,进行加密操作,然后替换成加密之后的值
encryptValue = DesensitizedUtil.mobilePhone(plaintextValue);
field.set(item, encryptValue);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}