diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml b/yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml index 53879bd0f2..c4a96ebe1e 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml @@ -10,7 +10,7 @@ yudao-spring-boot-starter-desensitize - 脱敏组件 + 脱敏组件:支持 JSON 返回数据时,将邮箱、手机等字段进行脱敏 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 index abd756ef23..4c75cd9576 100644 --- 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 @@ -1,5 +1,7 @@ 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; @@ -18,11 +20,13 @@ public class DesensitizationHandlerFactory { */ 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); // 双重校验锁 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/DesensitizeBy.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/DesensitizeBy.java index d06e633a6a..0166cb6ef2 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/DesensitizeBy.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/DesensitizeBy.java @@ -12,7 +12,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * 顶级脱敏注解,自定义注解需要使用此注解。 + * 顶级脱敏注解,自定义注解需要使用此注解 * * @author gaibu */ @@ -26,5 +26,7 @@ public @interface DesensitizeBy { /** * 脱敏处理器 */ + @SuppressWarnings("rawtypes") Class handler(); + } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java index e59d96cec6..470a0becf5 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java @@ -4,6 +4,8 @@ import java.lang.annotation.Annotation; /** * 脱敏处理器接口 + * + * @author gaibu */ public interface DesensitizationHandler { 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 85dee2f53f..3f85f88bd4 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 @@ -24,8 +24,11 @@ import java.lang.reflect.Field; /** * 脱敏序列化器 * + * 实现 JSON 返回数据时,使用 {@link DesensitizationHandler} 对声明脱敏注解的字段,进行脱敏处理。 + * * @author gaibu */ +@SuppressWarnings("rawtypes") public class StringDesensitizeSerializer extends StdSerializer implements ContextualSerializer { @Getter @@ -37,17 +40,19 @@ public class StringDesensitizeSerializer extends StdSerializer implement } @Override - public JsonSerializer createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { + public JsonSerializer createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) { DesensitizeBy annotation = beanProperty.getAnnotation(DesensitizeBy.class); if (annotation == null) { return this; } + // 创建一个 StringDesensitizeSerializer 对象,使用 DesensitizeBy 对应的处理器 StringDesensitizeSerializer serializer = new StringDesensitizeSerializer(); serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler())); return serializer; } @Override + @SuppressWarnings("unchecked") public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException { if (StrUtil.isBlank(value)) { gen.writeNull(); @@ -75,12 +80,12 @@ public class StringDesensitizeSerializer extends StdSerializer implement /** * 获取字段 * - * @param gen JsonGenerator + * @param generator JsonGenerator * @return 字段 */ - private Field getField(JsonGenerator gen) { - String currentName = gen.getOutputContext().getCurrentName(); - Object currentValue = gen.getCurrentValue(); + private Field getField(JsonGenerator generator) { + String currentName = generator.getOutputContext().getCurrentName(); + Object currentValue = generator.getCurrentValue(); Class currentValueClass = currentValue.getClass(); return ReflectUtil.getField(currentValueClass, currentName); } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/package-info.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/package-info.java new file mode 100644 index 0000000000..d56282535e --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/package-info.java @@ -0,0 +1,4 @@ +/** + * 脱敏组件:支持 JSON 返回数据时,将邮箱、手机等字段进行脱敏 + */ +package cn.iocoder.yudao.framework.desensitize.core; 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/Email.java index 02d7043fe5..686367c2f7 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/Email.java @@ -10,8 +10,9 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +// TODO @唐:名字还是 Email=》EmailDesensitize 合适一点,避免和 Validator 的注解有点冲突 /** - * 邮箱 + * 邮箱脱敏注解 * * @author gaibu */ @@ -28,7 +29,9 @@ public @interface Email { String regex() default "(^.)[^@]*(@.*$)"; /** - * 替换规则,邮箱;比如:example@gmail.com脱敏之后为e****@gmail.com + * 替换规则,邮箱; + * + * 比如:example@gmail.com 脱敏之后 为e****@gmail.com */ String replacer() default "$1****$2"; } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java index e5d2558fa2..d0fddea6a4 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java @@ -29,6 +29,7 @@ public @interface Regex { /** * 替换规则,会将匹配到的字符串全部替换成 replacer + * * 例如:regex=123; replacer=****** * 原始字符串 123456789 * 脱敏后字符串 ******456789 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java index 158c6de3de..f43431b1d0 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java @@ -9,13 +9,13 @@ import java.lang.annotation.Annotation; * * @author gaibu */ -public abstract class AbstractRegexDesensitizationHandler implements DesensitizationHandler { +public abstract class AbstractRegexDesensitizationHandler + implements DesensitizationHandler { @Override public String desensitize(String origin, T annotation) { String regex = getRegex(annotation); String replacer = getReplacer(annotation); - return origin.replaceAll(regex, replacer); } @@ -34,4 +34,5 @@ public abstract class AbstractRegexDesensitizationHandler * @return 待替换的字符串 */ abstract String getReplacer(T annotation); + } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java index 9fa5d80d77..718380570c 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java @@ -3,13 +3,12 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler; import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex; /** - * 默认正则脱敏处理器 + * {@link Regex} 的正则脱敏处理器 * * @author gaibu */ public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler { - @Override String getRegex(Regex annotation) { return annotation.regex(); 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 b6692d448b..ff77755e9a 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 @@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler; import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email; /** - * 邮箱脱敏处理器 + * {@link Email} 的脱敏处理器 * * @author gaibu */ @@ -18,4 +18,5 @@ public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHan String getReplacer(Email annotation) { return annotation.replacer(); } + } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Mobile.java similarity index 88% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Mobile.java index 87d7893fe9..ca2f4f041d 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Mobile.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; -import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PhoneNumberDesensitization; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.MobileDesensitization; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import java.lang.annotation.Documented; @@ -19,8 +19,8 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@DesensitizeBy(handler = PhoneNumberDesensitization.class) -public @interface PhoneNumber { +@DesensitizeBy(handler = MobileDesensitization.class) +public @interface Mobile { /** * 前缀保留长度 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java index bbe067ab79..f96e13b21e 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java @@ -33,7 +33,9 @@ public @interface Password { int suffixKeep() default 0; /** - * 替换规则,密码;比如:123456脱敏之后为****** + * 替换规则,密码; + * + * 比如:123456脱敏之后为****** */ String replacer() default "*"; diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java index bd5ca926da..e41c57319a 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java @@ -9,7 +9,8 @@ import java.lang.annotation.Annotation; * * @author gaibu */ -public abstract class AbstractDesensitizationHandler implements DesensitizationHandler { +public abstract class AbstractDesensitizationHandler + implements DesensitizationHandler { @Override public String desensitize(String origin, T annotation) { @@ -35,6 +36,21 @@ public abstract class AbstractDesensitizationHandler imple origin.substring(prefixKeep + interval); } + /** + * 根据长度循环构建替换符 + * + * @param replacer 替换符 + * @param length 长度 + * @return 构建后的替换符 + */ + private String buildReplacerByLength(String replacer, int length) { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < length; i++) { + builder.append(replacer); + } + return builder.toString(); + } + /** * 前缀保留长度 * @@ -59,19 +75,4 @@ public abstract class AbstractDesensitizationHandler imple */ abstract String getReplacer(T annotation); - /** - * 根据长度循环构建替换符 - * - * @param replacer 替换符 - * @param length 长度 - * @return 构建后的替换符 - */ - private String buildReplacerByLength(String replacer, int length) { - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < length; i++) { - builder.append(replacer); - } - return builder.toString(); - } - } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java index 76e63a6b11..ca2e00c899 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java @@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard; /** - * 银行卡脱敏处理器 + * {@link BankCard} 的脱敏处理器 * * @author gaibu */ @@ -24,4 +24,4 @@ public class BankCardDesensitization extends AbstractDesensitizationHandler { + @Override Integer getPrefixKeep(ChineseName annotation) { return annotation.prefixKeep(); @@ -22,4 +23,5 @@ public class ChineseNameDesensitization extends AbstractDesensitizationHandler { +public class MobileDesensitization extends AbstractDesensitizationHandler { @Override - Integer getPrefixKeep(PhoneNumber annotation) { + Integer getPrefixKeep(Mobile annotation) { return annotation.prefixKeep(); } @Override - Integer getSuffixKeep(PhoneNumber annotation) { + Integer getSuffixKeep(Mobile annotation) { return annotation.suffixKeep(); } @Override - String getReplacer(PhoneNumber annotation) { + String getReplacer(Mobile annotation) { return annotation.replacer(); } } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java index d3561ce5da..26a7de496f 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java @@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password; /** - * 密码脱敏处理器 + * {@link Password} 的码脱敏处理器 * * @author gaibu */ diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java similarity index 81% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java index 03db9cb720..18f4faee20 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java @@ -1,29 +1,33 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider; +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.Regex; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Address; +import cn.iocoder.yudao.framework.desensitize.core.annotation.Address; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Mobile; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider; import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; import lombok.Data; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; +/** + * {@link DesensitizeTest} 的单元测试 + */ public class DesensitizeTest extends BaseMockitoUnitTest { @Test public void test() { + // 准备参数 DesensitizeDemo desensitizeDemo = new DesensitizeDemo(); - desensitizeDemo.setUserName("芋道源码"); + desensitizeDemo.setNickname("芋道源码"); desensitizeDemo.setBankCard("9988002866797031"); desensitizeDemo.setCarLicense("粤A66666"); desensitizeDemo.setFixedPhone("01086551122"); @@ -33,13 +37,16 @@ public class DesensitizeTest extends BaseMockitoUnitTest { desensitizeDemo.setSlider1("ABCDEFG"); desensitizeDemo.setSlider2("ABCDEFG"); desensitizeDemo.setSlider3("ABCDEFG"); - desensitizeDemo.setEmail("1@eamil.com"); + desensitizeDemo.setEmail("1@email.com"); desensitizeDemo.setRegex("你好,我是芋道源码"); desensitizeDemo.setAddress("北京市海淀区上地十街10号"); desensitizeDemo.setOrigin("芋道源码"); + // 调用 DesensitizeDemo d = JsonUtils.parseObject(JsonUtils.toJsonString(desensitizeDemo), DesensitizeDemo.class); - assertEquals("芋***", d.getUserName()); + // 断言 + assertNotNull(d); + assertEquals("芋***", d.getNickname()); assertEquals("998800********31", d.getBankCard()); assertEquals("粤A6***6", d.getCarLicense()); assertEquals("0108*****22", d.getFixedPhone()); @@ -49,7 +56,7 @@ public class DesensitizeTest extends BaseMockitoUnitTest { assertEquals("#######", d.getSlider1()); assertEquals("ABC*EFG", d.getSlider2()); assertEquals("*******", d.getSlider3()); - assertEquals("1****@eamil.com", d.getEmail()); + assertEquals("1****@email.com", d.getEmail()); assertEquals("你好,我是*", d.getRegex()); assertEquals("北京市海淀区上地十街10号*", d.getAddress()); assertEquals("芋道源码", d.getOrigin()); @@ -57,8 +64,9 @@ public class DesensitizeTest extends BaseMockitoUnitTest { @Data public static class DesensitizeDemo { + @ChineseName - private String userName; + private String nickname; @BankCard private String bankCard; @CarLicense @@ -69,20 +77,22 @@ public class DesensitizeTest extends BaseMockitoUnitTest { private String idCard; @Password private String password; - @PhoneNumber + @Mobile private String phoneNumber; - @Slider(prefixKeep = 6,suffixKeep = 1,replacer = "#") + @Slider(prefixKeep = 6, suffixKeep = 1, replacer = "#") private String slider1; - @Slider(prefixKeep = 3,suffixKeep = 3) + @Slider(prefixKeep = 3, suffixKeep = 3) private String slider2; @Slider(prefixKeep = 10) private String slider3; @Email private String email; - @Regex(regex = "芋道源码",replacer = "*") + @Regex(regex = "芋道源码", replacer = "*") private String regex; @Address private String address; private String origin; + } + } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/annotation/Address.java similarity index 70% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/annotation/Address.java index 0cd338e1f5..735d25b346 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/annotation/Address.java @@ -1,7 +1,8 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; +package cn.iocoder.yudao.framework.desensitize.core.annotation; +import cn.iocoder.yudao.framework.desensitize.core.DesensitizeTest; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; -import cn.iocoder.yudao.framework.desensitize.core.slider.handler.AddressHandler; +import cn.iocoder.yudao.framework.desensitize.core.handler.AddressHandler; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import java.lang.annotation.Documented; @@ -13,6 +14,8 @@ import java.lang.annotation.Target; /** * 地址 * + * 用于 {@link DesensitizeTest} 测试使用 + * * @author gaibu */ @Documented diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/handler/AddressHandler.java similarity index 50% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/handler/AddressHandler.java index ef1a2bbf9a..7a8455f8d1 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/handler/AddressHandler.java @@ -1,11 +1,19 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; +package cn.iocoder.yudao.framework.desensitize.core.handler; +import cn.iocoder.yudao.framework.desensitize.core.DesensitizeTest; import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Address; +import cn.iocoder.yudao.framework.desensitize.core.annotation.Address; +/** + * {@link Address} 的脱敏处理器 + * + * 用于 {@link DesensitizeTest} 测试使用 + */ public class AddressHandler implements DesensitizationHandler
{ + @Override public String desensitize(String origin, Address annotation) { return origin + annotation.replacer(); } + } diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/package-info.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/package-info.java index 8c69219d02..2dc5316920 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/package-info.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/package-info.java @@ -1 +1,4 @@ +/** + * Web 框架,全局异常、API 日志等 + */ package cn.iocoder.yudao.framework;