Skip to content

Commit d0eddcb

Browse files
lishuyanlaCharles7c
authored andcommitted
fix(security/crypto): 修复 构造默认加密上下文时缺失默认加密器 导致找不到加密器的问题。
1 parent 0c606e6 commit d0eddcb

File tree

1 file changed

+31
-30
lines changed
  • continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util

1 file changed

+31
-30
lines changed

continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/util/EncryptHelper.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323
import top.continew.starter.security.crypto.annotation.FieldEncrypt;
24-
import top.continew.starter.security.crypto.autoconfigure.CryptoProperties;
2524
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
25+
import top.continew.starter.security.crypto.autoconfigure.CryptoProperties;
2626
import top.continew.starter.security.crypto.encryptor.IEncryptor;
2727
import top.continew.starter.security.crypto.enums.Algorithm;
2828

@@ -68,14 +68,14 @@ public static void init(CryptoProperties properties) {
6868
* 计算 CryptoContext 对象的hashCode作为缓存中的key,通过hashCode查询缓存中存在则直接返回,不存在则创建并缓存
6969
* </p>
7070
*
71-
* @param encryptContext 加密执行者需要的相关配置参数
71+
* @param cryptoContext 加密执行者需要的相关配置参数
7272
* @return 加密执行者
7373
*/
74-
public static IEncryptor registerAndGetEncryptor(CryptoContext encryptContext) {
75-
int key = encryptContext.hashCode();
76-
return ENCRYPTOR_CACHE.computeIfAbsent(key, k -> encryptContext.getEncryptor().equals(IEncryptor.class)
77-
? ReflectUtil.newInstance(encryptContext.getAlgorithm().getEncryptor(), encryptContext)
78-
: ReflectUtil.newInstance(encryptContext.getEncryptor(), encryptContext));
74+
public static IEncryptor registerAndGetEncryptor(CryptoContext cryptoContext) {
75+
int key = cryptoContext.hashCode();
76+
return ENCRYPTOR_CACHE.computeIfAbsent(key, k -> cryptoContext.getEncryptor().equals(IEncryptor.class)
77+
? ReflectUtil.newInstance(cryptoContext.getAlgorithm().getEncryptor(), cryptoContext)
78+
: ReflectUtil.newInstance(cryptoContext.getEncryptor(), cryptoContext));
7979
}
8080

8181
/**
@@ -104,8 +104,8 @@ public static String encrypt(String value, FieldEncrypt fieldEncrypt) {
104104
}
105105
String ciphertext = value;
106106
try {
107-
CryptoContext encryptContext = buildEncryptContext(fieldEncrypt);
108-
IEncryptor encryptor = registerAndGetEncryptor(encryptContext);
107+
CryptoContext cryptoContext = buildCryptoContext(fieldEncrypt);
108+
IEncryptor encryptor = registerAndGetEncryptor(cryptoContext);
109109
ciphertext = encryptor.encrypt(ciphertext);
110110
} catch (Exception e) {
111111
log.warn("加密失败,请检查加密配置,处理加密字段异常:{}", e.getMessage(), e);
@@ -125,8 +125,8 @@ public static String encrypt(String value) {
125125
}
126126
String ciphertext = value;
127127
try {
128-
CryptoContext encryptContext = buildEncryptContext();
129-
IEncryptor encryptor = registerAndGetEncryptor(encryptContext);
128+
CryptoContext cryptoContext = buildCryptoContext();
129+
IEncryptor encryptor = registerAndGetEncryptor(cryptoContext);
130130
ciphertext = encryptor.encrypt(ciphertext);
131131
} catch (Exception e) {
132132
log.warn("加密失败,请检查加密配置,处理加密字段异常:{}", e.getMessage(), e);
@@ -147,8 +147,8 @@ public static String decrypt(String value, FieldEncrypt fieldEncrypt) {
147147
}
148148
String plaintext = value;
149149
try {
150-
CryptoContext encryptContext = buildEncryptContext(fieldEncrypt);
151-
IEncryptor encryptor = registerAndGetEncryptor(encryptContext);
150+
CryptoContext cryptoContext = buildCryptoContext(fieldEncrypt);
151+
IEncryptor encryptor = registerAndGetEncryptor(cryptoContext);
152152
plaintext = encryptor.decrypt(plaintext);
153153
} catch (Exception e) {
154154
log.warn("解密失败,请检查加密配置,处理解密字段异常:{}", e.getMessage(), e);
@@ -168,8 +168,8 @@ public static String decrypt(String value) {
168168
}
169169
String plaintext = value;
170170
try {
171-
CryptoContext encryptContext = buildEncryptContext();
172-
IEncryptor encryptor = registerAndGetEncryptor(encryptContext);
171+
CryptoContext cryptoContext = buildCryptoContext();
172+
IEncryptor encryptor = registerAndGetEncryptor(cryptoContext);
173173
plaintext = encryptor.decrypt(plaintext);
174174
} catch (Exception e) {
175175
log.warn("解密失败,请检查加密配置,处理解密字段异常:{}", e.getMessage(), e);
@@ -183,37 +183,38 @@ public static String decrypt(String value) {
183183
* @param fieldEncrypt 字段加密注解
184184
* @return 加密上下文
185185
*/
186-
private static CryptoContext buildEncryptContext(FieldEncrypt fieldEncrypt) {
187-
CryptoContext encryptContext = new CryptoContext();
188-
encryptContext.setAlgorithm(fieldEncrypt.value() == Algorithm.DEFAULT
186+
private static CryptoContext buildCryptoContext(FieldEncrypt fieldEncrypt) {
187+
CryptoContext cryptoContext = new CryptoContext();
188+
cryptoContext.setAlgorithm(fieldEncrypt.value() == Algorithm.DEFAULT
189189
? defaultProperties.getAlgorithm()
190190
: fieldEncrypt.value());
191-
encryptContext.setEncryptor(fieldEncrypt.encryptor().equals(IEncryptor.class)
191+
cryptoContext.setEncryptor(fieldEncrypt.encryptor().equals(IEncryptor.class)
192192
? IEncryptor.class
193193
: fieldEncrypt.encryptor());
194-
encryptContext.setPassword(fieldEncrypt.password().isEmpty()
194+
cryptoContext.setPassword(fieldEncrypt.password().isEmpty()
195195
? defaultProperties.getPassword()
196196
: fieldEncrypt.password());
197-
encryptContext.setPrivateKey(fieldEncrypt.privateKey().isEmpty()
197+
cryptoContext.setPrivateKey(fieldEncrypt.privateKey().isEmpty()
198198
? defaultProperties.getPrivateKey()
199199
: fieldEncrypt.privateKey());
200-
encryptContext.setPublicKey(fieldEncrypt.publicKey().isEmpty()
200+
cryptoContext.setPublicKey(fieldEncrypt.publicKey().isEmpty()
201201
? defaultProperties.getPublicKey()
202202
: fieldEncrypt.publicKey());
203-
return encryptContext;
203+
return cryptoContext;
204204
}
205205

206206
/**
207207
* 构建加密上下文
208208
*
209209
* @return 加密上下文
210210
*/
211-
private static CryptoContext buildEncryptContext() {
212-
CryptoContext encryptContext = new CryptoContext();
213-
encryptContext.setAlgorithm(defaultProperties.getAlgorithm());
214-
encryptContext.setPassword(defaultProperties.getPassword());
215-
encryptContext.setPrivateKey(defaultProperties.getPrivateKey());
216-
encryptContext.setPublicKey(defaultProperties.getPublicKey());
217-
return encryptContext;
211+
private static CryptoContext buildCryptoContext() {
212+
CryptoContext cryptoContext = new CryptoContext();
213+
cryptoContext.setAlgorithm(defaultProperties.getAlgorithm());
214+
cryptoContext.setEncryptor(IEncryptor.class);
215+
cryptoContext.setPassword(defaultProperties.getPassword());
216+
cryptoContext.setPrivateKey(defaultProperties.getPrivateKey());
217+
cryptoContext.setPublicKey(defaultProperties.getPublicKey());
218+
return cryptoContext;
218219
}
219220
}

0 commit comments

Comments
 (0)