feat: 支持 vo 返回的脱敏

This commit is contained in:
gaibu 2023-01-12 19:57:11 +08:00
parent 70f15ab60d
commit 61a34ddac5
4 changed files with 44 additions and 8 deletions

View File

@ -1,16 +1,28 @@
package cn.iocoder.yudao.framework.desensitize.handler; package cn.iocoder.yudao.framework.desensitize.handler;
import java.lang.annotation.Annotation;
/** /**
* 脱敏处理器接口 * 脱敏处理器接口
*/ */
public interface DesensitizationHandler { public interface DesensitizationHandler<T extends Annotation> {
/** /**
* 脱敏 * 脱敏
* *
* @param origin 原始字符串 * @param origin 原始字符串
* @param arg 参数
* @return 脱敏后的字符串 * @return 脱敏后的字符串
*/ */
String desensitize(String origin, Object... arg); String desensitize(String origin, Object... arg);
/**
* 获取注解参数
*
* @param anno 注解
* @return 注解参数
*/
default Object[] getAnnotationArgs(T anno) {
return new Object[0];
}
} }

View File

@ -1,9 +1,11 @@
package cn.iocoder.yudao.framework.desensitize.handler; package cn.iocoder.yudao.framework.desensitize.handler;
import cn.iocoder.yudao.framework.desensitize.annotation.RegexDesensitize;
/** /**
* 正则脱敏处理器 * 正则脱敏处理器
*/ */
public class RegexDesensitizationHandler implements DesensitizationHandler { public class RegexDesensitizationHandler implements DesensitizationHandler<RegexDesensitize> {
@Override @Override
public String desensitize(String origin, Object... arg) { public String desensitize(String origin, Object... arg) {
@ -13,4 +15,9 @@ public class RegexDesensitizationHandler implements DesensitizationHandler {
return origin.replaceAll(regex, replacer); return origin.replaceAll(regex, replacer);
} }
@Override
public Object[] getAnnotationArgs(RegexDesensitize anno) {
return new Object[]{anno.regex(), anno.replacer()};
}
} }

View File

@ -1,9 +1,11 @@
package cn.iocoder.yudao.framework.desensitize.handler; package cn.iocoder.yudao.framework.desensitize.handler;
import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize;
/** /**
* 滑动脱敏处理器 * 滑动脱敏处理器
*/ */
public class SliderDesensitizationHandler implements DesensitizationHandler { public class SliderDesensitizationHandler implements DesensitizationHandler<SliderDesensitize> {
@Override @Override
public String desensitize(String origin, Object... arg) { public String desensitize(String origin, Object... arg) {
@ -29,6 +31,11 @@ public class SliderDesensitizationHandler implements DesensitizationHandler {
origin.substring(prefixKeep + interval); origin.substring(prefixKeep + interval);
} }
@Override
public Object[] getAnnotationArgs(SliderDesensitize anno) {
return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()};
}
/** /**
* 根据长度循环构建替换符 * 根据长度循环构建替换符
* *

View File

@ -18,6 +18,7 @@ import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
/** /**
@ -64,7 +65,7 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
// 滑动处理器 // 滑动处理器
SliderDesensitize sliderDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, SliderDesensitize.class)); SliderDesensitize sliderDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, SliderDesensitize.class));
if (sliderDesensitize != null) { if (sliderDesensitize != null) {
value = this.desensitizationHandler.desensitize(value, sliderDesensitize.prefixKeep(), sliderDesensitize.suffixKeep(), sliderDesensitize.replacer()); value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(sliderDesensitize));
gen.writeString(value); gen.writeString(value);
return; return;
} }
@ -72,15 +73,24 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
// 正则处理器 // 正则处理器
RegexDesensitize regexDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, RegexDesensitize.class)); RegexDesensitize regexDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, RegexDesensitize.class));
if (regexDesensitize != null) { if (regexDesensitize != null) {
value = this.desensitizationHandler.desensitize(value, regexDesensitize.regex(), regexDesensitize.replacer()); value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(regexDesensitize));
gen.writeString(value); gen.writeString(value);
return; return;
} }
// 自定义处理器 // 自定义处理器
Desensitize desensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, Desensitize.class)); Desensitize[] annotations = AnnotationUtil.getCombinationAnnotations(field, Desensitize.class);
if (desensitize != null) { if (ArrayUtil.isEmpty(annotations)) {
value = this.desensitizationHandler.desensitize(value); gen.writeString(value);
return;
}
for (Annotation annotation : field.getAnnotations()) {
if (AnnotationUtil.hasAnnotation(annotation.annotationType(), Desensitize.class)) {
value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(annotation));
gen.writeString(value);
return;
}
} }
gen.writeString(value); gen.writeString(value);