|
@@ -3,9 +3,13 @@ package com.ruoyi.framework.encrypt;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.ruoyi.common.annotation.EncryptField;
|
|
|
+import com.ruoyi.common.encrypt.EncryptContext;
|
|
|
+import com.ruoyi.common.enums.AlgorithmType;
|
|
|
+import com.ruoyi.common.enums.EncodeType;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
-import com.ruoyi.common.utils.spring.SpringUtils;
|
|
|
import com.ruoyi.framework.config.properties.EncryptorProperties;
|
|
|
+import com.ruoyi.framework.manager.EncryptorManager;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.ibatis.executor.parameter.ParameterHandler;
|
|
|
import org.apache.ibatis.plugin.Interceptor;
|
|
@@ -24,7 +28,7 @@ import java.util.Set;
|
|
|
* 入参加密拦截器
|
|
|
*
|
|
|
* @author 老马
|
|
|
- * @date 2023-01-10 16:42
|
|
|
+ * @version 4.6.0
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Intercepts({@Signature(
|
|
@@ -32,10 +36,11 @@ import java.util.Set;
|
|
|
method = "setParameters",
|
|
|
args = {PreparedStatement.class})
|
|
|
})
|
|
|
+@AllArgsConstructor
|
|
|
public class MybatisEncryptInterceptor implements Interceptor {
|
|
|
|
|
|
- private final EncryptorManager encryptorManager = SpringUtils.getBean(EncryptorManager.class);
|
|
|
- private final EncryptorProperties defaultProperties = SpringUtils.getBean(EncryptorProperties.class);
|
|
|
+ private final EncryptorManager encryptorManager;
|
|
|
+ private final EncryptorProperties defaultProperties;
|
|
|
|
|
|
@Override
|
|
|
public Object intercept(Invocation invocation) throws Throwable {
|
|
@@ -60,6 +65,7 @@ public class MybatisEncryptInterceptor implements Interceptor {
|
|
|
*
|
|
|
* @param sourceObject 待加密对象
|
|
|
*/
|
|
|
+ @SuppressWarnings("unchecked cast")
|
|
|
private void encryptHandler(Object sourceObject) {
|
|
|
if (sourceObject instanceof Map) {
|
|
|
((Map<?, Object>) sourceObject).values().forEach(this::encryptHandler);
|
|
@@ -68,13 +74,13 @@ public class MybatisEncryptInterceptor implements Interceptor {
|
|
|
if (sourceObject instanceof List) {
|
|
|
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率
|
|
|
Object firstItem = ((List<?>) sourceObject).get(0);
|
|
|
- if (CollectionUtil.isEmpty(EncryptedFieldsCache.get(firstItem.getClass()))) {
|
|
|
+ if (CollectionUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
|
|
|
return;
|
|
|
}
|
|
|
((List<?>) sourceObject).forEach(this::encryptHandler);
|
|
|
return;
|
|
|
}
|
|
|
- Set<Field> fields = EncryptedFieldsCache.get(sourceObject.getClass());
|
|
|
+ Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass());
|
|
|
try {
|
|
|
for (Field field : fields) {
|
|
|
field.set(sourceObject, this.encryptField(String.valueOf(field.get(sourceObject)), field));
|
|
@@ -93,15 +99,13 @@ public class MybatisEncryptInterceptor implements Interceptor {
|
|
|
*/
|
|
|
private String encryptField(String value, Field field) {
|
|
|
EncryptField encryptField = field.getAnnotation(EncryptField.class);
|
|
|
- EncryptorProperties properties = new EncryptorProperties();
|
|
|
- properties.setEnabled(true);
|
|
|
- properties.setAlgorithm(encryptField.algorithm());
|
|
|
- properties.setEncode(encryptField.encode());
|
|
|
- properties.setPassword(StringUtils.isEmpty(encryptField.password()) ? defaultProperties.getPassword() : encryptField.password());
|
|
|
- properties.setPrivateKey(StringUtils.isEmpty(encryptField.privateKey()) ? defaultProperties.getPrivateKey() : encryptField.privateKey());
|
|
|
- properties.setPublicKey(StringUtils.isEmpty(encryptField.publicKey()) ? defaultProperties.getPublicKey() : encryptField.publicKey());
|
|
|
- this.encryptorManager.registAndGetEncryptor(properties);
|
|
|
- return this.encryptorManager.encrypt(value, properties);
|
|
|
+ EncryptContext encryptContext = new EncryptContext();
|
|
|
+ encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm());
|
|
|
+ encryptContext.setEncode(encryptField.encode() == EncodeType.DEFAULT ? defaultProperties.getEncode() : encryptField.encode());
|
|
|
+ encryptContext.setPassword(StringUtils.isBlank(encryptField.password()) ? defaultProperties.getPassword() : encryptField.password());
|
|
|
+ encryptContext.setPrivateKey(StringUtils.isBlank(encryptField.privateKey()) ? defaultProperties.getPrivateKey() : encryptField.privateKey());
|
|
|
+ encryptContext.setPublicKey(StringUtils.isBlank(encryptField.publicKey()) ? defaultProperties.getPublicKey() : encryptField.publicKey());
|
|
|
+ return this.encryptorManager.encrypt(value, encryptContext);
|
|
|
}
|
|
|
|
|
|
|