From 61a34ddac5478887621d05cd7cf9c6019a62f27b Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 12 Jan 2023 19:57:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20vo=20=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=9A=84=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/DesensitizationHandler.java | 14 ++++++++++++- .../handler/RegexDesensitizationHandler.java | 9 ++++++++- .../handler/SliderDesensitizationHandler.java | 9 ++++++++- .../StringDesensitizeSerializer.java | 20 ++++++++++++++----- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java index ba5a63bfb..74870e771 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java @@ -1,16 +1,28 @@ package cn.iocoder.yudao.framework.desensitize.handler; +import java.lang.annotation.Annotation; + /** * 脱敏处理器接口 */ -public interface DesensitizationHandler { +public interface DesensitizationHandler { /** * 脱敏 * * @param origin 原始字符串 + * @param arg 参数 * @return 脱敏后的字符串 */ String desensitize(String origin, Object... arg); + /** + * 获取注解参数 + * + * @param anno 注解 + * @return 注解参数 + */ + default Object[] getAnnotationArgs(T anno) { + return new Object[0]; + } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java index 63089054f..57fc71178 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java @@ -1,9 +1,11 @@ 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 { @Override public String desensitize(String origin, Object... arg) { @@ -13,4 +15,9 @@ public class RegexDesensitizationHandler implements DesensitizationHandler { return origin.replaceAll(regex, replacer); } + @Override + public Object[] getAnnotationArgs(RegexDesensitize anno) { + return new Object[]{anno.regex(), anno.replacer()}; + } + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java index 2d8336d1f..47887da39 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java @@ -1,9 +1,11 @@ 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 { @Override public String desensitize(String origin, Object... arg) { @@ -29,6 +31,11 @@ public class SliderDesensitizationHandler implements DesensitizationHandler { origin.substring(prefixKeep + interval); } + @Override + public Object[] getAnnotationArgs(SliderDesensitize anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } + /** * 根据长度循环构建替换符 * diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/serializer/StringDesensitizeSerializer.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/serializer/StringDesensitizeSerializer.java index f90475d6d..956be23f3 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/serializer/StringDesensitizeSerializer.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/serializer/StringDesensitizeSerializer.java @@ -18,6 +18,7 @@ import com.fasterxml.jackson.databind.ser.ContextualSerializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import java.io.IOException; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; /** @@ -64,7 +65,7 @@ public class StringDesensitizeSerializer extends StdSerializer implement // 滑动处理器 SliderDesensitize sliderDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, SliderDesensitize.class)); 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); return; } @@ -72,15 +73,24 @@ public class StringDesensitizeSerializer extends StdSerializer implement // 正则处理器 RegexDesensitize regexDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, RegexDesensitize.class)); if (regexDesensitize != null) { - value = this.desensitizationHandler.desensitize(value, regexDesensitize.regex(), regexDesensitize.replacer()); + value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(regexDesensitize)); gen.writeString(value); return; } // 自定义处理器 - Desensitize desensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, Desensitize.class)); - if (desensitize != null) { - value = this.desensitizationHandler.desensitize(value); + Desensitize[] annotations = AnnotationUtil.getCombinationAnnotations(field, Desensitize.class); + if (ArrayUtil.isEmpty(annotations)) { + 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);