From 2aa208f44139270604724f2c505198829be1e2d8 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 19 Jan 2023 10:35:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=84=B1=E6=95=8F=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/DesensitizationHandlerFactory.java | 42 ------------------- .../StringDesensitizeSerializer.java | 5 +-- .../{Email.java => EmailDesensitize.java} | 3 +- .../handler/EmailDesensitizationHandler.java | 10 ++--- .../desensitize/core/DesensitizeTest.java | 4 +- 5 files changed, 10 insertions(+), 54 deletions(-) delete mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/{Email.java => EmailDesensitize.java} (87%) diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java deleted file mode 100644 index 4c75cd957..000000000 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java +++ /dev/null @@ -1,42 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.base; - -import cn.hutool.Hutool; -import cn.hutool.core.lang.Singleton; -import cn.hutool.core.util.ReflectUtil; -import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * 脱敏处理器 Holder - * - * @author gaibu - */ -public class DesensitizationHandlerFactory { - - /** - * handler 缓存,默认初始化内置的处理器 - */ - private static final Map, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap, DesensitizationHandler>(); - - // TODO @唐:可以考虑,使用 hutool 提供的 Singleton.get() - public static DesensitizationHandler getDesensitizationHandler(Class clazz) { - DesensitizationHandler handler = HANDLER_MAP.get(clazz); - if (handler != null) { - return handler; - } - // 不存在,则进行创建 - synchronized (DesensitizationHandlerFactory.class) { - handler = HANDLER_MAP.get(clazz); - // 双重校验锁 - if (handler != null) { - return handler; - } - handler = ReflectUtil.newInstanceIfPossible(clazz); - HANDLER_MAP.put(clazz, handler); - } - return handler; - } - -} diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java index 3f85f88bd..2c15a747d 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java @@ -1,15 +1,14 @@ package cn.iocoder.yudao.framework.desensitize.core.base.serializer; import cn.hutool.core.annotation.AnnotationUtil; +import cn.hutool.core.lang.Singleton; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; -import cn.iocoder.yudao.framework.desensitize.core.base.DesensitizationHandlerFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.BeanProperty; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.ContextualSerializer; @@ -47,7 +46,7 @@ public class StringDesensitizeSerializer extends StdSerializer implement } // 创建一个 StringDesensitizeSerializer 对象,使用 DesensitizeBy 对应的处理器 StringDesensitizeSerializer serializer = new StringDesensitizeSerializer(); - serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler())); + serializer.setDesensitizationHandler(Singleton.get(annotation.handler())); return serializer; } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java similarity index 87% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java index 686367c2f..2e7ae3498 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java @@ -10,7 +10,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -// TODO @唐:名字还是 Email=》EmailDesensitize 合适一点,避免和 Validator 的注解有点冲突 /** * 邮箱脱敏注解 * @@ -21,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = EmailDesensitizationHandler.class) -public @interface Email { +public @interface EmailDesensitize { /** * 匹配的正则表达式 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java index ff77755e9..8d1867a64 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java @@ -1,21 +1,21 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler; -import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email; +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize; /** - * {@link Email} 的脱敏处理器 + * {@link EmailDesensitize} 的脱敏处理器 * * @author gaibu */ -public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { +public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { @Override - String getRegex(Email annotation) { + String getRegex(EmailDesensitize annotation) { return annotation.regex(); } @Override - String getReplacer(Email annotation) { + String getReplacer(EmailDesensitize annotation) { return annotation.replacer(); } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java index 18f4faee2..400943833 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.framework.desensitize.core; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; -import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email; +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize; import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex; import cn.iocoder.yudao.framework.desensitize.core.annotation.Address; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard; @@ -85,7 +85,7 @@ public class DesensitizeTest extends BaseMockitoUnitTest { private String slider2; @Slider(prefixKeep = 10) private String slider3; - @Email + @EmailDesensitize private String email; @Regex(regex = "芋道源码", replacer = "*") private String regex;