mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 08:57:17 +08:00
fix(security/crypto): 修复新版 API 未支持自定义加密器问题
This commit is contained in:
@@ -27,7 +27,7 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import top.continew.starter.core.constant.PropertiesConstants;
|
import top.continew.starter.core.constant.PropertiesConstants;
|
||||||
import top.continew.starter.security.crypto.core.MyBatisDecryptInterceptor;
|
import top.continew.starter.security.crypto.core.MyBatisDecryptInterceptor;
|
||||||
import top.continew.starter.security.crypto.core.MyBatisEncryptInterceptor;
|
import top.continew.starter.security.crypto.core.MyBatisEncryptInterceptor;
|
||||||
import top.continew.starter.security.crypto.utils.EncryptHelper;
|
import top.continew.starter.security.crypto.util.EncryptHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加/解密自动配置
|
* 加/解密自动配置
|
||||||
|
@@ -27,7 +27,7 @@ import org.apache.ibatis.plugin.Invocation;
|
|||||||
import org.apache.ibatis.plugin.Signature;
|
import org.apache.ibatis.plugin.Signature;
|
||||||
import org.apache.ibatis.type.SimpleTypeRegistry;
|
import org.apache.ibatis.type.SimpleTypeRegistry;
|
||||||
import top.continew.starter.security.crypto.annotation.FieldEncrypt;
|
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.lang.reflect.Field;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
@@ -30,7 +30,7 @@ import org.apache.ibatis.session.ResultHandler;
|
|||||||
import org.apache.ibatis.session.RowBounds;
|
import org.apache.ibatis.session.RowBounds;
|
||||||
import top.continew.starter.core.constant.StringConstants;
|
import top.continew.starter.core.constant.StringConstants;
|
||||||
import top.continew.starter.security.crypto.annotation.FieldEncrypt;
|
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.lang.reflect.Field;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@@ -33,6 +33,14 @@ public class CryptoContext {
|
|||||||
*/
|
*/
|
||||||
private Algorithm algorithm;
|
private Algorithm algorithm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密/解密处理器
|
||||||
|
* <p>
|
||||||
|
* 优先级高于加密/解密算法
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
Class<? extends IEncryptor> encryptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对称加密算法密钥
|
* 对称加密算法密钥
|
||||||
*/
|
*/
|
||||||
@@ -56,6 +64,14 @@ public class CryptoContext {
|
|||||||
this.algorithm = algorithm;
|
this.algorithm = algorithm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Class<? extends IEncryptor> getEncryptor() {
|
||||||
|
return encryptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEncryptor(Class<? extends IEncryptor> encryptor) {
|
||||||
|
this.encryptor = encryptor;
|
||||||
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
@@ -89,12 +105,13 @@ public class CryptoContext {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
CryptoContext that = (CryptoContext)o;
|
CryptoContext that = (CryptoContext)o;
|
||||||
return algorithm == that.algorithm && Objects.equals(password, that.password) && Objects
|
return algorithm == that.algorithm && Objects.equals(encryptor, that.encryptor) && Objects
|
||||||
.equals(publicKey, that.publicKey) && Objects.equals(privateKey, that.privateKey);
|
.equals(password, that.password) && Objects.equals(publicKey, that.publicKey) && Objects
|
||||||
|
.equals(privateKey, that.privateKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(algorithm, password, publicKey, privateKey);
|
return Objects.hash(algorithm, encryptor, password, publicKey, privateKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* 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.text.CharSequenceUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
@@ -73,8 +73,9 @@ public class EncryptHelper {
|
|||||||
*/
|
*/
|
||||||
public static IEncryptor registerAndGetEncryptor(CryptoContext encryptContext) {
|
public static IEncryptor registerAndGetEncryptor(CryptoContext encryptContext) {
|
||||||
int key = encryptContext.hashCode();
|
int key = encryptContext.hashCode();
|
||||||
return ENCRYPTOR_CACHE.computeIfAbsent(key, k -> ReflectUtil.newInstance(encryptContext.getAlgorithm()
|
return ENCRYPTOR_CACHE.computeIfAbsent(key, k -> encryptContext.getEncryptor().equals(IEncryptor.class)
|
||||||
.getEncryptor(), encryptContext));
|
? ReflectUtil.newInstance(encryptContext.getAlgorithm().getEncryptor(), encryptContext)
|
||||||
|
: ReflectUtil.newInstance(encryptContext.getEncryptor(), encryptContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -187,6 +188,9 @@ public class EncryptHelper {
|
|||||||
encryptContext.setAlgorithm(fieldEncrypt.value() == Algorithm.DEFAULT
|
encryptContext.setAlgorithm(fieldEncrypt.value() == Algorithm.DEFAULT
|
||||||
? defaultProperties.getAlgorithm()
|
? defaultProperties.getAlgorithm()
|
||||||
: fieldEncrypt.value());
|
: fieldEncrypt.value());
|
||||||
|
encryptContext.setEncryptor(fieldEncrypt.encryptor().equals(IEncryptor.class)
|
||||||
|
? IEncryptor.class
|
||||||
|
: fieldEncrypt.encryptor());
|
||||||
encryptContext.setPassword(fieldEncrypt.password().isEmpty()
|
encryptContext.setPassword(fieldEncrypt.password().isEmpty()
|
||||||
? defaultProperties.getPassword()
|
? defaultProperties.getPassword()
|
||||||
: fieldEncrypt.password());
|
: fieldEncrypt.password());
|
Reference in New Issue
Block a user