mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-04-08 11:33:15 +08:00
update 优化 翻译处理器 避免多次序列化 实现类增加缓存避免重复解析
This commit is contained in:
@@ -129,7 +129,7 @@ public class JsonValueEnhancer {
|
|||||||
return jsonMapper.valueToTree(value);
|
return jsonMapper.valueToTree(value);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
ObjectNode objectNode = asObjectNode(jsonMapper.valueToTree(value));
|
ObjectNode objectNode = jsonMapper.createObjectNode();
|
||||||
for (PropertyMetadata metadata : getProperties(value.getClass())) {
|
for (PropertyMetadata metadata : getProperties(value.getClass())) {
|
||||||
Object originalValue = metadata.getValue(value);
|
Object originalValue = metadata.getValue(value);
|
||||||
JsonFieldContext fieldContext = new JsonFieldContext(value, metadata.propertyName(), metadata.member(), originalValue);
|
JsonFieldContext fieldContext = new JsonFieldContext(value, metadata.propertyName(), metadata.member(), originalValue);
|
||||||
@@ -158,13 +158,6 @@ public class JsonValueEnhancer {
|
|||||||
return enhanceTree(value);
|
return enhanceTree(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ObjectNode asObjectNode(JsonNode node) {
|
|
||||||
if (node instanceof ObjectNode objectNode) {
|
|
||||||
return objectNode;
|
|
||||||
}
|
|
||||||
return jsonMapper.createObjectNode();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<PropertyMetadata> getProperties(Class<?> type) {
|
private List<PropertyMetadata> getProperties(Class<?> type) {
|
||||||
return propertyCache.computeIfAbsent(type, this::resolveProperties);
|
return propertyCache.computeIfAbsent(type, this::resolveProperties);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,12 @@ package org.dromara.common.sensitive.handler;
|
|||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.common.core.utils.SpringUtils;
|
|
||||||
import org.dromara.common.json.enhance.JsonEnhancementContext;
|
import org.dromara.common.json.enhance.JsonEnhancementContext;
|
||||||
import org.dromara.common.json.enhance.JsonFieldContext;
|
import org.dromara.common.json.enhance.JsonFieldContext;
|
||||||
import org.dromara.common.json.enhance.JsonFieldProcessor;
|
import org.dromara.common.json.enhance.JsonFieldProcessor;
|
||||||
import org.dromara.common.sensitive.annotation.Sensitive;
|
import org.dromara.common.sensitive.annotation.Sensitive;
|
||||||
import org.dromara.common.sensitive.core.SensitiveService;
|
import org.dromara.common.sensitive.core.SensitiveService;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,22 +17,19 @@ import org.springframework.core.annotation.Order;
|
|||||||
@Order(100)
|
@Order(100)
|
||||||
public class SensitiveJsonFieldProcessor implements JsonFieldProcessor {
|
public class SensitiveJsonFieldProcessor implements JsonFieldProcessor {
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private SensitiveService sensitiveService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object process(JsonFieldContext fieldContext, Object value, JsonEnhancementContext context) {
|
public Object process(JsonFieldContext fieldContext, Object value, JsonEnhancementContext context) {
|
||||||
Sensitive sensitive = fieldContext.getAnnotation(Sensitive.class);
|
Sensitive sensitive = fieldContext.getAnnotation(Sensitive.class);
|
||||||
if (sensitive == null || !(value instanceof String text)) {
|
if (sensitive == null || !(value instanceof String text)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class);
|
|
||||||
if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive(sensitive.roleKey(), sensitive.perms())) {
|
if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive(sensitive.roleKey(), sensitive.perms())) {
|
||||||
return sensitive.strategy().desensitizer().apply(text);
|
return sensitive.strategy().desensitizer().apply(text);
|
||||||
}
|
}
|
||||||
return text;
|
return text;
|
||||||
} catch (BeansException e) {
|
|
||||||
log.error("脱敏实现不存在, 采用默认处理 => {}", e.getMessage());
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.dromara.common.translation.core.handler;
|
package org.dromara.common.translation.core.handler;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
import org.dromara.common.core.utils.reflect.ReflectUtils;
|
import org.dromara.common.core.utils.reflect.ReflectUtils;
|
||||||
@@ -13,11 +12,11 @@ import org.dromara.common.translation.annotation.TranslationType;
|
|||||||
import org.dromara.common.translation.core.TranslationInterface;
|
import org.dromara.common.translation.core.TranslationInterface;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,14 +24,24 @@ import java.util.Set;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Order(0)
|
@Order(0)
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||||
|
|
||||||
private static final String ATTR_BATCHES = TranslationJsonFieldProcessor.class.getName() + ".batches";
|
private static final String ATTR_BATCHES = TranslationJsonFieldProcessor.class.getName() + ".batches";
|
||||||
|
|
||||||
private static final String ATTR_RESULTS = TranslationJsonFieldProcessor.class.getName() + ".results";
|
private static final String ATTR_RESULTS = TranslationJsonFieldProcessor.class.getName() + ".results";
|
||||||
|
|
||||||
private final List<TranslationInterface<?>> translations;
|
private final Map<String, TranslationInterface<?>> translationMap;
|
||||||
|
|
||||||
|
public TranslationJsonFieldProcessor(List<TranslationInterface<?>> translations) {
|
||||||
|
Map<String, TranslationInterface<?>> map = new LinkedHashMap<>(translations.size());
|
||||||
|
for (TranslationInterface<?> t : translations) {
|
||||||
|
TranslationType annotation = t.getClass().getAnnotation(TranslationType.class);
|
||||||
|
if (annotation != null) {
|
||||||
|
map.put(annotation.type(), t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.translationMap = Collections.unmodifiableMap(map);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void collect(JsonFieldContext fieldContext, JsonEnhancementContext context) {
|
public void collect(JsonFieldContext fieldContext, JsonEnhancementContext context) {
|
||||||
@@ -118,13 +127,7 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private TranslationInterface<?> getTranslation(String type) {
|
private TranslationInterface<?> getTranslation(String type) {
|
||||||
for (TranslationInterface<?> translation : translations) {
|
return translationMap.get(type);
|
||||||
TranslationType translationType = translation.getClass().getAnnotation(TranslationType.class);
|
|
||||||
if (translationType != null && Objects.equals(type, translationType.type())) {
|
|
||||||
return translation;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private record TranslationBatchKey(String type, String other) {
|
private record TranslationBatchKey(String type, String other) {
|
||||||
|
|||||||
Reference in New Issue
Block a user