修改如下:

1.EncryptorManager修改为采用反射创建类
2.IEncryptor接口中增加初始化方法
3.增加加密上下文对象用来隔离批注和配置文件
This commit is contained in:
mayuanfei 2023-01-17 11:11:27 +08:00
parent 3fd825cf0c
commit 3969ccff8c
11 changed files with 166 additions and 52 deletions

View File

@ -0,0 +1,34 @@
package com.ruoyi.common.encrypt;
import com.ruoyi.common.enums.EncodeType;
import lombok.Data;
/**
* 加密上下文用于encryptor传递必要的参数
* 隔离配置和注解
*
* @author 老马
* @date 2023-01-17 08:31
*/
@Data
public class EncryptContext {
/**
* 安全秘钥
*/
private String password;
/**
* 公钥
*/
private String publicKey;
/**
* 私钥
*/
private String privateKey;
/**
* 编码方式base64/hex
*/
private EncodeType encode;
}

View File

@ -20,6 +20,16 @@ public interface IEncryptor {
*/ */
AlgorithmType algorithm(); AlgorithmType algorithm();
/**
* 初始化加密者
*
* @param context 加密上下文
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/17 09:01
*/
void init(EncryptContext context) throws Exception;
/** /**
* 加密 * 加密
* *

View File

@ -14,27 +14,27 @@ public enum AlgorithmType {
/** /**
* base64 * base64
*/ */
BASE64("base64"), BASE64("com.ruoyi.framework.encrypt.encryptor.Base64Encryptor"),
/** /**
* aes * aes
*/ */
AES("aes"), AES("com.ruoyi.framework.encrypt.encryptor.AesEncryptor"),
/** /**
* rsa * rsa
*/ */
RSA("rsa"), RSA("com.ruoyi.framework.encrypt.encryptor.RsaEncryptor"),
/** /**
* sm2 * sm2
*/ */
SM2("sm2"), SM2("com.ruoyi.framework.encrypt.encryptor.Sm2Encryptor"),
/** /**
* sm4 * sm4
*/ */
SM4("sm4"); SM4("com.ruoyi.framework.encrypt.encryptor.Sm4Encryptor");
private final String algorithm; private final String clazz;
} }

View File

@ -1,26 +1,20 @@
package com.ruoyi.common.enums; package com.ruoyi.common.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/** /**
* 编码类型 * 编码类型
* *
* @author 老马 * @author 老马
* @date 2023-01-11 11:39 * @date 2023-01-11 11:39
*/ */
@Getter
@AllArgsConstructor
public enum EncodeType { public enum EncodeType {
/** /**
* base64编码 * base64编码
*/ */
BASE64("base64"), BASE64,
/** /**
* 16进制编码 * 16进制编码
*/ */
HEX("hex"); HEX;
private final String encode;
} }

View File

@ -46,12 +46,14 @@ public class TestDemo extends BaseEntity {
/** /**
* key键 * key键
*/ */
@EncryptField(algorithm=AlgorithmType.SM2, privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgZSlOvw8FBiH+aFJWLYZP/VRjg9wjfRarTkGBZd/T3N+gCgYIKoEcz1UBgi2hRANCAAR5DGuQwJqkxnbCsP+iPSDoHWIF4RwcR5EsSvT8QPxO1wRkR2IhCkzvRb32x2CUgJFdvoqVqfApFDPZzShqzBwX", publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEeQxrkMCapMZ2wrD/oj0g6B1iBeEcHEeRLEr0/ED8TtcEZEdiIQpM70W99sdglICRXb6KlanwKRQz2c0oaswcFw==") // @EncryptField(algorithm=AlgorithmType.SM2, privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgZSlOvw8FBiH+aFJWLYZP/VRjg9wjfRarTkGBZd/T3N+gCgYIKoEcz1UBgi2hRANCAAR5DGuQwJqkxnbCsP+iPSDoHWIF4RwcR5EsSvT8QPxO1wRkR2IhCkzvRb32x2CUgJFdvoqVqfApFDPZzShqzBwX", publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEeQxrkMCapMZ2wrD/oj0g6B1iBeEcHEeRLEr0/ED8TtcEZEdiIQpM70W99sdglICRXb6KlanwKRQz2c0oaswcFw==")
@EncryptField(algorithm = AlgorithmType.RSA, privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBANBBEeueWlXlkkj2+WY5l+IWe42d8b5K28g+G/CFKC/yYAEHtqGlCsBOrb+YBkG9mPzmuYA/n9k0NFIc8E8yY5vZQaroyFBrTTWEzG9RY2f7Y3svVyybs6jpXSUs4xff8abo7wL1Y/wUaeatTViamxYnyTvdTmLm3d+JjRij68rxAgMBAAECgYAB0TnhXraSopwIVRfmboea1b0upl+BUdTJcmci412UjrKr5aE695ZLPkXbFXijVu7HJlyyv94NVUdaMACV7Ku/S2RuNB70M7YJm8rAjHFC3/i2ZeIM60h1Ziy4QKv0XM3pRATlDCDNhC1WUrtQCQSgU8kcp6eUUppruOqDzcY04QJBAPm9+sBP9CwDRgy3e5+V8aZtJkwDstb0lVVV/KY890cydVxiCwvX3fqVnxKMlb+x0YtH0sb9v+71xvK2lGobaRECQQDVePU6r/cCEfpc+nkWF6osAH1f8Mux3rYv2DoBGvaPzV2BGfsLed4neRfCwWNCKvGPCdW+L0xMJg8+RwaoBUPhAkAT5kViqXxFPYWJYd1h2+rDXhMdH3ZSlm6HvDBDdrwlWinr0Iwcx3iSjPV93uHXwm118aUj4fg3LDJMCKxOwBxhAkByrQXfvwOMYygBprRBf/j0plazoWFrbd6lGR0f1uI5IfNnFRPdeFw1DEINZ2Hw+6zEUF44SqRMC+4IYJNc02dBAkBCgy7RvfyV/A7N6kKXxTHauY0v6XwSSvpeKtRJkbIcRWOdIYvaHO9L7cklj3vIEdwjSUp9K4VTBYYlmAz1xh03", publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQQRHrnlpV5ZJI9vlmOZfiFnuNnfG+StvIPhvwhSgv8mABB7ahpQrATq2/mAZBvZj85rmAP5/ZNDRSHPBPMmOb2UGq6MhQa001hMxvUWNn+2N7L1csm7Oo6V0lLOMX3/Gm6O8C9WP8FGnmrU1YmpsWJ8k73U5i5t3fiY0Yo+vK8QIDAQAB")
private String testKey; private String testKey;
/** /**
* *
*/ */
//@EncryptField(algorithm = AlgorithmType.SM4, password = "10rfylhtccpuyke5")
@EncryptField(algorithm = AlgorithmType.AES, password = "10rfylhtccpuyke5") @EncryptField(algorithm = AlgorithmType.AES, password = "10rfylhtccpuyke5")
private String value; private String value;

View File

@ -1,9 +1,11 @@
package com.ruoyi.framework.encrypt; package com.ruoyi.framework.encrypt;
import cn.hutool.core.util.ReflectUtil;
import com.ruoyi.common.encrypt.EncryptContext;
import com.ruoyi.common.encrypt.IEncryptor; import com.ruoyi.common.encrypt.IEncryptor;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.properties.EncryptorProperties; import com.ruoyi.framework.config.properties.EncryptorProperties;
import com.ruoyi.framework.encrypt.encryptor.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.Map; import java.util.Map;
@ -36,26 +38,14 @@ public class EncryptorManager {
if (encryptorMap.containsKey(encryptorKey)) { if (encryptorMap.containsKey(encryptorKey)) {
return encryptorMap.get(encryptorKey); return encryptorMap.get(encryptorKey);
} }
switch (properties.getAlgorithm()) { EncryptContext encryptContext = BeanCopyUtils.copy(properties, EncryptContext.class);
case BASE64: IEncryptor encryptor = ReflectUtil.newInstance(properties.getAlgorithm().getClazz());
encryptorMap.put(encryptorKey, new Base64Encryptor()); try {
break; encryptor.init(encryptContext);
case AES: } catch (Exception e) {
encryptorMap.put(encryptorKey, new AesEncryptor(properties.getPassword())); log.error("加密执行者注册失败。", e);
break;
case RSA:
encryptorMap.put(encryptorKey, new RsaEncryptor(properties.getPrivateKey(), properties.getPublicKey()));
break;
case SM2:
encryptorMap.put(encryptorKey, new Sm2Encryptor(properties.getPrivateKey(), properties.getPublicKey()));
break;
case SM4:
encryptorMap.put(encryptorKey, new Sm4Encryptor(properties.getPassword()));
break;
default:
Base64Encryptor defaultEncryptor = new Base64Encryptor();
encryptorMap.put(encryptorKey, defaultEncryptor);
} }
encryptorMap.put(encryptorKey, encryptor);
return encryptorMap.get(encryptorKey); return encryptorMap.get(encryptorKey);
} }
@ -118,7 +108,7 @@ public class EncryptorManager {
* @date 2023/1/11 17:39 * @date 2023/1/11 17:39
*/ */
private String getEncryptorKeyFromProperties(EncryptorProperties properties) { private String getEncryptorKeyFromProperties(EncryptorProperties properties) {
return properties.getAlgorithm().getAlgorithm() + StringUtils.defaultString(properties.getPassword()) + return properties.getAlgorithm() + StringUtils.defaultString(properties.getPassword()) +
StringUtils.defaultString(properties.getPublicKey()) + StringUtils.defaultString(properties.getPrivateKey()); StringUtils.defaultString(properties.getPublicKey()) + StringUtils.defaultString(properties.getPrivateKey());
} }

View File

@ -1,8 +1,11 @@
package com.ruoyi.framework.encrypt.encryptor; package com.ruoyi.framework.encrypt.encryptor;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES; import cn.hutool.crypto.symmetric.AES;
import com.ruoyi.common.encrypt.EncryptContext;
import com.ruoyi.common.encrypt.IEncryptor; import com.ruoyi.common.encrypt.IEncryptor;
import com.ruoyi.common.enums.AlgorithmType; import com.ruoyi.common.enums.AlgorithmType;
import com.ruoyi.common.enums.EncodeType; import com.ruoyi.common.enums.EncodeType;
@ -19,10 +22,6 @@ public class AesEncryptor implements IEncryptor {
private AES aes = null; private AES aes = null;
public AesEncryptor(String password) {
aes = SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8));
}
/** /**
* 获得当前算法 * 获得当前算法
* *
@ -35,6 +34,27 @@ public class AesEncryptor implements IEncryptor {
return AlgorithmType.AES; return AlgorithmType.AES;
} }
/**
* 初始化加密者
*
* @param context 加密上下文
* @author 老马
* @date 2023/1/17 09:01
*/
@Override
public void init(EncryptContext context) throws Exception {
String password = context.getPassword();
if (StrUtil.isBlank(password)) {
throw new RuntimeException("aes没有获得秘钥信息");
}
// aes算法的秘钥要求是16位24位32位
int[] array = {16, 24, 32};
if(!ArrayUtil.contains(array, password.length())) {
throw new RuntimeException("aes秘钥长度应该为16位、24位、32位实际为"+password.length()+"");
}
aes = SecureUtil.aes(context.getPassword().getBytes(StandardCharsets.UTF_8));
}
/** /**
* 加密 * 加密
* *

View File

@ -1,6 +1,7 @@
package com.ruoyi.framework.encrypt.encryptor; package com.ruoyi.framework.encrypt.encryptor;
import cn.hutool.core.codec.Base64; import cn.hutool.core.codec.Base64;
import com.ruoyi.common.encrypt.EncryptContext;
import com.ruoyi.common.encrypt.IEncryptor; import com.ruoyi.common.encrypt.IEncryptor;
import com.ruoyi.common.enums.AlgorithmType; import com.ruoyi.common.enums.AlgorithmType;
import com.ruoyi.common.enums.EncodeType; import com.ruoyi.common.enums.EncodeType;
@ -24,6 +25,18 @@ public class Base64Encryptor implements IEncryptor {
return AlgorithmType.BASE64; return AlgorithmType.BASE64;
} }
/**
* 初始化加密者
*
* @param context 加密上下文
* @author 老马
* @date 2023/1/17 09:01
*/
@Override
public void init(EncryptContext context) {
// 无需初始化
}
/** /**
* 加密 * 加密
* *

View File

@ -5,9 +5,11 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA; import cn.hutool.crypto.asymmetric.RSA;
import com.ruoyi.common.encrypt.EncryptContext;
import com.ruoyi.common.encrypt.IEncryptor; import com.ruoyi.common.encrypt.IEncryptor;
import com.ruoyi.common.enums.AlgorithmType; import com.ruoyi.common.enums.AlgorithmType;
import com.ruoyi.common.enums.EncodeType; import com.ruoyi.common.enums.EncodeType;
import com.ruoyi.common.utils.StringUtils;
/** /**
@ -20,10 +22,6 @@ public class RsaEncryptor implements IEncryptor {
private RSA rsa = null; private RSA rsa = null;
public RsaEncryptor(String privateKey, String publicKey) {
this.rsa = SecureUtil.rsa(Base64.decode(privateKey), Base64.decode(publicKey));
}
/** /**
* 获得当前算法 * 获得当前算法
* *
@ -36,6 +34,24 @@ public class RsaEncryptor implements IEncryptor {
return AlgorithmType.RSA; return AlgorithmType.RSA;
} }
/**
* 初始化加密者
*
* @param context 加密上下文
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/17 09:01
*/
@Override
public void init(EncryptContext context) throws Exception {
String privateKey = context.getPrivateKey();
String publicKey = context.getPublicKey();
if (StringUtils.isAnyEmpty(privateKey, publicKey)) {
throw new RuntimeException("rsa公私钥均需要提供公钥加密私钥解密。");
}
this.rsa = SecureUtil.rsa(Base64.decode(privateKey), Base64.decode(publicKey));
}
/** /**
* 加密 * 加密
* *

View File

@ -6,9 +6,11 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SmUtil; import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2; import cn.hutool.crypto.asymmetric.SM2;
import com.ruoyi.common.encrypt.EncryptContext;
import com.ruoyi.common.encrypt.IEncryptor; import com.ruoyi.common.encrypt.IEncryptor;
import com.ruoyi.common.enums.AlgorithmType; import com.ruoyi.common.enums.AlgorithmType;
import com.ruoyi.common.enums.EncodeType; import com.ruoyi.common.enums.EncodeType;
import com.ruoyi.common.utils.StringUtils;
/** /**
* sm2算法实现 * sm2算法实现
@ -20,10 +22,6 @@ public class Sm2Encryptor implements IEncryptor {
private SM2 sm2 = null; private SM2 sm2 = null;
public Sm2Encryptor(String privateKey, String publicKey) {
sm2 = SmUtil.sm2(Base64.decode(privateKey), Base64.decode(publicKey));
}
/** /**
* 获得当前算法 * 获得当前算法
* *
@ -36,6 +34,24 @@ public class Sm2Encryptor implements IEncryptor {
return AlgorithmType.SM2; return AlgorithmType.SM2;
} }
/**
* 初始化加密者
*
* @param context 加密上下文
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/17 09:01
*/
@Override
public void init(EncryptContext context) throws Exception {
String privateKey = context.getPrivateKey();
String publicKey = context.getPublicKey();
if (StringUtils.isAnyEmpty(privateKey, publicKey)) {
throw new RuntimeException("sm2公私钥均需要提供公钥加密私钥解密。");
}
this.sm2 = SmUtil.sm2(Base64.decode(privateKey), Base64.decode(publicKey));
}
/** /**
* 加密 * 加密
* *

View File

@ -1,8 +1,10 @@
package com.ruoyi.framework.encrypt.encryptor; package com.ruoyi.framework.encrypt.encryptor;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil; import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4; import cn.hutool.crypto.symmetric.SM4;
import com.ruoyi.common.encrypt.EncryptContext;
import com.ruoyi.common.encrypt.IEncryptor; import com.ruoyi.common.encrypt.IEncryptor;
import com.ruoyi.common.enums.AlgorithmType; import com.ruoyi.common.enums.AlgorithmType;
import com.ruoyi.common.enums.EncodeType; import com.ruoyi.common.enums.EncodeType;
@ -19,10 +21,6 @@ public class Sm4Encryptor implements IEncryptor {
private SM4 sm4 = null; private SM4 sm4 = null;
public Sm4Encryptor(String password) {
this.sm4 = SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8));
}
/** /**
* 获得当前算法 * 获得当前算法
* *
@ -35,6 +33,27 @@ public class Sm4Encryptor implements IEncryptor {
return AlgorithmType.SM4; return AlgorithmType.SM4;
} }
/**
* 初始化加密者
*
* @param context 加密上下文
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/17 09:01
*/
@Override
public void init(EncryptContext context) throws Exception {
String password = context.getPassword();
if (StrUtil.isBlank(password)) {
throw new RuntimeException("sm4没有获得秘钥信息");
}
// sm4算法的秘钥要求是16位长度
if (16 != password.length()) {
throw new RuntimeException("sm4秘钥长度应该为16位实际为" + password.length() + "");
}
this.sm4 = SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8));
}
/** /**
* 加密 * 加密
* *