From cd9bcda9350a28d6547af3a391fdea757ad8b8ea Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 12 Jan 2023 15:31:44 +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 --- yudao-dependencies/pom.xml | 5 ++++ .../StringDesensitizeSerializer.java | 29 ++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index 7b056c46b..be2f14dd6 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -141,6 +141,11 @@ yudao-spring-boot-starter-captcha ${revision} + + cn.iocoder.boot + yudao-spring-boot-starter-biz-desensitize + ${revision} + 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 a43b3e28e..f90475d6d 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 @@ -1,5 +1,7 @@ package cn.iocoder.yudao.framework.desensitize.serializer; +import cn.hutool.core.annotation.AnnotationUtil; +import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.desensitize.annotation.Desensitize; @@ -22,13 +24,19 @@ import java.lang.reflect.Field; * 脱敏序列化器 */ public class StringDesensitizeSerializer extends StdSerializer implements ContextualSerializer { - private final DesensitizationHandler desensitizationHandler; + private DesensitizationHandler desensitizationHandler; - protected StringDesensitizeSerializer(DesensitizationHandler desensitizationHandler) { + protected StringDesensitizeSerializer() { super(String.class); - this.desensitizationHandler = desensitizationHandler; } + public DesensitizationHandler getDesensitizationHandler() { + return desensitizationHandler; + } + + public void setDesensitizationHandler(DesensitizationHandler desensitizationHandler) { + this.desensitizationHandler = desensitizationHandler; + } @Override public JsonSerializer createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { @@ -36,8 +44,9 @@ public class StringDesensitizeSerializer extends StdSerializer implement if (annotation == null) { return this; } - - return new StringDesensitizeSerializer(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationHandler())); + StringDesensitizeSerializer serializer = new StringDesensitizeSerializer(); + serializer.setDesensitizationHandler(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationHandler())); + return serializer; } @Override @@ -53,19 +62,23 @@ public class StringDesensitizeSerializer extends StdSerializer implement Field field = ReflectUtil.getField(currentValueClass, currentName); // 滑动处理器 - SliderDesensitize sliderDesensitize = field.getAnnotation(SliderDesensitize.class); + SliderDesensitize sliderDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, SliderDesensitize.class)); if (sliderDesensitize != null) { value = this.desensitizationHandler.desensitize(value, sliderDesensitize.prefixKeep(), sliderDesensitize.suffixKeep(), sliderDesensitize.replacer()); + gen.writeString(value); + return; } // 正则处理器 - RegexDesensitize regexDesensitize = field.getAnnotation(RegexDesensitize.class); + RegexDesensitize regexDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, RegexDesensitize.class)); if (regexDesensitize != null) { value = this.desensitizationHandler.desensitize(value, regexDesensitize.regex(), regexDesensitize.replacer()); + gen.writeString(value); + return; } // 自定义处理器 - Desensitize desensitize = field.getAnnotation(Desensitize.class); + Desensitize desensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, Desensitize.class)); if (desensitize != null) { value = this.desensitizationHandler.desensitize(value); }