修改如下:

1.格式化代码
2.mybatis的两个拦截器类从EncryptorConfig去掉,采用注解形式注入
3.错误日志记录采用日志框架来处理异常内容
This commit is contained in:
mayuanfei 2023-01-16 16:16:15 +08:00
parent 1625e25647
commit c437089b29
13 changed files with 106 additions and 104 deletions

View File

@ -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 {

View File

@ -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.*;
/**

View File

@ -15,18 +15,22 @@ public enum AlgorithmType {
* base64
*/
BASE64("base64"),
/**
* aes
*/
AES("aes"),
/**
* rsa
*/
RSA("rsa"),
/**
* sm2
*/
SM2("sm2"),
/**
* sm4
*/

View File

@ -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();
}
}

View File

@ -84,7 +84,7 @@ public class EncryptorManager {
IEncryptor encryptor = this.registAndGetEncryptor(properties);
return encryptor.encrypt(value, properties.getEncode());
} catch (Exception e) {
log.error("字段加密异常,原样返回。异常信息:【{}】", e.getMessage());
log.error("字段加密异常,原样返回", e);
return value;
}
}
@ -103,7 +103,7 @@ public class EncryptorManager {
IEncryptor encryptor = this.registAndGetEncryptor(properties);
return encryptor.decrypt(value, properties.getEncode());
} catch (Exception e) {
log.error("字段解密异常,原样返回。异常信息:【{}】", e.getMessage());
log.error("字段解密异常,原样返回", e);
return value;
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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;
/**