mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-04-09 20:13:16 +08:00
[重大更新] 重写翻译和脱敏实现 使用jackson tree解析加ResponseBodyAdvice处理数据的方案 实现可批量翻译大幅度提高效率 用法与灵活性不变
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
package org.dromara.common.sensitive.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import org.dromara.common.sensitive.core.SensitiveStrategy;
|
||||
import org.dromara.common.sensitive.handler.SensitiveHandler;
|
||||
import tools.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -17,8 +15,7 @@ import java.lang.annotation.Target;
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@JacksonAnnotationsInside
|
||||
@JsonSerialize(using = SensitiveHandler.class)
|
||||
@Documented
|
||||
public @interface Sensitive {
|
||||
|
||||
SensitiveStrategy strategy();
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.dromara.common.sensitive.config;
|
||||
|
||||
import org.dromara.common.sensitive.handler.SensitiveJsonFieldProcessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* 脱敏模块配置。
|
||||
*/
|
||||
@AutoConfiguration
|
||||
public class SensitiveConfig {
|
||||
|
||||
@Bean
|
||||
public SensitiveJsonFieldProcessor sensitiveJsonFieldProcessor() {
|
||||
return new SensitiveJsonFieldProcessor();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package org.dromara.common.sensitive.handler;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.core.utils.SpringUtils;
|
||||
import org.dromara.common.sensitive.annotation.Sensitive;
|
||||
import org.dromara.common.sensitive.core.SensitiveService;
|
||||
import org.dromara.common.sensitive.core.SensitiveStrategy;
|
||||
import org.springframework.beans.BeansException;
|
||||
import tools.jackson.core.JacksonException;
|
||||
import tools.jackson.core.JsonGenerator;
|
||||
import tools.jackson.databind.BeanProperty;
|
||||
import tools.jackson.databind.SerializationContext;
|
||||
import tools.jackson.databind.ValueSerializer;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 数据脱敏json序列化工具
|
||||
*
|
||||
* @author Yjoioooo
|
||||
*/
|
||||
@Slf4j
|
||||
public class SensitiveHandler extends ValueSerializer<String> {
|
||||
|
||||
private final SensitiveStrategy strategy;
|
||||
private final String[] roleKey;
|
||||
private final String[] perms;
|
||||
|
||||
/**
|
||||
* 提供给 jackson 创建上下文序列化器时使用 不然会报错
|
||||
*/
|
||||
public SensitiveHandler() {
|
||||
this.strategy = null;
|
||||
this.roleKey = null;
|
||||
this.perms = null;
|
||||
}
|
||||
|
||||
public SensitiveHandler(SensitiveStrategy strategy, String[] strings, String[] perms) {
|
||||
this.strategy = strategy;
|
||||
this.roleKey = strings;
|
||||
this.perms = perms;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(String value, JsonGenerator gen, SerializationContext ctxt) throws JacksonException {
|
||||
try {
|
||||
SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class);
|
||||
if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive(roleKey, perms)) {
|
||||
gen.writeString(strategy.desensitizer().apply(value));
|
||||
} else {
|
||||
gen.writeString(value);
|
||||
}
|
||||
} catch (BeansException e) {
|
||||
log.error("脱敏实现不存在, 采用默认处理 => {}", e.getMessage());
|
||||
gen.writeString(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueSerializer<?> createContextual(SerializationContext ctxt, BeanProperty property) {
|
||||
Sensitive annotation = property.getAnnotation(Sensitive.class);
|
||||
if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass())) {
|
||||
return new SensitiveHandler(annotation.strategy(), annotation.roleKey(), annotation.perms());
|
||||
}
|
||||
return super.createContextual(ctxt, property);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.dromara.common.sensitive.handler;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
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.JsonFieldContext;
|
||||
import org.dromara.common.json.enhance.JsonFieldProcessor;
|
||||
import org.dromara.common.sensitive.annotation.Sensitive;
|
||||
import org.dromara.common.sensitive.core.SensitiveService;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* 响应脱敏处理器。
|
||||
*/
|
||||
@Slf4j
|
||||
@Order(100)
|
||||
public class SensitiveJsonFieldProcessor implements JsonFieldProcessor {
|
||||
|
||||
@Override
|
||||
public Object process(JsonFieldContext fieldContext, Object value, JsonEnhancementContext context) {
|
||||
Sensitive sensitive = fieldContext.getAnnotation(Sensitive.class);
|
||||
if (sensitive == null || !(value instanceof String text)) {
|
||||
return value;
|
||||
}
|
||||
try {
|
||||
SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class);
|
||||
if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive(sensitive.roleKey(), sensitive.perms())) {
|
||||
return sensitive.strategy().desensitizer().apply(text);
|
||||
}
|
||||
return text;
|
||||
} catch (BeansException e) {
|
||||
log.error("脱敏实现不存在, 采用默认处理 => {}", e.getMessage());
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.dromara.common.sensitive.config.SensitiveConfig
|
||||
Reference in New Issue
Block a user