update 优化 重构 !pr274 简化结构 解决代码逻辑问题 规范注释

This commit is contained in:
疯狂的狮子li
2023-01-18 14:11:48 +08:00
parent e20dacbfd9
commit d2675744f4
24 changed files with 339 additions and 409 deletions

View File

@ -19,7 +19,7 @@ public @interface EncryptField {
/**
* 加密算法
*/
AlgorithmType algorithm() default AlgorithmType.BASE64;
AlgorithmType algorithm() default AlgorithmType.DEFAULT;
/**
* 秘钥。AES、SM4需要
@ -39,6 +39,6 @@ public @interface EncryptField {
/**
* 编码方式。对加密算法为BASE64的不起作用
*/
EncodeType encode() default EncodeType.BASE64;
EncodeType encode() default EncodeType.DEFAULT;
}

View File

@ -1,18 +1,23 @@
package com.ruoyi.common.encrypt;
import com.ruoyi.common.enums.AlgorithmType;
import com.ruoyi.common.enums.EncodeType;
import lombok.Builder;
import lombok.Data;
/**
* 加密上下文用于encryptor传递必要的参数。
* 隔离配置和注解
* 加密上下文 用于encryptor传递必要的参数。
*
* @author 老马
* @date 2023-01-17 08:31
* @version 4.6.0
*/
@Data
public class EncryptContext {
/**
* 默认算法
*/
private AlgorithmType algorithm;
/**
* 安全秘钥
*/
@ -32,4 +37,5 @@ public class EncryptContext {
* 编码方式base64/hex
*/
private EncodeType encode;
}

View File

@ -7,16 +7,12 @@ import com.ruoyi.common.enums.EncodeType;
* 加解者
*
* @author 老马
* @date 2023-01-10 16:08
* @version 4.6.0
*/
public interface IEncryptor {
/**
* 获得当前算法
*
* @return com.ruoyi.common.enums.AlgorithmType
* @author 老马
* @date 2023/1/11 11:18
*/
AlgorithmType algorithm();
@ -25,22 +21,15 @@ public interface IEncryptor {
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String 加密后的字符串
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/10 16:38
* @return 加密后的字符串
*/
String encrypt(String value, EncodeType encodeType) throws Exception;
String encrypt(String value, EncodeType encodeType);
/**
* 解密
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String 解密后的字符串
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/10 16:38
* @return 解密后的字符串
*/
String decrypt(String value, EncodeType encodeType) throws Exception;
String decrypt(String value);
}

View File

@ -7,10 +7,12 @@ import com.ruoyi.common.encrypt.IEncryptor;
* 所有加密执行者的基类
*
* @author 老马
* @date 2023-01-17 16:52
* @version 4.6.0
*/
public abstract class AbstractEncryptor implements IEncryptor {
public AbstractEncryptor(EncryptContext context) {
//子类必须实现带参数的构造方法
// 用户配置校验与配置注入
}
}

View File

@ -1,7 +1,6 @@
package com.ruoyi.common.encrypt.encryptor;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
@ -15,32 +14,28 @@ import java.nio.charset.StandardCharsets;
* AES算法实现
*
* @author 老马
* @date 2023-01-06 11:39
* @version 4.6.0
*/
public class AesEncryptor extends AbstractEncryptor {
private AES aes = null;
private final AES aes;
public AesEncryptor(EncryptContext context) {
super(context);
String password = context.getPassword();
if (StrUtil.isBlank(password)) {
throw new RuntimeException("aes没有获得秘钥信息");
throw new IllegalArgumentException("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()+"");
if (!ArrayUtil.contains(array, password.length())) {
throw new IllegalArgumentException("AES秘钥长度应该为16位、24位、32位实际为" + password.length() + "");
}
aes = SecureUtil.aes(context.getPassword().getBytes(StandardCharsets.UTF_8));
}
/**
* 获得当前算法
*
* @return com.ruoyi.common.enums.AlgorithmType
* @author 老马
* @date 2023/1/11 11:18
*/
@Override
public AlgorithmType algorithm() {
@ -52,36 +47,23 @@ public class AesEncryptor extends AbstractEncryptor {
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String encrypt(String value, EncodeType encodeType) throws Exception {
if (ObjectUtil.isNotNull(this.aes)) {
if (encodeType == EncodeType.HEX) {
return aes.encryptHex(value);
} else {
return aes.encryptBase64(value);
}
public String encrypt(String value, EncodeType encodeType) {
if (encodeType == EncodeType.HEX) {
return aes.encryptHex(value);
} else {
return aes.encryptBase64(value);
}
return value;
}
/**
* 解密
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String decrypt(String value, EncodeType encodeType) throws Exception {
if (ObjectUtil.isNotNull(this.aes)) {
return this.aes.decryptStr(value);
}
return value;
public String decrypt(String value) {
return this.aes.decryptStr(value);
}
}

View File

@ -6,10 +6,10 @@ import com.ruoyi.common.enums.AlgorithmType;
import com.ruoyi.common.enums.EncodeType;
/**
* Base64算法实现。不建议在生产环境使用
* Base64算法实现
*
* @author 老马
* @date 2023-01-06 10:00
* @version 4.6.0
*/
public class Base64Encryptor extends AbstractEncryptor {
@ -19,10 +19,6 @@ public class Base64Encryptor extends AbstractEncryptor {
/**
* 获得当前算法
*
* @return com.ruoyi.common.enums.AlgorithmType
* @author 老马
* @date 2023/1/11 11:18
*/
@Override
public AlgorithmType algorithm() {
@ -34,12 +30,9 @@ public class Base64Encryptor extends AbstractEncryptor {
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String encrypt(String value, EncodeType encodeType) throws Exception {
public String encrypt(String value, EncodeType encodeType) {
return Base64.encode(value);
}
@ -47,13 +40,9 @@ public class Base64Encryptor extends AbstractEncryptor {
* 解密
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String decrypt(String value, EncodeType encodeType) throws Exception {
public String decrypt(String value) {
return Base64.decodeStr(value);
}
}

View File

@ -1,7 +1,6 @@
package com.ruoyi.common.encrypt.encryptor;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
@ -15,28 +14,24 @@ import com.ruoyi.common.utils.StringUtils;
* RSA算法实现
*
* @author 老马
* @date 2023-01-06 09:37
* @version 4.6.0
*/
public class RsaEncryptor extends AbstractEncryptor {
private RSA rsa = null;
private final RSA rsa;
public RsaEncryptor(EncryptContext context) {
super(context);
String privateKey = context.getPrivateKey();
String publicKey = context.getPublicKey();
if (StringUtils.isAnyEmpty(privateKey, publicKey)) {
throw new RuntimeException("rsa公私钥均需要提供,公钥加密,私钥解密。");
throw new IllegalArgumentException("RSA公私钥均需要提供,公钥加密,私钥解密。");
}
this.rsa = SecureUtil.rsa(Base64.decode(privateKey), Base64.decode(publicKey));
}
/**
* 获得当前算法
*
* @return com.ruoyi.common.enums.AlgorithmType
* @author 老马
* @date 2023/1/11 11:18
*/
@Override
public AlgorithmType algorithm() {
@ -48,36 +43,23 @@ public class RsaEncryptor extends AbstractEncryptor {
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String encrypt(String value, EncodeType encodeType) throws Exception {
if (ObjectUtil.isNotNull(this.rsa)) {
if (encodeType == EncodeType.HEX) {
return rsa.encryptHex(value, KeyType.PublicKey);
} else {
return rsa.encryptBase64(value, KeyType.PublicKey);
}
public String encrypt(String value, EncodeType encodeType) {
if (encodeType == EncodeType.HEX) {
return rsa.encryptHex(value, KeyType.PublicKey);
} else {
return rsa.encryptBase64(value, KeyType.PublicKey);
}
return value;
}
/**
* 解密
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String decrypt(String value, EncodeType encodeType) throws Exception {
if (ObjectUtil.isNotNull(this.rsa)) {
return this.rsa.decryptStr(value, KeyType.PrivateKey);
}
return value;
public String decrypt(String value) {
return this.rsa.decryptStr(value, KeyType.PrivateKey);
}
}

View File

@ -2,7 +2,6 @@ package com.ruoyi.common.encrypt.encryptor;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
@ -15,28 +14,24 @@ import com.ruoyi.common.utils.StringUtils;
* sm2算法实现
*
* @author 老马
* @date 2023-01-06 17:13
* @version 4.6.0
*/
public class Sm2Encryptor extends AbstractEncryptor {
private SM2 sm2 = null;
private final SM2 sm2;
public Sm2Encryptor(EncryptContext context) {
super(context);
String privateKey = context.getPrivateKey();
String publicKey = context.getPublicKey();
if (StringUtils.isAnyEmpty(privateKey, publicKey)) {
throw new RuntimeException("sm2公私钥均需要提供公钥加密私钥解密。");
throw new IllegalArgumentException("SM2公私钥均需要提供公钥加密私钥解密。");
}
this.sm2 = SmUtil.sm2(Base64.decode(privateKey), Base64.decode(publicKey));
}
/**
* 获得当前算法
*
* @return com.ruoyi.common.enums.AlgorithmType
* @author 老马
* @date 2023/1/11 11:18
*/
@Override
public AlgorithmType algorithm() {
@ -48,38 +43,23 @@ public class Sm2Encryptor extends AbstractEncryptor {
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String encrypt(String value, EncodeType encodeType) throws Exception {
if (ObjectUtil.isNotNull(this.sm2)) {
if (encodeType == EncodeType.HEX) {
return sm2.encryptHex(value, KeyType.PublicKey);
} else {
return sm2.encryptBase64(value, KeyType.PublicKey);
}
public String encrypt(String value, EncodeType encodeType) {
if (encodeType == EncodeType.HEX) {
return sm2.encryptHex(value, KeyType.PublicKey);
} else {
return sm2.encryptBase64(value, KeyType.PublicKey);
}
return value;
}
/**
* 解密
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String decrypt(String value, EncodeType encodeType) throws Exception {
if (ObjectUtil.isNotNull(this.sm2)) {
return this.sm2.decryptStr(value, KeyType.PrivateKey);
}
return value;
public String decrypt(String value) {
return this.sm2.decryptStr(value, KeyType.PrivateKey);
}
}

View File

@ -1,6 +1,5 @@
package com.ruoyi.common.encrypt.encryptor;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;
@ -14,31 +13,27 @@ import java.nio.charset.StandardCharsets;
* sm4算法实现
*
* @author 老马
* @date 2023-01-06 17:40
* @version 4.6.0
*/
public class Sm4Encryptor extends AbstractEncryptor {
private SM4 sm4 = null;
private final SM4 sm4;
public Sm4Encryptor(EncryptContext context) {
super(context);
String password = context.getPassword();
if (StrUtil.isBlank(password)) {
throw new RuntimeException("sm4没有获得秘钥信息");
throw new IllegalArgumentException("SM4没有获得秘钥信息");
}
// sm4算法的秘钥要求是16位长度
if (16 != password.length()) {
throw new RuntimeException("sm4秘钥长度应该为16位实际为" + password.length() + "");
throw new IllegalArgumentException("SM4秘钥长度应该为16位实际为" + password.length() + "");
}
this.sm4 = SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8));
}
/**
* 获得当前算法
*
* @return com.ruoyi.common.enums.AlgorithmType
* @author 老马
* @date 2023/1/11 11:18
*/
@Override
public AlgorithmType algorithm() {
@ -50,38 +45,23 @@ public class Sm4Encryptor extends AbstractEncryptor {
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String encrypt(String value, EncodeType encodeType) throws Exception {
if (ObjectUtil.isNotNull(this.sm4)) {
if (encodeType == EncodeType.HEX) {
return sm4.encryptHex(value);
} else {
return sm4.encryptBase64(value);
}
public String encrypt(String value, EncodeType encodeType) {
if (encodeType == EncodeType.HEX) {
return sm4.encryptHex(value);
} else {
return sm4.encryptBase64(value);
}
return value;
}
/**
* 解密
*
* @param value 待加密字符串
* @param encodeType 加密后的编码格式
* @return java.lang.String
* @throws Exception 抛出异常
* @author 老马
* @date 2023/1/10 16:38
*/
@Override
public String decrypt(String value, EncodeType encodeType) throws Exception {
if (ObjectUtil.isNotNull(this.sm4)) {
return this.sm4.decryptStr(value);
}
return value;
public String decrypt(String value) {
return this.sm4.decryptStr(value);
}
}

View File

@ -1,6 +1,5 @@
package com.ruoyi.common.enums;
import com.ruoyi.common.encrypt.IEncryptor;
import com.ruoyi.common.encrypt.encryptor.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
@ -9,10 +8,17 @@ import lombok.Getter;
* 算法名称
*
* @author 老马
* @version 4.6.0
*/
@Getter
@AllArgsConstructor
public enum AlgorithmType {
/**
* 默认走yml配置
*/
DEFAULT(null),
/**
* base64
*/
@ -38,5 +44,5 @@ public enum AlgorithmType {
*/
SM4(Sm4Encryptor.class);
private final Class<? extends IEncryptor> clazz;
private final Class<? extends AbstractEncryptor> clazz;
}

View File

@ -4,9 +4,15 @@ package com.ruoyi.common.enums;
* 编码类型
*
* @author 老马
* @date 2023-01-11 11:39
* @version 4.6.0
*/
public enum EncodeType {
/**
* 默认使用yml配置
*/
DEFAULT,
/**
* base64编码
*/