From 32d7d6ab2975f11e8d31ad4f7407a3023c7061df Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 17 Jan 2023 08:11:57 +0800 Subject: [PATCH] =?UTF-8?q?code=20review=20=E8=84=B1=E6=95=8F=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pom.xml | 5 ++++- .../core/base/DesensitizationHandlerHolder.java | 3 +++ .../core/base/annotation/Desensitize.java | 12 +++++++----- .../core/base/handler/DesensitizationHandler.java | 2 +- .../handler/AbstractRegexDesensitizationHandler.java | 2 ++ .../handler/AbstractDesensitizationHandler.java | 8 +++++--- .../serializer/StringDesensitizeSerializer.java | 7 ++++--- 7 files changed, 26 insertions(+), 13 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml index 7f15d0d4c..1afb387b8 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml @@ -9,8 +9,11 @@ ${revision} + yudao-spring-boot-starter-biz-desensitize + + 11 11 @@ -40,4 +43,4 @@ test - \ No newline at end of file + diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerHolder.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerHolder.java index 5787eae56..95c193f6b 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerHolder.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerHolder.java @@ -8,14 +8,17 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.handler.DefaultDesensi import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +// TODO @城:DesensitizationHandlerHolder =》 DesensitizationHandlerFactory? Holder 没有太多工厂的味道哈 /** * 脱敏处理器 Holder */ public class DesensitizationHandlerHolder { + /** * handler 缓存,默认初始化内置的处理器 */ private static final Map, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<>() {{ + // TODO @城:这 2 个,是不是不用呀?需要的时候初始化 put(DefaultRegexDesensitizationHandler.class, new DefaultRegexDesensitizationHandler()); put(DefaultDesensitizationHandler.class, new DefaultDesensitizationHandler()); }}; diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/Desensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/Desensitize.java index 888330ebe..b95ccd68d 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/Desensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/Desensitize.java @@ -11,19 +11,21 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; - +// TODO @城:每个接口上,author 写下哈。 +// TODO @城:Desensitize = 》DesensitizeBy +// TODO @城:Desensitize 类注释,方便读者阅读理解 /** * Desensitize 顶级脱敏注解 */ @Documented -@Target({ ElementType.ANNOTATION_TYPE}) +@Target(ElementType.ANNOTATION_TYPE) @Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside -@JsonSerialize(using = StringDesensitizeSerializer.class) +@JacksonAnnotationsInside // TODO @城:这个的作用,也可以写下 +@JsonSerialize(using = StringDesensitizeSerializer.class) // TODO @城:这个的作用,也可以写下 public @interface Desensitize { /** * 脱敏处理器 */ - Class desensitizationBy(); + Class desensitizationBy(); // TODO @城:desensitizationBy -> handler } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java index 9c10fb0ac..a4dd24665 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java @@ -11,7 +11,7 @@ public interface DesensitizationHandler { * 脱敏 * * @param origin 原始字符串 - * @param anno 注解信息 + * @param anno 注解信息 // TODO 不要这样的缩写哈,anno -> annotation * @return 脱敏后的字符串 */ String desensitize(String origin, T anno); diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java index 17c86055e..a9d495adf 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java @@ -15,6 +15,8 @@ public abstract class AbstractRegexDesensitizationHandler return origin.replaceAll(regex, replacer); } + // TODO @城:是不是抽象两个方法,一个拿 regex;一个拿 replacer + /** * 获取注解的参数 * diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java index 92b8c0d6c..0b25cdbe5 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java @@ -12,25 +12,26 @@ public abstract class AbstractDesensitizationHandler imple int prefixKeep = (Integer) args[0]; int suffixKeep = (Integer) args[1]; String replacer = (String) args[2]; - int length = origin.length(); - // 原始字符串长度小于等于保留长度,则原始字符串全部替换 + // 情况一:原始字符串长度小于等于保留长度,则原始字符串全部替换 if (prefixKeep >= length || suffixKeep >= length) { return buildReplacerByLength(replacer, length); } - // 如果原始字符串小于等于前后缀保留字符串长度,则原始字符串全部替换 + // 情况二:如果原始字符串小于等于前后缀保留字符串长度,则原始字符串全部替换 if ((prefixKeep + suffixKeep) >= length) { return buildReplacerByLength(replacer, length); } + // 情况三:TODO 城 int interval = length - prefixKeep - suffixKeep; return origin.substring(0, prefixKeep) + buildReplacerByLength(replacer, interval) + origin.substring(prefixKeep + interval); } + // TODO @城:类似,子类直接获取到参数哈 /** * 获取注解的参数 * @@ -49,4 +50,5 @@ public abstract class AbstractDesensitizationHandler imple private String buildReplacerByLength(String replacer, int length) { return String.valueOf(replacer).repeat(Math.max(0, length)); } + } 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 0fbdb04f1..accfa77dd 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 @@ -19,16 +19,19 @@ import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Field; +// TODO @城:挪到 base/serializer 包下 /** * 脱敏序列化器 */ public class StringDesensitizeSerializer extends StdSerializer implements ContextualSerializer { + private DesensitizationHandler desensitizationHandler; protected StringDesensitizeSerializer() { super(String.class); } + // TODO @城:get 和 set 方法是必须的么?如果是的话,可以换成 lombok 注解哈,简洁一点~ public DesensitizationHandler getDesensitizationHandler() { return desensitizationHandler; } @@ -55,6 +58,7 @@ public class StringDesensitizeSerializer extends StdSerializer implement return; } + // TODO @城:抽个 private getField 方法。让这个方法的逻辑主干,更清晰 String currentName = gen.getOutputContext().getCurrentName(); Object currentValue = gen.getCurrentValue(); Class currentValueClass = currentValue.getClass(); @@ -66,16 +70,13 @@ public class StringDesensitizeSerializer extends StdSerializer implement gen.writeString(value); return; } - for (Annotation annotation : field.getAnnotations()) { - if (AnnotationUtil.hasAnnotation(annotation.annotationType(), Desensitize.class)) { value = this.desensitizationHandler.desensitize(value, annotation); gen.writeString(value); return; } } - gen.writeString(value); }