update 优化 encrypt 加解密模块语法 简化代码消除警告
This commit is contained in:
parent
3baad24912
commit
cb80568828
@ -7,6 +7,7 @@ import com.ruoyi.common.encrypt.properties.EncryptorProperties;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -16,6 +17,7 @@ import org.springframework.context.annotation.Bean;
|
|||||||
* @version 4.6.0
|
* @version 4.6.0
|
||||||
*/
|
*/
|
||||||
@AutoConfiguration
|
@AutoConfiguration
|
||||||
|
@EnableConfigurationProperties(EncryptorProperties.class)
|
||||||
@ConditionalOnProperty(value = "mybatis-encryptor.enable", havingValue = "true")
|
@ConditionalOnProperty(value = "mybatis-encryptor.enable", havingValue = "true")
|
||||||
public class EncryptorAutoConfiguration {
|
public class EncryptorAutoConfiguration {
|
||||||
|
|
||||||
|
@ -55,17 +55,17 @@ 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) {
|
||||||
((Map<?, Object>) sourceObject).values().forEach(this::decryptHandler);
|
map.values().forEach(this::decryptHandler);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (sourceObject instanceof List) {
|
if (sourceObject instanceof List<?> list) {
|
||||||
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率
|
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率
|
||||||
Object firstItem = ((List<?>) sourceObject).get(0);
|
Object firstItem = list.get(0);
|
||||||
if (CollectionUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
|
if (CollectionUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
((List<?>) sourceObject).forEach(this::decryptHandler);
|
list.forEach(this::decryptHandler);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass());
|
Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass());
|
||||||
|
@ -49,9 +49,8 @@ public class MybatisEncryptInterceptor implements Interceptor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object plugin(Object target) {
|
public Object plugin(Object target) {
|
||||||
if (target instanceof ParameterHandler) {
|
if (target instanceof ParameterHandler parameterHandler) {
|
||||||
// 进行加密操作
|
// 进行加密操作
|
||||||
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);
|
||||||
@ -65,19 +64,18 @@ public class MybatisEncryptInterceptor implements Interceptor {
|
|||||||
*
|
*
|
||||||
* @param sourceObject 待加密对象
|
* @param sourceObject 待加密对象
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked cast")
|
|
||||||
private void encryptHandler(Object sourceObject) {
|
private void encryptHandler(Object sourceObject) {
|
||||||
if (sourceObject instanceof Map) {
|
if (sourceObject instanceof Map<?, ?> map) {
|
||||||
((Map<?, Object>) sourceObject).values().forEach(this::encryptHandler);
|
map.values().forEach(this::encryptHandler);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (sourceObject instanceof List) {
|
if (sourceObject instanceof List<?> list) {
|
||||||
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率
|
// 判断第一个元素是否含有注解。如果没有直接返回,提高效率
|
||||||
Object firstItem = ((List<?>) sourceObject).get(0);
|
Object firstItem = list.get(0);
|
||||||
if (CollectionUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
|
if (CollectionUtil.isEmpty(encryptorManager.getFieldCache(firstItem.getClass()))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
((List<?>) sourceObject).forEach(this::encryptHandler);
|
list.forEach(this::encryptHandler);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass());
|
Set<Field> fields = encryptorManager.getFieldCache(sourceObject.getClass());
|
||||||
|
@ -4,7 +4,6 @@ import com.ruoyi.common.encrypt.enumd.AlgorithmType;
|
|||||||
import com.ruoyi.common.encrypt.enumd.EncodeType;
|
import com.ruoyi.common.encrypt.enumd.EncodeType;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加解密属性配置类
|
* 加解密属性配置类
|
||||||
@ -13,7 +12,6 @@ import org.springframework.stereotype.Component;
|
|||||||
* @version 4.6.0
|
* @version 4.6.0
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Component
|
|
||||||
@ConfigurationProperties(prefix = "mybatis-encryptor")
|
@ConfigurationProperties(prefix = "mybatis-encryptor")
|
||||||
public class EncryptorProperties {
|
public class EncryptorProperties {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user