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