提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.framework.encrypt; |
X |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.util.ObjectUtil; |
|
5 |
import com.dl.common.annotation.EncryptField; |
|
6 |
import com.dl.common.encrypt.EncryptContext; |
|
7 |
import com.dl.common.enums.AlgorithmType; |
|
8 |
import com.dl.common.enums.EncodeType; |
|
9 |
import com.dl.common.utils.StringUtils; |
|
10 |
import com.dl.framework.config.properties.EncryptorProperties; |
|
11 |
import com.dl.framework.manager.EncryptorManager; |
|
12 |
import lombok.AllArgsConstructor; |
|
13 |
import lombok.extern.slf4j.Slf4j; |
|
14 |
import org.apache.ibatis.executor.parameter.ParameterHandler; |
|
15 |
import org.apache.ibatis.plugin.Interceptor; |
|
16 |
import org.apache.ibatis.plugin.Intercepts; |
|
17 |
import org.apache.ibatis.plugin.Invocation; |
|
18 |
import org.apache.ibatis.plugin.Signature; |
|
19 |
|
|
20 |
import java.lang.reflect.Field; |
|
21 |
import java.sql.PreparedStatement; |
|
22 |
import java.util.*; |
|
23 |
|
|
24 |
/** |
|
25 |
* 入参加密拦截器 |
|
26 |
* |
|
27 |
* @author 老马 |
|
28 |
* @version 4.6.0 |
|
29 |
*/ |
|
30 |
@Slf4j |
|
31 |
@Intercepts({@Signature( |
|
32 |
type = ParameterHandler.class, |
|
33 |
method = "setParameters", |
|
34 |
args = {PreparedStatement.class}) |
|
35 |
}) |
|
36 |
@AllArgsConstructor |
|
37 |
public class MybatisEncryptInterceptor implements Interceptor { |
|
38 |
|
|
39 |
private final EncryptorManager encryptorManager; |
|
40 |
private final EncryptorProperties defaultProperties; |
|
41 |
|
|
42 |
@Override |
|
43 |
public Object intercept(Invocation invocation) throws Throwable { |
|
44 |
return invocation; |
|
45 |
} |
|
46 |
|
|
47 |
@Override |
|
48 |
public Object plugin(Object target) { |
|
49 |
if (target instanceof ParameterHandler) { |
|
50 |
// 进行加密操作 |
|
51 |
ParameterHandler parameterHandler = (ParameterHandler) target; |
|
52 |
Object parameterObject = parameterHandler.getParameterObject(); |
|
53 |
if (ObjectUtil.isNotNull(parameterObject) && !(parameterObject instanceof String)) { |
|
54 |
this.encryptHandler(parameterObject); |
|
55 |
} |
|
56 |
} |
|
57 |
return target; |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* 加密对象 |
|
62 |
* |
|
63 |
* @param sourceObject 待加密对象 |
|
64 |
*/ |
|
65 |
private void encryptHandler(Object sourceObject) { |
|
66 |
if (ObjectUtil.isNull(sourceObject)) { |
|
67 |
return; |
|
68 |
} |
|
69 |
if (sourceObject instanceof Map<?, ?>) { |
|
70 |
new HashSet<>(((Map<?, ?>) sourceObject).values()).forEach(this::encryptHandler); |
|
71 |
return; |
|
72 |
} |
|
73 |
if (sourceObject instanceof List<?>) { |
|
74 |
List<?> sourceList = (List<?>) sourceObject; |
|
75 |
if(CollUtil.isEmpty(sourceList)) { |
|
76 |
return; |
|
77 |
} |
|
78 |
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率 |
|
79 |
Object firstItem = sourceList.get(0); |
|
80 |
if (ObjectUtil.isNull(firstItem) || CollUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) { |
|
81 |
return; |
|
82 |
} |
|
83 |
((List<?>) sourceObject).forEach(this::encryptHandler); |
|
84 |
return; |
|
85 |
} |
|
86 |
Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass()); |
|
87 |
try { |
|
88 |
for (Field field : fields) { |
|
89 |
field.set(sourceObject, this.encryptField(String.valueOf(field.get(sourceObject)), field)); |
|
90 |
} |
|
91 |
} catch (Exception e) { |
|
92 |
log.error("处理加密字段时出错", e); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
/** |
|
97 |
* 字段值进行加密。通过字段的批注注册新的加密算法 |
|
98 |
* |
|
99 |
* @param value 待加密的值 |
|
100 |
* @param field 待加密字段 |
|
101 |
* @return 加密后结果 |
|
102 |
*/ |
|
103 |
private String encryptField(String value, Field field) { |
|
104 |
if (ObjectUtil.isNull(value)) { |
|
105 |
return null; |
|
106 |
} |
|
107 |
EncryptField encryptField = field.getAnnotation(EncryptField.class); |
|
108 |
EncryptContext encryptContext = new EncryptContext(); |
|
109 |
encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm()); |
|
110 |
encryptContext.setEncode(encryptField.encode() == EncodeType.DEFAULT ? defaultProperties.getEncode() : encryptField.encode()); |
|
111 |
encryptContext.setPassword(StringUtils.isBlank(encryptField.password()) ? defaultProperties.getPassword() : encryptField.password()); |
|
112 |
encryptContext.setPrivateKey(StringUtils.isBlank(encryptField.privateKey()) ? defaultProperties.getPrivateKey() : encryptField.privateKey()); |
|
113 |
encryptContext.setPublicKey(StringUtils.isBlank(encryptField.publicKey()) ? defaultProperties.getPublicKey() : encryptField.publicKey()); |
|
114 |
return this.encryptorManager.encrypt(value, encryptContext); |
|
115 |
} |
|
116 |
|
|
117 |
|
|
118 |
@Override |
|
119 |
public void setProperties(Properties properties) { |
|
120 |
} |
|
121 |
} |