diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/CryptoAutoConfiguration.java b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/CryptoAutoConfiguration.java index e1b76cda..e2e87579 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/CryptoAutoConfiguration.java +++ b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/CryptoAutoConfiguration.java @@ -27,7 +27,7 @@ import org.springframework.context.annotation.Bean; import top.continew.starter.core.constant.PropertiesConstants; import top.continew.starter.security.crypto.core.MyBatisDecryptInterceptor; import top.continew.starter.security.crypto.core.MyBatisEncryptInterceptor; -import top.continew.starter.security.crypto.utils.EncryptHelper; +import top.continew.starter.security.crypto.util.EncryptHelper; /** * 加/解密自动配置 diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/core/MyBatisDecryptInterceptor.java b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/core/MyBatisDecryptInterceptor.java index 6b5802d7..25202f8c 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/core/MyBatisDecryptInterceptor.java +++ b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/core/MyBatisDecryptInterceptor.java @@ -27,7 +27,7 @@ import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.type.SimpleTypeRegistry; import top.continew.starter.security.crypto.annotation.FieldEncrypt; -import top.continew.starter.security.crypto.utils.EncryptHelper; +import top.continew.starter.security.crypto.util.EncryptHelper; import java.lang.reflect.Field; import java.sql.Statement; diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/core/MyBatisEncryptInterceptor.java b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/core/MyBatisEncryptInterceptor.java index 261032f4..6126f477 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/core/MyBatisEncryptInterceptor.java +++ b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/core/MyBatisEncryptInterceptor.java @@ -30,7 +30,7 @@ import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import top.continew.starter.core.constant.StringConstants; import top.continew.starter.security.crypto.annotation.FieldEncrypt; -import top.continew.starter.security.crypto.utils.EncryptHelper; +import top.continew.starter.security.crypto.util.EncryptHelper; import java.lang.reflect.Field; import java.util.Arrays; diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/CryptoContext.java b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/CryptoContext.java index 7acba65c..7abf2f26 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/CryptoContext.java +++ b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/encryptor/CryptoContext.java @@ -33,6 +33,14 @@ public class CryptoContext { */ private Algorithm algorithm; + /** + * 加密/解密处理器 + *

+ * 优先级高于加密/解密算法 + *

+ */ + Class encryptor; + /** * 对称加密算法密钥 */ @@ -56,6 +64,14 @@ public class CryptoContext { this.algorithm = algorithm; } + public Class getEncryptor() { + return encryptor; + } + + public void setEncryptor(Class encryptor) { + this.encryptor = encryptor; + } + public String getPassword() { return password; } @@ -89,12 +105,13 @@ public class CryptoContext { return false; } CryptoContext that = (CryptoContext)o; - return algorithm == that.algorithm && Objects.equals(password, that.password) && Objects - .equals(publicKey, that.publicKey) && Objects.equals(privateKey, that.privateKey); + return algorithm == that.algorithm && Objects.equals(encryptor, that.encryptor) && Objects + .equals(password, that.password) && Objects.equals(publicKey, that.publicKey) && Objects + .equals(privateKey, that.privateKey); } @Override public int hashCode() { - return Objects.hash(algorithm, password, publicKey, privateKey); + return Objects.hash(algorithm, encryptor, password, publicKey, privateKey); } } diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/utils/EncryptHelper.java b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/EncryptHelper.java similarity index 93% rename from continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/utils/EncryptHelper.java rename to continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/EncryptHelper.java index d52fb958..ec532c7e 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/utils/EncryptHelper.java +++ b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/EncryptHelper.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.security.crypto.utils; +package top.continew.starter.security.crypto.util; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.ReflectUtil; @@ -73,8 +73,9 @@ public class EncryptHelper { */ public static IEncryptor registerAndGetEncryptor(CryptoContext encryptContext) { int key = encryptContext.hashCode(); - return ENCRYPTOR_CACHE.computeIfAbsent(key, k -> ReflectUtil.newInstance(encryptContext.getAlgorithm() - .getEncryptor(), encryptContext)); + return ENCRYPTOR_CACHE.computeIfAbsent(key, k -> encryptContext.getEncryptor().equals(IEncryptor.class) + ? ReflectUtil.newInstance(encryptContext.getAlgorithm().getEncryptor(), encryptContext) + : ReflectUtil.newInstance(encryptContext.getEncryptor(), encryptContext)); } /** @@ -187,6 +188,9 @@ public class EncryptHelper { encryptContext.setAlgorithm(fieldEncrypt.value() == Algorithm.DEFAULT ? defaultProperties.getAlgorithm() : fieldEncrypt.value()); + encryptContext.setEncryptor(fieldEncrypt.encryptor().equals(IEncryptor.class) + ? IEncryptor.class + : fieldEncrypt.encryptor()); encryptContext.setPassword(fieldEncrypt.password().isEmpty() ? defaultProperties.getPassword() : fieldEncrypt.password());