Browse Source

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

疯狂的狮子Li 1 year ago
parent
commit
b55409bf37

+ 7 - 9
ruoyi-framework/src/main/java/com/ruoyi/framework/encrypt/MybatisDecryptInterceptor.java

@@ -1,6 +1,6 @@
 package com.ruoyi.framework.encrypt;
 
-import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.ruoyi.common.annotation.EncryptField;
 import com.ruoyi.common.encrypt.EncryptContext;
@@ -62,12 +62,12 @@ public class MybatisDecryptInterceptor implements Interceptor {
         }
         if (sourceObject instanceof List<?>) {
             List<?> sourceList = (List<?>) sourceObject;
-            if(CollectionUtil.isEmpty(sourceList)) {
+            if(CollUtil.isEmpty(sourceList)) {
                 return;
             }
             // 判断第一个元素是否含有注解。如果没有直接返回,提高效率
             Object firstItem = sourceList.get(0);
-            if (CollectionUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
+            if (ObjectUtil.isNull(firstItem) || CollUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
                 return;
             }
             ((List<?>) sourceObject).forEach(this::decryptHandler);
@@ -76,12 +76,7 @@ public class MybatisDecryptInterceptor implements Interceptor {
         Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass());
         try {
             for (Field field : fields) {
-                // 防止对象不是null 属性内容是null
-                Object obj = field.get(sourceObject);
-                if (ObjectUtil.isNull(obj)) {
-                    continue;
-                }
-                field.set(sourceObject, this.decryptField(String.valueOf(field.get(obj)), field));
+                field.set(sourceObject, this.decryptField(String.valueOf(field.get(sourceObject)), field));
             }
         } catch (Exception e) {
             log.error("处理解密字段时出错", e);
@@ -96,6 +91,9 @@ public class MybatisDecryptInterceptor implements Interceptor {
      * @return 加密后结果
      */
     private String decryptField(String value, Field field) {
+        if (ObjectUtil.isNull(value)) {
+            return null;
+        }
         EncryptField encryptField = field.getAnnotation(EncryptField.class);
         EncryptContext encryptContext = new EncryptContext();
         encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm());

+ 7 - 9
ruoyi-framework/src/main/java/com/ruoyi/framework/encrypt/MybatisEncryptInterceptor.java

@@ -1,6 +1,6 @@
 package com.ruoyi.framework.encrypt;
 
-import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.ruoyi.common.annotation.EncryptField;
 import com.ruoyi.common.encrypt.EncryptContext;
@@ -72,12 +72,12 @@ public class MybatisEncryptInterceptor implements Interceptor {
         }
         if (sourceObject instanceof List<?>) {
             List<?> sourceList = (List<?>) sourceObject;
-            if(CollectionUtil.isEmpty(sourceList)) {
+            if(CollUtil.isEmpty(sourceList)) {
                 return;
             }
             // 判断第一个元素是否含有注解。如果没有直接返回,提高效率
             Object firstItem = sourceList.get(0);
-            if (CollectionUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
+            if (ObjectUtil.isNull(firstItem) || CollUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
                 return;
             }
             ((List<?>) sourceObject).forEach(this::encryptHandler);
@@ -86,12 +86,7 @@ public class MybatisEncryptInterceptor implements Interceptor {
         Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass());
         try {
             for (Field field : fields) {
-                // 防止对象不是null 属性内容是null
-                Object obj = field.get(sourceObject);
-                if (ObjectUtil.isNull(obj)) {
-                    continue;
-                }
-                field.set(sourceObject, this.encryptField(String.valueOf(field.get(obj)), field));
+                field.set(sourceObject, this.encryptField(String.valueOf(field.get(sourceObject)), field));
             }
         } catch (Exception e) {
             log.error("处理加密字段时出错", e);
@@ -106,6 +101,9 @@ public class MybatisEncryptInterceptor implements Interceptor {
      * @return 加密后结果
      */
     private String encryptField(String value, Field field) {
+        if (ObjectUtil.isNull(value)) {
+            return null;
+        }
         EncryptField encryptField = field.getAnnotation(EncryptField.class);
         EncryptContext encryptContext = new EncryptContext();
         encryptContext.setAlgorithm(encryptField.algorithm() == AlgorithmType.DEFAULT ? defaultProperties.getAlgorithm() : encryptField.algorithm());