mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-12 14:57:10 +08:00
fix(security/crypto): 修复 构造默认加密上下文时缺失默认加密器 导致找不到加密器的问题。
This commit is contained in:
@@ -21,8 +21,8 @@ import cn.hutool.core.util.ReflectUtil;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import top.continew.starter.security.crypto.annotation.FieldEncrypt;
|
import top.continew.starter.security.crypto.annotation.FieldEncrypt;
|
||||||
import top.continew.starter.security.crypto.autoconfigure.CryptoProperties;
|
|
||||||
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
|
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
|
||||||
|
import top.continew.starter.security.crypto.autoconfigure.CryptoProperties;
|
||||||
import top.continew.starter.security.crypto.encryptor.IEncryptor;
|
import top.continew.starter.security.crypto.encryptor.IEncryptor;
|
||||||
import top.continew.starter.security.crypto.enums.Algorithm;
|
import top.continew.starter.security.crypto.enums.Algorithm;
|
||||||
|
|
||||||
@@ -68,14 +68,14 @@ public class EncryptHelper {
|
|||||||
* 计算 CryptoContext 对象的hashCode作为缓存中的key,通过hashCode查询缓存中存在则直接返回,不存在则创建并缓存
|
* 计算 CryptoContext 对象的hashCode作为缓存中的key,通过hashCode查询缓存中存在则直接返回,不存在则创建并缓存
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param encryptContext 加密执行者需要的相关配置参数
|
* @param cryptoContext 加密执行者需要的相关配置参数
|
||||||
* @return 加密执行者
|
* @return 加密执行者
|
||||||
*/
|
*/
|
||||||
public static IEncryptor registerAndGetEncryptor(CryptoContext encryptContext) {
|
public static IEncryptor registerAndGetEncryptor(CryptoContext cryptoContext) {
|
||||||
int key = encryptContext.hashCode();
|
int key = cryptoContext.hashCode();
|
||||||
return ENCRYPTOR_CACHE.computeIfAbsent(key, k -> encryptContext.getEncryptor().equals(IEncryptor.class)
|
return ENCRYPTOR_CACHE.computeIfAbsent(key, k -> cryptoContext.getEncryptor().equals(IEncryptor.class)
|
||||||
? ReflectUtil.newInstance(encryptContext.getAlgorithm().getEncryptor(), encryptContext)
|
? ReflectUtil.newInstance(cryptoContext.getAlgorithm().getEncryptor(), cryptoContext)
|
||||||
: ReflectUtil.newInstance(encryptContext.getEncryptor(), encryptContext));
|
: ReflectUtil.newInstance(cryptoContext.getEncryptor(), cryptoContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,8 +104,8 @@ public class EncryptHelper {
|
|||||||
}
|
}
|
||||||
String ciphertext = value;
|
String ciphertext = value;
|
||||||
try {
|
try {
|
||||||
CryptoContext encryptContext = buildEncryptContext(fieldEncrypt);
|
CryptoContext cryptoContext = buildCryptoContext(fieldEncrypt);
|
||||||
IEncryptor encryptor = registerAndGetEncryptor(encryptContext);
|
IEncryptor encryptor = registerAndGetEncryptor(cryptoContext);
|
||||||
ciphertext = encryptor.encrypt(ciphertext);
|
ciphertext = encryptor.encrypt(ciphertext);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("加密失败,请检查加密配置,处理加密字段异常:{}", e.getMessage(), e);
|
log.warn("加密失败,请检查加密配置,处理加密字段异常:{}", e.getMessage(), e);
|
||||||
@@ -125,8 +125,8 @@ public class EncryptHelper {
|
|||||||
}
|
}
|
||||||
String ciphertext = value;
|
String ciphertext = value;
|
||||||
try {
|
try {
|
||||||
CryptoContext encryptContext = buildEncryptContext();
|
CryptoContext cryptoContext = buildCryptoContext();
|
||||||
IEncryptor encryptor = registerAndGetEncryptor(encryptContext);
|
IEncryptor encryptor = registerAndGetEncryptor(cryptoContext);
|
||||||
ciphertext = encryptor.encrypt(ciphertext);
|
ciphertext = encryptor.encrypt(ciphertext);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("加密失败,请检查加密配置,处理加密字段异常:{}", e.getMessage(), e);
|
log.warn("加密失败,请检查加密配置,处理加密字段异常:{}", e.getMessage(), e);
|
||||||
@@ -147,8 +147,8 @@ public class EncryptHelper {
|
|||||||
}
|
}
|
||||||
String plaintext = value;
|
String plaintext = value;
|
||||||
try {
|
try {
|
||||||
CryptoContext encryptContext = buildEncryptContext(fieldEncrypt);
|
CryptoContext cryptoContext = buildCryptoContext(fieldEncrypt);
|
||||||
IEncryptor encryptor = registerAndGetEncryptor(encryptContext);
|
IEncryptor encryptor = registerAndGetEncryptor(cryptoContext);
|
||||||
plaintext = encryptor.decrypt(plaintext);
|
plaintext = encryptor.decrypt(plaintext);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("解密失败,请检查加密配置,处理解密字段异常:{}", e.getMessage(), e);
|
log.warn("解密失败,请检查加密配置,处理解密字段异常:{}", e.getMessage(), e);
|
||||||
@@ -168,8 +168,8 @@ public class EncryptHelper {
|
|||||||
}
|
}
|
||||||
String plaintext = value;
|
String plaintext = value;
|
||||||
try {
|
try {
|
||||||
CryptoContext encryptContext = buildEncryptContext();
|
CryptoContext cryptoContext = buildCryptoContext();
|
||||||
IEncryptor encryptor = registerAndGetEncryptor(encryptContext);
|
IEncryptor encryptor = registerAndGetEncryptor(cryptoContext);
|
||||||
plaintext = encryptor.decrypt(plaintext);
|
plaintext = encryptor.decrypt(plaintext);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("解密失败,请检查加密配置,处理解密字段异常:{}", e.getMessage(), e);
|
log.warn("解密失败,请检查加密配置,处理解密字段异常:{}", e.getMessage(), e);
|
||||||
@@ -183,24 +183,24 @@ public class EncryptHelper {
|
|||||||
* @param fieldEncrypt 字段加密注解
|
* @param fieldEncrypt 字段加密注解
|
||||||
* @return 加密上下文
|
* @return 加密上下文
|
||||||
*/
|
*/
|
||||||
private static CryptoContext buildEncryptContext(FieldEncrypt fieldEncrypt) {
|
private static CryptoContext buildCryptoContext(FieldEncrypt fieldEncrypt) {
|
||||||
CryptoContext encryptContext = new CryptoContext();
|
CryptoContext cryptoContext = new CryptoContext();
|
||||||
encryptContext.setAlgorithm(fieldEncrypt.value() == Algorithm.DEFAULT
|
cryptoContext.setAlgorithm(fieldEncrypt.value() == Algorithm.DEFAULT
|
||||||
? defaultProperties.getAlgorithm()
|
? defaultProperties.getAlgorithm()
|
||||||
: fieldEncrypt.value());
|
: fieldEncrypt.value());
|
||||||
encryptContext.setEncryptor(fieldEncrypt.encryptor().equals(IEncryptor.class)
|
cryptoContext.setEncryptor(fieldEncrypt.encryptor().equals(IEncryptor.class)
|
||||||
? IEncryptor.class
|
? IEncryptor.class
|
||||||
: fieldEncrypt.encryptor());
|
: fieldEncrypt.encryptor());
|
||||||
encryptContext.setPassword(fieldEncrypt.password().isEmpty()
|
cryptoContext.setPassword(fieldEncrypt.password().isEmpty()
|
||||||
? defaultProperties.getPassword()
|
? defaultProperties.getPassword()
|
||||||
: fieldEncrypt.password());
|
: fieldEncrypt.password());
|
||||||
encryptContext.setPrivateKey(fieldEncrypt.privateKey().isEmpty()
|
cryptoContext.setPrivateKey(fieldEncrypt.privateKey().isEmpty()
|
||||||
? defaultProperties.getPrivateKey()
|
? defaultProperties.getPrivateKey()
|
||||||
: fieldEncrypt.privateKey());
|
: fieldEncrypt.privateKey());
|
||||||
encryptContext.setPublicKey(fieldEncrypt.publicKey().isEmpty()
|
cryptoContext.setPublicKey(fieldEncrypt.publicKey().isEmpty()
|
||||||
? defaultProperties.getPublicKey()
|
? defaultProperties.getPublicKey()
|
||||||
: fieldEncrypt.publicKey());
|
: fieldEncrypt.publicKey());
|
||||||
return encryptContext;
|
return cryptoContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -208,12 +208,13 @@ public class EncryptHelper {
|
|||||||
*
|
*
|
||||||
* @return 加密上下文
|
* @return 加密上下文
|
||||||
*/
|
*/
|
||||||
private static CryptoContext buildEncryptContext() {
|
private static CryptoContext buildCryptoContext() {
|
||||||
CryptoContext encryptContext = new CryptoContext();
|
CryptoContext cryptoContext = new CryptoContext();
|
||||||
encryptContext.setAlgorithm(defaultProperties.getAlgorithm());
|
cryptoContext.setAlgorithm(defaultProperties.getAlgorithm());
|
||||||
encryptContext.setPassword(defaultProperties.getPassword());
|
cryptoContext.setEncryptor(IEncryptor.class);
|
||||||
encryptContext.setPrivateKey(defaultProperties.getPrivateKey());
|
cryptoContext.setPassword(defaultProperties.getPassword());
|
||||||
encryptContext.setPublicKey(defaultProperties.getPublicKey());
|
cryptoContext.setPrivateKey(defaultProperties.getPrivateKey());
|
||||||
return encryptContext;
|
cryptoContext.setPublicKey(defaultProperties.getPublicKey());
|
||||||
|
return cryptoContext;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user