mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-21 02:25:56 +08:00
修改如下:
1.格式化代码 2.mybatis的两个拦截器类从EncryptorConfig去掉,采用注解形式注入 3.错误日志记录采用日志框架来处理异常内容
This commit is contained in:
parent
1625e25647
commit
c437089b29
@ -1,5 +1,6 @@
|
||||
package com.ruoyi.test;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import com.ruoyi.demo.domain.TestDemo;
|
||||
import com.ruoyi.demo.mapper.TestDemoMapper;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
@ -15,6 +16,7 @@ import javax.annotation.Resource;
|
||||
* @date 2023-01-12 08:53
|
||||
*/
|
||||
@SpringBootTest
|
||||
@SaIgnore
|
||||
@DisplayName("加密测试")
|
||||
public class EncryptUnitTest {
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ package com.ruoyi.common.annotation;
|
||||
|
||||
import com.ruoyi.common.enums.AlgorithmType;
|
||||
import com.ruoyi.common.enums.EncodeType;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
|
||||
@ -12,35 +12,35 @@ import com.ruoyi.common.enums.EncodeType;
|
||||
public interface IEncryptor {
|
||||
|
||||
/**
|
||||
* 获得当前算法
|
||||
*
|
||||
* @return com.ruoyi.common.enums.AlgorithmType
|
||||
* @author 老马
|
||||
* @date 2023/1/11 11:18
|
||||
*/
|
||||
* 获得当前算法
|
||||
*
|
||||
* @return com.ruoyi.common.enums.AlgorithmType
|
||||
* @author 老马
|
||||
* @date 2023/1/11 11:18
|
||||
*/
|
||||
AlgorithmType algorithm();
|
||||
|
||||
/**
|
||||
* 加密
|
||||
*
|
||||
* @param value 待加密字符串
|
||||
* @param encodeType 加密后的编码格式
|
||||
* @return java.lang.String 加密后的字符串
|
||||
* @throws Exception 抛出异常
|
||||
* @author 老马
|
||||
* @date 2023/1/10 16:38
|
||||
*/
|
||||
* 加密
|
||||
*
|
||||
* @param value 待加密字符串
|
||||
* @param encodeType 加密后的编码格式
|
||||
* @return java.lang.String 加密后的字符串
|
||||
* @throws Exception 抛出异常
|
||||
* @author 老马
|
||||
* @date 2023/1/10 16:38
|
||||
*/
|
||||
String encrypt(String value, EncodeType encodeType) throws Exception;
|
||||
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
* @param value 待加密字符串
|
||||
* @param encodeType 加密后的编码格式
|
||||
* @return java.lang.String 解密后的字符串
|
||||
* @throws Exception 抛出异常
|
||||
* @author 老马
|
||||
* @date 2023/1/10 16:38
|
||||
*/
|
||||
* 解密
|
||||
*
|
||||
* @param value 待加密字符串
|
||||
* @param encodeType 加密后的编码格式
|
||||
* @return java.lang.String 解密后的字符串
|
||||
* @throws Exception 抛出异常
|
||||
* @author 老马
|
||||
* @date 2023/1/10 16:38
|
||||
*/
|
||||
String decrypt(String value, EncodeType encodeType) throws Exception;
|
||||
}
|
||||
|
||||
@ -15,18 +15,22 @@ public enum AlgorithmType {
|
||||
* base64
|
||||
*/
|
||||
BASE64("base64"),
|
||||
|
||||
/**
|
||||
* aes
|
||||
*/
|
||||
AES("aes"),
|
||||
|
||||
/**
|
||||
* rsa
|
||||
*/
|
||||
RSA("rsa"),
|
||||
|
||||
/**
|
||||
* sm2
|
||||
*/
|
||||
SM2("sm2"),
|
||||
|
||||
/**
|
||||
* sm4
|
||||
*/
|
||||
|
||||
@ -52,7 +52,7 @@ public class TestDemo extends BaseEntity {
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
@EncryptField(algorithm=AlgorithmType.AES, password = "10rfylhtccpuyke5")
|
||||
@EncryptField(algorithm = AlgorithmType.AES, password = "10rfylhtccpuyke5")
|
||||
private String value;
|
||||
|
||||
/**
|
||||
|
||||
@ -2,8 +2,6 @@ package com.ruoyi.framework.config;
|
||||
|
||||
import com.ruoyi.framework.config.properties.EncryptorProperties;
|
||||
import com.ruoyi.framework.encrypt.EncryptorManager;
|
||||
import com.ruoyi.framework.encrypt.MybatisDecryptInterceptor;
|
||||
import com.ruoyi.framework.encrypt.MybatisEncryptInterceptor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -24,16 +22,4 @@ public class EncryptorConfig {
|
||||
encryptorManager.registAndGetEncryptor(properties);
|
||||
return encryptorManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "mybatis-encryptor.enabled", havingValue = "true")
|
||||
public MybatisEncryptInterceptor mybatisEncryptInterceptor(EncryptorProperties properties) {
|
||||
return new MybatisEncryptInterceptor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "mybatis-encryptor.enabled", havingValue = "true")
|
||||
public MybatisDecryptInterceptor mybatisDecryptInterceptor(EncryptorProperties properties) {
|
||||
return new MybatisDecryptInterceptor();
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,15 +25,15 @@ public class EncryptorManager {
|
||||
|
||||
|
||||
/**
|
||||
* 注册加密执行者到缓存
|
||||
*
|
||||
* @param properties 加密执行者需要的相关配置参数
|
||||
* @author 老马
|
||||
* @date 2023/1/11 15:09
|
||||
*/
|
||||
* 注册加密执行者到缓存
|
||||
*
|
||||
* @param properties 加密执行者需要的相关配置参数
|
||||
* @author 老马
|
||||
* @date 2023/1/11 15:09
|
||||
*/
|
||||
public IEncryptor registAndGetEncryptor(EncryptorProperties properties) {
|
||||
String encryptorKey = this.getEncryptorKeyFromProperties(properties);
|
||||
if(encryptorMap.containsKey(encryptorKey)){
|
||||
if (encryptorMap.containsKey(encryptorKey)) {
|
||||
return encryptorMap.get(encryptorKey);
|
||||
}
|
||||
switch (properties.getAlgorithm()) {
|
||||
@ -60,65 +60,65 @@ public class EncryptorManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除缓存中的加密执行者
|
||||
*
|
||||
* @param properties 加密执行者需要的相关配置参数
|
||||
* @author 老马
|
||||
* @date 2023/1/11 15:55
|
||||
*/
|
||||
* 移除缓存中的加密执行者
|
||||
*
|
||||
* @param properties 加密执行者需要的相关配置参数
|
||||
* @author 老马
|
||||
* @date 2023/1/11 15:55
|
||||
*/
|
||||
public void removeEncryptor(EncryptorProperties properties) {
|
||||
this.encryptorMap.remove(getEncryptorKeyFromProperties(properties));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置进行加密。会进行本地缓存对应的算法和对应的秘钥信息。
|
||||
*
|
||||
* @param value 待加密的值
|
||||
* @param properties 加密相关的配置信息
|
||||
* @return java.lang.String 加密后的结果
|
||||
* @author 老马
|
||||
* @date 2023/1/11 16:46
|
||||
*/
|
||||
* 根据配置进行加密。会进行本地缓存对应的算法和对应的秘钥信息。
|
||||
*
|
||||
* @param value 待加密的值
|
||||
* @param properties 加密相关的配置信息
|
||||
* @return java.lang.String 加密后的结果
|
||||
* @author 老马
|
||||
* @date 2023/1/11 16:46
|
||||
*/
|
||||
public String encrypt(String value, EncryptorProperties properties) {
|
||||
try {
|
||||
IEncryptor encryptor = this.registAndGetEncryptor(properties);
|
||||
return encryptor.encrypt(value, properties.getEncode());
|
||||
}catch (Exception e) {
|
||||
log.error("字段加密异常,原样返回。异常信息:【{}】", e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("字段加密异常,原样返回", e);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置进行解密
|
||||
*
|
||||
* @param value 待解密的值
|
||||
* @param properties 加密相关的配置信息
|
||||
* @return java.lang.String
|
||||
* @author 老马
|
||||
* @date 2023/1/11 17:43
|
||||
*/
|
||||
* 根据配置进行解密
|
||||
*
|
||||
* @param value 待解密的值
|
||||
* @param properties 加密相关的配置信息
|
||||
* @return java.lang.String
|
||||
* @author 老马
|
||||
* @date 2023/1/11 17:43
|
||||
*/
|
||||
public String decrypt(String value, EncryptorProperties properties) {
|
||||
try{
|
||||
try {
|
||||
IEncryptor encryptor = this.registAndGetEncryptor(properties);
|
||||
return encryptor.decrypt(value, properties.getEncode());
|
||||
}catch (Exception e) {
|
||||
log.error("字段解密异常,原样返回。异常信息:【{}】", e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("字段解密异常,原样返回", e);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从配置内容中提取缓存的KEY
|
||||
*
|
||||
* @param properties 加密相关的配置信息
|
||||
* @return java.lang.String
|
||||
* @author 老马
|
||||
* @date 2023/1/11 17:39
|
||||
*/
|
||||
* 从配置内容中提取缓存的KEY
|
||||
*
|
||||
* @param properties 加密相关的配置信息
|
||||
* @return java.lang.String
|
||||
* @author 老马
|
||||
* @date 2023/1/11 17:39
|
||||
*/
|
||||
private String getEncryptorKeyFromProperties(EncryptorProperties properties) {
|
||||
return properties.getAlgorithm().getAlgorithm()+ StringUtils.defaultString(properties.getPassword()) +
|
||||
return properties.getAlgorithm().getAlgorithm() + StringUtils.defaultString(properties.getPassword()) +
|
||||
StringUtils.defaultString(properties.getPublicKey()) + StringUtils.defaultString(properties.getPrivateKey());
|
||||
}
|
||||
|
||||
|
||||
@ -9,6 +9,8 @@ import com.ruoyi.framework.config.properties.EncryptorProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.executor.resultset.ResultSetHandler;
|
||||
import org.apache.ibatis.plugin.*;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.Statement;
|
||||
@ -26,6 +28,8 @@ import java.util.*;
|
||||
method = "handleResultSets",
|
||||
args = {Statement.class})
|
||||
})
|
||||
@ConditionalOnProperty(value = "mybatis-encryptor.enabled", havingValue = "true")
|
||||
@Component
|
||||
public class MybatisDecryptInterceptor implements Interceptor {
|
||||
|
||||
private final EncryptorManager encryptorManager = SpringUtils.getBean(EncryptorManager.class);
|
||||
@ -47,7 +51,7 @@ public class MybatisDecryptInterceptor implements Interceptor {
|
||||
*
|
||||
* @param sourceObject 待加密对象
|
||||
*/
|
||||
private void decryptHandler (Object sourceObject) {
|
||||
private void decryptHandler(Object sourceObject) {
|
||||
if (sourceObject instanceof Map) {
|
||||
((Map<?, Object>) sourceObject).values().forEach(this::decryptHandler);
|
||||
return;
|
||||
@ -55,7 +59,7 @@ public class MybatisDecryptInterceptor implements Interceptor {
|
||||
if (sourceObject instanceof List) {
|
||||
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率
|
||||
Object firstItem = ((List<?>) sourceObject).get(0);
|
||||
if(CollectionUtil.isEmpty(EncryptedFieldsCacheHelper.get(firstItem.getClass()))) {
|
||||
if (CollectionUtil.isEmpty(EncryptedFieldsCacheHelper.get(firstItem.getClass()))) {
|
||||
return;
|
||||
}
|
||||
((List<?>) sourceObject).forEach(this::decryptHandler);
|
||||
@ -84,9 +88,9 @@ public class MybatisDecryptInterceptor implements Interceptor {
|
||||
properties.setEnabled(true);
|
||||
properties.setAlgorithm(encryptField.algorithm());
|
||||
properties.setEncode(encryptField.encode());
|
||||
properties.setPassword(StringUtils.isEmpty(encryptField.password())?defaultProperties.getPassword():encryptField.password());
|
||||
properties.setPrivateKey(StringUtils.isEmpty(encryptField.privateKey())?defaultProperties.getPrivateKey():encryptField.privateKey());
|
||||
properties.setPublicKey(StringUtils.isEmpty(encryptField.publicKey())?defaultProperties.getPublicKey():encryptField.publicKey());
|
||||
properties.setPassword(StringUtils.isEmpty(encryptField.password()) ? defaultProperties.getPassword() : encryptField.password());
|
||||
properties.setPrivateKey(StringUtils.isEmpty(encryptField.privateKey()) ? defaultProperties.getPrivateKey() : encryptField.privateKey());
|
||||
properties.setPublicKey(StringUtils.isEmpty(encryptField.publicKey()) ? defaultProperties.getPublicKey() : encryptField.publicKey());
|
||||
this.encryptorManager.registAndGetEncryptor(properties);
|
||||
return this.encryptorManager.decrypt(value, properties);
|
||||
}
|
||||
|
||||
@ -13,6 +13,8 @@ import org.apache.ibatis.plugin.Interceptor;
|
||||
import org.apache.ibatis.plugin.Intercepts;
|
||||
import org.apache.ibatis.plugin.Invocation;
|
||||
import org.apache.ibatis.plugin.Signature;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.PreparedStatement;
|
||||
@ -33,6 +35,8 @@ import java.util.Set;
|
||||
method = "setParameters",
|
||||
args = {PreparedStatement.class})
|
||||
})
|
||||
@ConditionalOnProperty(value = "mybatis-encryptor.enabled", havingValue = "true")
|
||||
@Component
|
||||
public class MybatisEncryptInterceptor implements Interceptor {
|
||||
|
||||
private final EncryptorManager encryptorManager = SpringUtils.getBean(EncryptorManager.class);
|
||||
@ -49,7 +53,7 @@ public class MybatisEncryptInterceptor implements Interceptor {
|
||||
// 进行加密操作
|
||||
ParameterHandler parameterHandler = (ParameterHandler) target;
|
||||
Object parameterObject = parameterHandler.getParameterObject();
|
||||
if(ObjectUtil.isNotNull(parameterObject) && !(parameterObject instanceof String)){
|
||||
if (ObjectUtil.isNotNull(parameterObject) && !(parameterObject instanceof String)) {
|
||||
this.encryptHandler(parameterObject);
|
||||
}
|
||||
}
|
||||
@ -61,7 +65,7 @@ public class MybatisEncryptInterceptor implements Interceptor {
|
||||
*
|
||||
* @param sourceObject 待加密对象
|
||||
*/
|
||||
private void encryptHandler (Object sourceObject) {
|
||||
private void encryptHandler(Object sourceObject) {
|
||||
if (sourceObject instanceof Map) {
|
||||
((Map<?, Object>) sourceObject).values().forEach(this::encryptHandler);
|
||||
return;
|
||||
@ -69,7 +73,7 @@ public class MybatisEncryptInterceptor implements Interceptor {
|
||||
if (sourceObject instanceof List) {
|
||||
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率
|
||||
Object firstItem = ((List<?>) sourceObject).get(0);
|
||||
if(CollectionUtil.isEmpty(EncryptedFieldsCacheHelper.get(firstItem.getClass()))) {
|
||||
if (CollectionUtil.isEmpty(EncryptedFieldsCacheHelper.get(firstItem.getClass()))) {
|
||||
return;
|
||||
}
|
||||
((List<?>) sourceObject).forEach(this::encryptHandler);
|
||||
@ -98,9 +102,9 @@ public class MybatisEncryptInterceptor implements Interceptor {
|
||||
properties.setEnabled(true);
|
||||
properties.setAlgorithm(encryptField.algorithm());
|
||||
properties.setEncode(encryptField.encode());
|
||||
properties.setPassword(StringUtils.isEmpty(encryptField.password())?defaultProperties.getPassword():encryptField.password());
|
||||
properties.setPrivateKey(StringUtils.isEmpty(encryptField.privateKey())?defaultProperties.getPrivateKey():encryptField.privateKey());
|
||||
properties.setPublicKey(StringUtils.isEmpty(encryptField.publicKey())?defaultProperties.getPublicKey():encryptField.publicKey());
|
||||
properties.setPassword(StringUtils.isEmpty(encryptField.password()) ? defaultProperties.getPassword() : encryptField.password());
|
||||
properties.setPrivateKey(StringUtils.isEmpty(encryptField.privateKey()) ? defaultProperties.getPrivateKey() : encryptField.privateKey());
|
||||
properties.setPublicKey(StringUtils.isEmpty(encryptField.publicKey()) ? defaultProperties.getPublicKey() : encryptField.publicKey());
|
||||
this.encryptorManager.registAndGetEncryptor(properties);
|
||||
return this.encryptorManager.encrypt(value, properties);
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import cn.hutool.crypto.symmetric.AES;
|
||||
import com.ruoyi.common.encrypt.IEncryptor;
|
||||
import com.ruoyi.common.enums.AlgorithmType;
|
||||
import com.ruoyi.common.enums.EncodeType;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
@ -45,14 +46,14 @@ public class AesEncryptor implements IEncryptor {
|
||||
*/
|
||||
@Override
|
||||
public String encrypt(String value, EncodeType encodeType) throws Exception {
|
||||
if(ObjectUtil.isNotNull(this.aes)) {
|
||||
if(encodeType == EncodeType.HEX) {
|
||||
if (ObjectUtil.isNotNull(this.aes)) {
|
||||
if (encodeType == EncodeType.HEX) {
|
||||
return aes.encryptHex(value);
|
||||
}else {
|
||||
} else {
|
||||
return aes.encryptBase64(value);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,8 +66,8 @@ public class AesEncryptor implements IEncryptor {
|
||||
* @date 2023/1/10 16:38
|
||||
*/
|
||||
@Override
|
||||
public String decrypt(String value,EncodeType encodeType) throws Exception {
|
||||
if(ObjectUtil.isNotNull(this.aes)) {
|
||||
public String decrypt(String value, EncodeType encodeType) throws Exception {
|
||||
if (ObjectUtil.isNotNull(this.aes)) {
|
||||
return this.aes.decryptStr(value);
|
||||
}
|
||||
return value;
|
||||
|
||||
@ -47,7 +47,7 @@ public class RsaEncryptor implements IEncryptor {
|
||||
*/
|
||||
@Override
|
||||
public String encrypt(String value, EncodeType encodeType) throws Exception {
|
||||
if(ObjectUtil.isNotNull(this.rsa)) {
|
||||
if (ObjectUtil.isNotNull(this.rsa)) {
|
||||
if (encodeType == EncodeType.HEX) {
|
||||
return rsa.encryptHex(value, KeyType.PublicKey);
|
||||
} else {
|
||||
@ -68,7 +68,7 @@ public class RsaEncryptor implements IEncryptor {
|
||||
*/
|
||||
@Override
|
||||
public String decrypt(String value, EncodeType encodeType) throws Exception {
|
||||
if(ObjectUtil.isNotNull(this.rsa)) {
|
||||
if (ObjectUtil.isNotNull(this.rsa)) {
|
||||
return this.rsa.decryptStr(value, KeyType.PrivateKey);
|
||||
}
|
||||
return value;
|
||||
|
||||
@ -48,7 +48,7 @@ public class Sm2Encryptor implements IEncryptor {
|
||||
*/
|
||||
@Override
|
||||
public String encrypt(String value, EncodeType encodeType) throws Exception {
|
||||
if(ObjectUtil.isNotNull(this.sm2)) {
|
||||
if (ObjectUtil.isNotNull(this.sm2)) {
|
||||
if (encodeType == EncodeType.HEX) {
|
||||
return sm2.encryptHex(value, KeyType.PublicKey);
|
||||
} else {
|
||||
@ -70,7 +70,7 @@ public class Sm2Encryptor implements IEncryptor {
|
||||
*/
|
||||
@Override
|
||||
public String decrypt(String value, EncodeType encodeType) throws Exception {
|
||||
if(ObjectUtil.isNotNull(this.sm2)) {
|
||||
if (ObjectUtil.isNotNull(this.sm2)) {
|
||||
return this.sm2.decryptStr(value, KeyType.PrivateKey);
|
||||
}
|
||||
return value;
|
||||
|
||||
@ -47,7 +47,7 @@ public class Sm4Encryptor implements IEncryptor {
|
||||
*/
|
||||
@Override
|
||||
public String encrypt(String value, EncodeType encodeType) throws Exception {
|
||||
if(ObjectUtil.isNotNull(this.sm4)) {
|
||||
if (ObjectUtil.isNotNull(this.sm4)) {
|
||||
if (encodeType == EncodeType.HEX) {
|
||||
return sm4.encryptHex(value);
|
||||
} else {
|
||||
@ -69,7 +69,7 @@ public class Sm4Encryptor implements IEncryptor {
|
||||
*/
|
||||
@Override
|
||||
public String decrypt(String value, EncodeType encodeType) throws Exception {
|
||||
if(ObjectUtil.isNotNull(this.sm4)) {
|
||||
if (ObjectUtil.isNotNull(this.sm4)) {
|
||||
return this.sm4.decryptStr(value);
|
||||
}
|
||||
return value;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user