修改如下:

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

View File

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

View File

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

View File

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

View File

@ -84,7 +84,7 @@ public class EncryptorManager {
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;
} }
} }
@ -103,7 +103,7 @@ public class EncryptorManager {
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;
} }
} }

View File

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

View File

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

View File

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