fix(security/crypto): 修复新版 API 未支持自定义加密器问题

This commit is contained in:
书中自有颜如玉
2025-07-22 07:35:38 +00:00
committed by Charles7c
parent b7646cd87b
commit 36c30a20dd
5 changed files with 30 additions and 9 deletions

View File

@@ -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;
/**
* 加/解密自动配置

View File

@@ -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;

View File

@@ -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;

View File

@@ -33,6 +33,14 @@ public class CryptoContext {
*/
private Algorithm algorithm;
/**
* 加密/解密处理器
* <p>
* 优先级高于加密/解密算法
* </p>
*/
Class<? extends IEncryptor> encryptor;
/**
* 对称加密算法密钥
*/
@@ -56,6 +64,14 @@ public class CryptoContext {
this.algorithm = algorithm;
}
public Class<? extends IEncryptor> getEncryptor() {
return encryptor;
}
public void setEncryptor(Class<? extends IEncryptor> 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);
}
}

View File

@@ -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());