Ver Fonte

优化数据加解密配置逻辑

woody há 9 meses atrás
pai
commit
c2cf10415c

+ 4 - 5
framework-common/src/main/java/com/chelvc/framework/common/crypto/AESCipherFactory.java

@@ -4,7 +4,6 @@ import javax.crypto.Cipher;
 
 import com.chelvc.framework.common.util.AESUtils;
 import com.chelvc.framework.common.util.AssertUtils;
-import com.chelvc.framework.common.util.ObjectUtils;
 import lombok.NonNull;
 
 /**
@@ -19,15 +18,15 @@ public class AESCipherFactory implements CipherFactory {
     private final String secret;
     private final String iv;
 
-    public AESCipherFactory(String secret) {
-        this(secret, ObjectUtils.ifNull(secret, AESUtils::secret2iv));
+    public AESCipherFactory(@NonNull String secret) {
+        this(secret, AESUtils.secret2iv(secret));
     }
 
-    public AESCipherFactory(String secret, String iv) {
+    public AESCipherFactory(@NonNull String secret, @NonNull String iv) {
         this(AESUtils.CBC_PKCS5PADDING, secret, iv);
     }
 
-    public AESCipherFactory(@NonNull String name, String secret, String iv) {
+    public AESCipherFactory(@NonNull String name, @NonNull String secret, @NonNull String iv) {
         this.key = name + secret;
         this.name = name;
         this.secret = secret;

+ 3 - 0
framework-common/src/main/java/com/chelvc/framework/common/util/AESUtils.java

@@ -85,6 +85,9 @@ public final class AESUtils {
      */
     public static String secret2iv(@NonNull String secret) {
         int size = Math.min(secret.length(), 16);
+        if (size == 0) {
+            return StringUtils.EMPTY;
+        }
         StringBuilder iv = new StringBuilder(size);
         for (int i = secret.length() - 1, max = 0; i >= 0 && max < size; i--, max++) {
             iv.append(secret.charAt(i));

+ 3 - 1
framework-database/src/main/java/com/chelvc/framework/database/context/DefaultDatabaseCryptoContext.java

@@ -1,6 +1,8 @@
 package com.chelvc.framework.database.context;
 
 import com.chelvc.framework.common.crypto.AESCipherFactory;
+import com.chelvc.framework.common.util.ObjectUtils;
+import com.chelvc.framework.common.util.StringUtils;
 import com.chelvc.framework.database.config.DatabaseProperties;
 import lombok.NonNull;
 
@@ -14,7 +16,7 @@ public class DefaultDatabaseCryptoContext extends AESCipherFactory implements Da
     private final DatabaseProperties properties;
 
     public DefaultDatabaseCryptoContext(@NonNull DatabaseProperties properties) {
-        super(properties.getSensitive().getSecret());
+        super(ObjectUtils.ifNull(properties.getSensitive().getSecret(), StringUtils.EMPTY));
         this.properties = properties;
     }
 

+ 3 - 1
framework-security/src/main/java/com/chelvc/framework/security/context/DefaultSecurityCryptoContext.java

@@ -1,6 +1,8 @@
 package com.chelvc.framework.security.context;
 
 import com.chelvc.framework.common.crypto.AESCipherFactory;
+import com.chelvc.framework.common.util.ObjectUtils;
+import com.chelvc.framework.common.util.StringUtils;
 import com.chelvc.framework.security.config.SecurityProperties;
 import lombok.NonNull;
 
@@ -12,6 +14,6 @@ import lombok.NonNull;
  */
 public class DefaultSecurityCryptoContext extends AESCipherFactory implements SecurityCryptoContext {
     public DefaultSecurityCryptoContext(@NonNull SecurityProperties properties) {
-        super(properties.getSecret());
+        super(ObjectUtils.ifNull(properties.getSecret(), StringUtils.EMPTY));
     }
 }