update 优化 加解密模块 将null判断下推防止任何可能的null出现

feature/model
疯狂的狮子Li 2 years ago
parent 1983c51747
commit efc4f86d88

@ -1,8 +1,11 @@
package org.dromara.common.encrypt.interceptor; package org.dromara.common.encrypt.interceptor;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.encrypt.annotation.EncryptField; import org.dromara.common.encrypt.annotation.EncryptField;
import org.dromara.common.encrypt.core.EncryptContext; import org.dromara.common.encrypt.core.EncryptContext;
@ -10,10 +13,6 @@ import org.dromara.common.encrypt.core.EncryptorManager;
import org.dromara.common.encrypt.enumd.AlgorithmType; import org.dromara.common.encrypt.enumd.AlgorithmType;
import org.dromara.common.encrypt.enumd.EncodeType; import org.dromara.common.encrypt.enumd.EncodeType;
import org.dromara.common.encrypt.properties.EncryptorProperties; import org.dromara.common.encrypt.properties.EncryptorProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.sql.Statement; import java.sql.Statement;
@ -62,12 +61,12 @@ public class MybatisDecryptInterceptor implements Interceptor {
return; return;
} }
if (sourceObject instanceof List<?> list) { if (sourceObject instanceof List<?> list) {
if(CollectionUtil.isEmpty(list)) { if(CollUtil.isEmpty(list)) {
return; return;
} }
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率 // 判断第一个元素是否含有注解。如果没有直接返回,提高效率
Object firstItem = list.get(0); Object firstItem = list.get(0);
if (CollectionUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) { if (ObjectUtil.isNull(firstItem) || CollUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
return; return;
} }
list.forEach(this::decryptHandler); list.forEach(this::decryptHandler);
@ -76,12 +75,7 @@ public class MybatisDecryptInterceptor implements Interceptor {
Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass()); Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass());
try { try {
for (Field field : fields) { for (Field field : fields) {
// 防止对象不是null 属性内容是null field.set(sourceObject, this.decryptField(String.valueOf(field.get(sourceObject)), field));
Object obj = field.get(sourceObject);
if (ObjectUtil.isNull(obj)) {
continue;
}
field.set(sourceObject, this.decryptField(String.valueOf(obj), field));
} }
} catch (Exception e) { } catch (Exception e) {
log.error("处理解密字段时出错", e); log.error("处理解密字段时出错", e);
@ -96,6 +90,9 @@ public class MybatisDecryptInterceptor implements Interceptor {
* @return * @return
*/ */
private String decryptField(String value, Field field) { private String decryptField(String value, Field field) {
if (ObjectUtil.isNull(value)) {
return null;
}
EncryptField encryptField = field.getAnnotation(EncryptField.class); EncryptField encryptField = field.getAnnotation(EncryptField.class);
EncryptContext encryptContext = new EncryptContext(); EncryptContext encryptContext = new EncryptContext();
encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm()); encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm());

@ -1,14 +1,7 @@
package org.dromara.common.encrypt.interceptor; package org.dromara.common.encrypt.interceptor;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.encrypt.annotation.EncryptField;
import org.dromara.common.encrypt.core.EncryptContext;
import org.dromara.common.encrypt.core.EncryptorManager;
import org.dromara.common.encrypt.enumd.AlgorithmType;
import org.dromara.common.encrypt.enumd.EncodeType;
import org.dromara.common.encrypt.properties.EncryptorProperties;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.parameter.ParameterHandler; import org.apache.ibatis.executor.parameter.ParameterHandler;
@ -16,6 +9,13 @@ import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.plugin.Signature;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.encrypt.annotation.EncryptField;
import org.dromara.common.encrypt.core.EncryptContext;
import org.dromara.common.encrypt.core.EncryptorManager;
import org.dromara.common.encrypt.enumd.AlgorithmType;
import org.dromara.common.encrypt.enumd.EncodeType;
import org.dromara.common.encrypt.properties.EncryptorProperties;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -70,12 +70,12 @@ public class MybatisEncryptInterceptor implements Interceptor {
return; return;
} }
if (sourceObject instanceof List<?> list) { if (sourceObject instanceof List<?> list) {
if(CollectionUtil.isEmpty(list)) { if(CollUtil.isEmpty(list)) {
return; return;
} }
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率 // 判断第一个元素是否含有注解。如果没有直接返回,提高效率
Object firstItem = list.get(0); Object firstItem = list.get(0);
if (CollectionUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) { if (ObjectUtil.isNull(firstItem) && CollUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
return; return;
} }
list.forEach(this::encryptHandler); list.forEach(this::encryptHandler);
@ -84,12 +84,7 @@ public class MybatisEncryptInterceptor implements Interceptor {
Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass()); Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass());
try { try {
for (Field field : fields) { for (Field field : fields) {
// 防止对象不是null 属性内容是null field.set(sourceObject, this.encryptField(String.valueOf(field.get(sourceObject)), field));
Object obj = field.get(sourceObject);
if (ObjectUtil.isNull(obj)) {
continue;
}
field.set(sourceObject, this.encryptField(String.valueOf(field.get(obj)), field));
} }
} catch (Exception e) { } catch (Exception e) {
log.error("处理加密字段时出错", e); log.error("处理加密字段时出错", e);
@ -104,6 +99,9 @@ public class MybatisEncryptInterceptor implements Interceptor {
* @return * @return
*/ */
private String encryptField(String value, Field field) { private String encryptField(String value, Field field) {
if (ObjectUtil.isNull(value)) {
return null;
}
EncryptField encryptField = field.getAnnotation(EncryptField.class); EncryptField encryptField = field.getAnnotation(EncryptField.class);
EncryptContext encryptContext = new EncryptContext(); EncryptContext encryptContext = new EncryptContext();
encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm()); encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm());

Loading…
Cancel
Save