Commit 6593ec4214ba27b87e890b759cc1b0a4db2e4236
1 parent
42bc0d15
code review 脱敏组件的代码
Showing
26 changed files
with
116 additions
and
65 deletions
yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java
| 1 | 1 | package cn.iocoder.yudao.framework.desensitize.core.base; |
| 2 | 2 | |
| 3 | +import cn.hutool.Hutool; | |
| 4 | +import cn.hutool.core.lang.Singleton; | |
| 3 | 5 | import cn.hutool.core.util.ReflectUtil; |
| 4 | 6 | import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; |
| 5 | 7 | |
| ... | ... | @@ -18,11 +20,13 @@ public class DesensitizationHandlerFactory { |
| 18 | 20 | */ |
| 19 | 21 | private static final Map<Class<? extends DesensitizationHandler>, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<Class<? extends DesensitizationHandler>, DesensitizationHandler>(); |
| 20 | 22 | |
| 23 | + // TODO @唐:可以考虑,使用 hutool 提供的 Singleton.get() | |
| 21 | 24 | public static DesensitizationHandler getDesensitizationHandler(Class<? extends DesensitizationHandler> clazz) { |
| 22 | 25 | DesensitizationHandler handler = HANDLER_MAP.get(clazz); |
| 23 | 26 | if (handler != null) { |
| 24 | 27 | return handler; |
| 25 | 28 | } |
| 29 | + // 不存在,则进行创建 | |
| 26 | 30 | synchronized (DesensitizationHandlerFactory.class) { |
| 27 | 31 | handler = HANDLER_MAP.get(clazz); |
| 28 | 32 | // 双重校验锁 | ... | ... |
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; |
| 12 | 12 | import java.lang.annotation.Target; |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | - * 顶级脱敏注解,自定义注解需要使用此注解。 | |
| 15 | + * 顶级脱敏注解,自定义注解需要使用此注解 | |
| 16 | 16 | * |
| 17 | 17 | * @author gaibu |
| 18 | 18 | */ |
| ... | ... | @@ -26,5 +26,7 @@ public @interface DesensitizeBy { |
| 26 | 26 | /** |
| 27 | 27 | * 脱敏处理器 |
| 28 | 28 | */ |
| 29 | + @SuppressWarnings("rawtypes") | |
| 29 | 30 | Class<? extends DesensitizationHandler> handler(); |
| 31 | + | |
| 30 | 32 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java
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; |
| 24 | 24 | /** |
| 25 | 25 | * 脱敏序列化器 |
| 26 | 26 | * |
| 27 | + * 实现 JSON 返回数据时,使用 {@link DesensitizationHandler} 对声明脱敏注解的字段,进行脱敏处理。 | |
| 28 | + * | |
| 27 | 29 | * @author gaibu |
| 28 | 30 | */ |
| 31 | +@SuppressWarnings("rawtypes") | |
| 29 | 32 | public class StringDesensitizeSerializer extends StdSerializer<String> implements ContextualSerializer { |
| 30 | 33 | |
| 31 | 34 | @Getter |
| ... | ... | @@ -37,17 +40,19 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement |
| 37 | 40 | } |
| 38 | 41 | |
| 39 | 42 | @Override |
| 40 | - public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { | |
| 43 | + public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) { | |
| 41 | 44 | DesensitizeBy annotation = beanProperty.getAnnotation(DesensitizeBy.class); |
| 42 | 45 | if (annotation == null) { |
| 43 | 46 | return this; |
| 44 | 47 | } |
| 48 | + // 创建一个 StringDesensitizeSerializer 对象,使用 DesensitizeBy 对应的处理器 | |
| 45 | 49 | StringDesensitizeSerializer serializer = new StringDesensitizeSerializer(); |
| 46 | 50 | serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler())); |
| 47 | 51 | return serializer; |
| 48 | 52 | } |
| 49 | 53 | |
| 50 | 54 | @Override |
| 55 | + @SuppressWarnings("unchecked") | |
| 51 | 56 | public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException { |
| 52 | 57 | if (StrUtil.isBlank(value)) { |
| 53 | 58 | gen.writeNull(); |
| ... | ... | @@ -75,12 +80,12 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement |
| 75 | 80 | /** |
| 76 | 81 | * 获取字段 |
| 77 | 82 | * |
| 78 | - * @param gen JsonGenerator | |
| 83 | + * @param generator JsonGenerator | |
| 79 | 84 | * @return 字段 |
| 80 | 85 | */ |
| 81 | - private Field getField(JsonGenerator gen) { | |
| 82 | - String currentName = gen.getOutputContext().getCurrentName(); | |
| 83 | - Object currentValue = gen.getCurrentValue(); | |
| 86 | + private Field getField(JsonGenerator generator) { | |
| 87 | + String currentName = generator.getOutputContext().getCurrentName(); | |
| 88 | + Object currentValue = generator.getCurrentValue(); | |
| 84 | 89 | Class<?> currentValueClass = currentValue.getClass(); |
| 85 | 90 | return ReflectUtil.getField(currentValueClass, currentName); |
| 86 | 91 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/package-info.java
0 → 100644
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; |
| 10 | 10 | import java.lang.annotation.RetentionPolicy; |
| 11 | 11 | import java.lang.annotation.Target; |
| 12 | 12 | |
| 13 | +// TODO @唐:名字还是 Email=》EmailDesensitize 合适一点,避免和 Validator 的注解有点冲突 | |
| 13 | 14 | /** |
| 14 | - * 邮箱 | |
| 15 | + * 邮箱脱敏注解 | |
| 15 | 16 | * |
| 16 | 17 | * @author gaibu |
| 17 | 18 | */ |
| ... | ... | @@ -28,7 +29,9 @@ public @interface Email { |
| 28 | 29 | String regex() default "(^.)[^@]*(@.*$)"; |
| 29 | 30 | |
| 30 | 31 | /** |
| 31 | - * 替换规则,邮箱;比如:example@gmail.com脱敏之后为e****@gmail.com | |
| 32 | + * 替换规则,邮箱; | |
| 33 | + * | |
| 34 | + * 比如:example@gmail.com 脱敏之后 为e****@gmail.com | |
| 32 | 35 | */ |
| 33 | 36 | String replacer() default "$1****$2"; |
| 34 | 37 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java
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; |
| 9 | 9 | * |
| 10 | 10 | * @author gaibu |
| 11 | 11 | */ |
| 12 | -public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> implements DesensitizationHandler<T> { | |
| 12 | +public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> | |
| 13 | + implements DesensitizationHandler<T> { | |
| 13 | 14 | |
| 14 | 15 | @Override |
| 15 | 16 | public String desensitize(String origin, T annotation) { |
| 16 | 17 | String regex = getRegex(annotation); |
| 17 | 18 | String replacer = getReplacer(annotation); |
| 18 | - | |
| 19 | 19 | return origin.replaceAll(regex, replacer); |
| 20 | 20 | } |
| 21 | 21 | |
| ... | ... | @@ -34,4 +34,5 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> |
| 34 | 34 | * @return 待替换的字符串 |
| 35 | 35 | */ |
| 36 | 36 | abstract String getReplacer(T annotation); |
| 37 | + | |
| 37 | 38 | } | ... | ... |
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; |
| 3 | 3 | import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex; |
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | - * 默认正则脱敏处理器 | |
| 6 | + * {@link Regex} 的正则脱敏处理器 | |
| 7 | 7 | * |
| 8 | 8 | * @author gaibu |
| 9 | 9 | */ |
| 10 | 10 | public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler<Regex> { |
| 11 | 11 | |
| 12 | - | |
| 13 | 12 | @Override |
| 14 | 13 | String getRegex(Regex annotation) { |
| 15 | 14 | return annotation.regex(); | ... | ... |
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; |
| 3 | 3 | import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email; |
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | - * 邮箱脱敏处理器 | |
| 6 | + * {@link Email} 的脱敏处理器 | |
| 7 | 7 | * |
| 8 | 8 | * @author gaibu |
| 9 | 9 | */ |
| ... | ... | @@ -18,4 +18,5 @@ public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHan |
| 18 | 18 | String getReplacer(Email annotation) { |
| 19 | 19 | return annotation.replacer(); |
| 20 | 20 | } |
| 21 | + | |
| 21 | 22 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java renamed to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Mobile.java
| 1 | 1 | package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; |
| 4 | -import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PhoneNumberDesensitization; | |
| 4 | +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.MobileDesensitization; | |
| 5 | 5 | import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; |
| 6 | 6 | |
| 7 | 7 | import java.lang.annotation.Documented; |
| ... | ... | @@ -19,8 +19,8 @@ import java.lang.annotation.Target; |
| 19 | 19 | @Target({ElementType.FIELD}) |
| 20 | 20 | @Retention(RetentionPolicy.RUNTIME) |
| 21 | 21 | @JacksonAnnotationsInside |
| 22 | -@DesensitizeBy(handler = PhoneNumberDesensitization.class) | |
| 23 | -public @interface PhoneNumber { | |
| 22 | +@DesensitizeBy(handler = MobileDesensitization.class) | |
| 23 | +public @interface Mobile { | |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * 前缀保留长度 | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java
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; |
| 9 | 9 | * |
| 10 | 10 | * @author gaibu |
| 11 | 11 | */ |
| 12 | -public abstract class AbstractDesensitizationHandler<T extends Annotation> implements DesensitizationHandler<T> { | |
| 12 | +public abstract class AbstractDesensitizationHandler<T extends Annotation> | |
| 13 | + implements DesensitizationHandler<T> { | |
| 13 | 14 | |
| 14 | 15 | @Override |
| 15 | 16 | public String desensitize(String origin, T annotation) { |
| ... | ... | @@ -36,6 +37,21 @@ public abstract class AbstractDesensitizationHandler<T extends Annotation> imple |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | /** |
| 40 | + * 根据长度循环构建替换符 | |
| 41 | + * | |
| 42 | + * @param replacer 替换符 | |
| 43 | + * @param length 长度 | |
| 44 | + * @return 构建后的替换符 | |
| 45 | + */ | |
| 46 | + private String buildReplacerByLength(String replacer, int length) { | |
| 47 | + StringBuilder builder = new StringBuilder(); | |
| 48 | + for (int i = 0; i < length; i++) { | |
| 49 | + builder.append(replacer); | |
| 50 | + } | |
| 51 | + return builder.toString(); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 39 | 55 | * 前缀保留长度 |
| 40 | 56 | * |
| 41 | 57 | * @param annotation 注解信息 |
| ... | ... | @@ -59,19 +75,4 @@ public abstract class AbstractDesensitizationHandler<T extends Annotation> imple |
| 59 | 75 | */ |
| 60 | 76 | abstract String getReplacer(T annotation); |
| 61 | 77 | |
| 62 | - /** | |
| 63 | - * 根据长度循环构建替换符 | |
| 64 | - * | |
| 65 | - * @param replacer 替换符 | |
| 66 | - * @param length 长度 | |
| 67 | - * @return 构建后的替换符 | |
| 68 | - */ | |
| 69 | - private String buildReplacerByLength(String replacer, int length) { | |
| 70 | - StringBuilder builder = new StringBuilder(); | |
| 71 | - for (int i = 0; i < length; i++) { | |
| 72 | - builder.append(replacer); | |
| 73 | - } | |
| 74 | - return builder.toString(); | |
| 75 | - } | |
| 76 | - | |
| 77 | 78 | } | ... | ... |
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; |
| 3 | 3 | import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard; |
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | - * 银行卡脱敏处理器 | |
| 6 | + * {@link BankCard} 的脱敏处理器 | |
| 7 | 7 | * |
| 8 | 8 | * @author gaibu |
| 9 | 9 | */ |
| ... | ... | @@ -24,4 +24,4 @@ public class BankCardDesensitization extends AbstractDesensitizationHandler<Bank |
| 24 | 24 | return annotation.replacer(); |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | -} | |
| 28 | 27 | \ No newline at end of file |
| 28 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java
| ... | ... | @@ -3,11 +3,12 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; |
| 3 | 3 | import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName; |
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | - * 中文姓名脱敏处理器 | |
| 6 | + * {@link ChineseName} 的脱敏处理器 | |
| 7 | 7 | * |
| 8 | 8 | * @author gaibu |
| 9 | 9 | */ |
| 10 | 10 | public class ChineseNameDesensitization extends AbstractDesensitizationHandler<ChineseName> { |
| 11 | + | |
| 11 | 12 | @Override |
| 12 | 13 | Integer getPrefixKeep(ChineseName annotation) { |
| 13 | 14 | return annotation.prefixKeep(); |
| ... | ... | @@ -22,4 +23,5 @@ public class ChineseNameDesensitization extends AbstractDesensitizationHandler<C |
| 22 | 23 | String getReplacer(ChineseName annotation) { |
| 23 | 24 | return annotation.replacer(); |
| 24 | 25 | } |
| 26 | + | |
| 25 | 27 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java renamed to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/MobileDesensitization.java
| 1 | 1 | package cn.iocoder.yudao.framework.desensitize.core.slider.handler; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber; | |
| 3 | +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Mobile; | |
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | - * 手机号脱敏处理器 | |
| 6 | + * {@link Mobile} 的脱敏处理器 | |
| 7 | 7 | * |
| 8 | 8 | * @author gaibu |
| 9 | 9 | */ |
| 10 | -public class PhoneNumberDesensitization extends AbstractDesensitizationHandler<PhoneNumber> { | |
| 10 | +public class MobileDesensitization extends AbstractDesensitizationHandler<Mobile> { | |
| 11 | 11 | |
| 12 | 12 | @Override |
| 13 | - Integer getPrefixKeep(PhoneNumber annotation) { | |
| 13 | + Integer getPrefixKeep(Mobile annotation) { | |
| 14 | 14 | return annotation.prefixKeep(); |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | @Override |
| 18 | - Integer getSuffixKeep(PhoneNumber annotation) { | |
| 18 | + Integer getSuffixKeep(Mobile annotation) { | |
| 19 | 19 | return annotation.suffixKeep(); |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | @Override |
| 23 | - String getReplacer(PhoneNumber annotation) { | |
| 23 | + String getReplacer(Mobile annotation) { | |
| 24 | 24 | return annotation.replacer(); |
| 25 | 25 | } |
| 26 | 26 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java
yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java renamed to yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java
| 1 | -package cn.iocoder.yudao.framework.desensitize.core.slider; | |
| 1 | +package cn.iocoder.yudao.framework.desensitize.core; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 4 | 4 | import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email; |
| 5 | 5 | import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex; |
| 6 | -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Address; | |
| 6 | +import cn.iocoder.yudao.framework.desensitize.core.annotation.Address; | |
| 7 | 7 | import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard; |
| 8 | 8 | import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense; |
| 9 | 9 | import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName; |
| 10 | 10 | import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone; |
| 11 | 11 | import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard; |
| 12 | 12 | import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password; |
| 13 | -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber; | |
| 13 | +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Mobile; | |
| 14 | 14 | import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider; |
| 15 | 15 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 16 | 16 | import lombok.Data; |
| 17 | 17 | import org.junit.jupiter.api.Test; |
| 18 | 18 | |
| 19 | -import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 19 | +import static org.junit.jupiter.api.Assertions.*; | |
| 20 | 20 | |
| 21 | +/** | |
| 22 | + * {@link DesensitizeTest} 的单元测试 | |
| 23 | + */ | |
| 21 | 24 | public class DesensitizeTest extends BaseMockitoUnitTest { |
| 22 | 25 | |
| 23 | 26 | @Test |
| 24 | 27 | public void test() { |
| 28 | + // 准备参数 | |
| 25 | 29 | DesensitizeDemo desensitizeDemo = new DesensitizeDemo(); |
| 26 | - desensitizeDemo.setUserName("芋道源码"); | |
| 30 | + desensitizeDemo.setNickname("芋道源码"); | |
| 27 | 31 | desensitizeDemo.setBankCard("9988002866797031"); |
| 28 | 32 | desensitizeDemo.setCarLicense("粤A66666"); |
| 29 | 33 | desensitizeDemo.setFixedPhone("01086551122"); |
| ... | ... | @@ -33,13 +37,16 @@ public class DesensitizeTest extends BaseMockitoUnitTest { |
| 33 | 37 | desensitizeDemo.setSlider1("ABCDEFG"); |
| 34 | 38 | desensitizeDemo.setSlider2("ABCDEFG"); |
| 35 | 39 | desensitizeDemo.setSlider3("ABCDEFG"); |
| 36 | - desensitizeDemo.setEmail("1@eamil.com"); | |
| 40 | + desensitizeDemo.setEmail("1@email.com"); | |
| 37 | 41 | desensitizeDemo.setRegex("你好,我是芋道源码"); |
| 38 | 42 | desensitizeDemo.setAddress("北京市海淀区上地十街10号"); |
| 39 | 43 | desensitizeDemo.setOrigin("芋道源码"); |
| 40 | 44 | |
| 45 | + // 调用 | |
| 41 | 46 | DesensitizeDemo d = JsonUtils.parseObject(JsonUtils.toJsonString(desensitizeDemo), DesensitizeDemo.class); |
| 42 | - assertEquals("芋***", d.getUserName()); | |
| 47 | + // 断言 | |
| 48 | + assertNotNull(d); | |
| 49 | + assertEquals("芋***", d.getNickname()); | |
| 43 | 50 | assertEquals("998800********31", d.getBankCard()); |
| 44 | 51 | assertEquals("粤A6***6", d.getCarLicense()); |
| 45 | 52 | assertEquals("0108*****22", d.getFixedPhone()); |
| ... | ... | @@ -49,7 +56,7 @@ public class DesensitizeTest extends BaseMockitoUnitTest { |
| 49 | 56 | assertEquals("#######", d.getSlider1()); |
| 50 | 57 | assertEquals("ABC*EFG", d.getSlider2()); |
| 51 | 58 | assertEquals("*******", d.getSlider3()); |
| 52 | - assertEquals("1****@eamil.com", d.getEmail()); | |
| 59 | + assertEquals("1****@email.com", d.getEmail()); | |
| 53 | 60 | assertEquals("你好,我是*", d.getRegex()); |
| 54 | 61 | assertEquals("北京市海淀区上地十街10号*", d.getAddress()); |
| 55 | 62 | assertEquals("芋道源码", d.getOrigin()); |
| ... | ... | @@ -57,8 +64,9 @@ public class DesensitizeTest extends BaseMockitoUnitTest { |
| 57 | 64 | |
| 58 | 65 | @Data |
| 59 | 66 | public static class DesensitizeDemo { |
| 67 | + | |
| 60 | 68 | @ChineseName |
| 61 | - private String userName; | |
| 69 | + private String nickname; | |
| 62 | 70 | @BankCard |
| 63 | 71 | private String bankCard; |
| 64 | 72 | @CarLicense |
| ... | ... | @@ -69,20 +77,22 @@ public class DesensitizeTest extends BaseMockitoUnitTest { |
| 69 | 77 | private String idCard; |
| 70 | 78 | @Password |
| 71 | 79 | private String password; |
| 72 | - @PhoneNumber | |
| 80 | + @Mobile | |
| 73 | 81 | private String phoneNumber; |
| 74 | - @Slider(prefixKeep = 6,suffixKeep = 1,replacer = "#") | |
| 82 | + @Slider(prefixKeep = 6, suffixKeep = 1, replacer = "#") | |
| 75 | 83 | private String slider1; |
| 76 | - @Slider(prefixKeep = 3,suffixKeep = 3) | |
| 84 | + @Slider(prefixKeep = 3, suffixKeep = 3) | |
| 77 | 85 | private String slider2; |
| 78 | 86 | @Slider(prefixKeep = 10) |
| 79 | 87 | private String slider3; |
| 80 | 88 | |
| 81 | 89 | private String email; |
| 82 | - @Regex(regex = "芋道源码",replacer = "*") | |
| 90 | + @Regex(regex = "芋道源码", replacer = "*") | |
| 83 | 91 | private String regex; |
| 84 | 92 | @Address |
| 85 | 93 | private String address; |
| 86 | 94 | private String origin; |
| 95 | + | |
| 87 | 96 | } |
| 97 | + | |
| 88 | 98 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java renamed to yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/annotation/Address.java
| 1 | -package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; | |
| 1 | +package cn.iocoder.yudao.framework.desensitize.core.annotation; | |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.desensitize.core.DesensitizeTest; | |
| 3 | 4 | import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; |
| 4 | -import cn.iocoder.yudao.framework.desensitize.core.slider.handler.AddressHandler; | |
| 5 | +import cn.iocoder.yudao.framework.desensitize.core.handler.AddressHandler; | |
| 5 | 6 | import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; |
| 6 | 7 | |
| 7 | 8 | import java.lang.annotation.Documented; |
| ... | ... | @@ -13,6 +14,8 @@ import java.lang.annotation.Target; |
| 13 | 14 | /** |
| 14 | 15 | * 地址 |
| 15 | 16 | * |
| 17 | + * 用于 {@link DesensitizeTest} 测试使用 | |
| 18 | + * | |
| 16 | 19 | * @author gaibu |
| 17 | 20 | */ |
| 18 | 21 | @Documented | ... | ... |
yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java renamed to yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/handler/AddressHandler.java
| 1 | -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; | |
| 1 | +package cn.iocoder.yudao.framework.desensitize.core.handler; | |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.desensitize.core.DesensitizeTest; | |
| 3 | 4 | import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; |
| 4 | -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Address; | |
| 5 | +import cn.iocoder.yudao.framework.desensitize.core.annotation.Address; | |
| 5 | 6 | |
| 7 | +/** | |
| 8 | + * {@link Address} 的脱敏处理器 | |
| 9 | + * | |
| 10 | + * 用于 {@link DesensitizeTest} 测试使用 | |
| 11 | + */ | |
| 6 | 12 | public class AddressHandler implements DesensitizationHandler<Address> { |
| 13 | + | |
| 7 | 14 | @Override |
| 8 | 15 | public String desensitize(String origin, Address annotation) { |
| 9 | 16 | return origin + annotation.replacer(); |
| 10 | 17 | } |
| 18 | + | |
| 11 | 19 | } | ... | ... |