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