From e637bff8cdc7071a04489fcbe3ffa60dd5b7c344 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Tue, 10 Jan 2023 15:10:16 +0800 Subject: [PATCH 01/13] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20vo=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-framework/pom.xml | 1 + .../pom.xml | 26 ++++++++++ .../desensitize/annotation/Desensitize.java | 50 +++++++++++++++++++ .../constants/DesensitizeConstants.java | 24 +++++++++ .../enums/DesensitizationStrategyEnum.java | 49 ++++++++++++++++++ .../DefaultDesensitizationHandler.java | 10 ++++ .../handler/DesensitizationHandler.java | 13 +++++ 7 files changed, 173 insertions(+) create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/Desensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/constants/DesensitizeConstants.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/enums/DesensitizationStrategyEnum.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DefaultDesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java diff --git a/yudao-framework/pom.xml b/yudao-framework/pom.xml index 3bfc020ba..0f20b1a78 100644 --- a/yudao-framework/pom.xml +++ b/yudao-framework/pom.xml @@ -41,6 +41,7 @@ yudao-spring-boot-starter-flowable yudao-spring-boot-starter-captcha yudao-spring-boot-starter-websocket + yudao-spring-boot-starter-biz-desensitize yudao-framework diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml new file mode 100644 index 000000000..4119f8ea7 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + cn.iocoder.boot + yudao-framework + 1.6.6-snapshot + + + yudao-spring-boot-starter-biz-desensitize + + + 11 + 11 + UTF-8 + + + + + cn.iocoder.boot + yudao-common + + + \ 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/annotation/Desensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/Desensitize.java new file mode 100644 index 000000000..bf0f585c4 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/Desensitize.java @@ -0,0 +1,50 @@ +package cn.iocoder.yudao.framework.desensitize.annotation; + +import cn.iocoder.yudao.framework.desensitize.enums.DesensitizationStrategyEnum; +import cn.iocoder.yudao.framework.desensitize.handler.DesensitizationHandler; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Desensitize 注解配置会覆盖 DesensitizationStrategyEnum 配置 + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface Desensitize { + + /** + * 脱敏策略 + */ + DesensitizationStrategyEnum strategy(); + + /** + * 脱敏替换字符 + */ + String replacer(); + + /** + * 正则表达式 + */ + String regex(); + + /** + * 前缀保留长度 + */ + int preKeep(); + + /** + * 后缀保留长度 + */ + int suffixKeep(); + + /** + * 脱敏处理器 + */ + Class handler(); +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/constants/DesensitizeConstants.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/constants/DesensitizeConstants.java new file mode 100644 index 000000000..f883173c3 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/constants/DesensitizeConstants.java @@ -0,0 +1,24 @@ +package cn.iocoder.yudao.framework.desensitize.constants; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class DesensitizeConstants { + + /** + * 默认正则 + */ + public static final String DEFAULT_REGEX = null; + + /** + * 默认保持长度 + */ + public static final int DEFAULT_KEEP_LENGTH = -1; + + /** + * 默认替换字符 + */ + public static final String DEFAULT_REPLACER = "****"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/enums/DesensitizationStrategyEnum.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/enums/DesensitizationStrategyEnum.java new file mode 100644 index 000000000..89a1f63c8 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/enums/DesensitizationStrategyEnum.java @@ -0,0 +1,49 @@ +package cn.iocoder.yudao.framework.desensitize.enums; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import static cn.iocoder.yudao.framework.desensitize.constants.DesensitizeConstants.DEFAULT_KEEP_LENGTH; +import static cn.iocoder.yudao.framework.desensitize.constants.DesensitizeConstants.DEFAULT_REGEX; +import static cn.iocoder.yudao.framework.desensitize.constants.DesensitizeConstants.DEFAULT_REPLACER; + +@Getter +@RequiredArgsConstructor +public enum DesensitizationStrategyEnum { + // 常用脱敏业务 + PHONE_NUMBER(DEFAULT_REGEX, 3, 4, DEFAULT_REPLACER), // 手机号;比如:13248765917脱敏之后为132****5917 + FIXED_PHONE(DEFAULT_REGEX, 4, 2, DEFAULT_REPLACER), // 固定电话;比如:01086551122脱敏之后为0108*****22 + ID_CARD(DEFAULT_REGEX, 6, 2, DEFAULT_REPLACER), // 身份证号码;比如:530321199204074611脱敏之后为530321**********11 + BANK_CARD(DEFAULT_REGEX, 6, 2, DEFAULT_REPLACER), // 银行卡号;比如:9988002866797031脱敏之后为998800********31 + CHINESE_NAME(DEFAULT_REGEX, 1, 0, "**"),// 中文名;比如:刘子豪脱敏之后为刘** + ADDRESS("[\\s\\S]+区", DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, DEFAULT_REPLACER), // 地址只显示到地区,不显示详细地址;比如:广州市天河区幸福小区102号脱敏之后为广州市天河区******** + EMAIL("(^.)[^@]*(@.*$)", DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, "$1****$2"), // 邮箱;比如:example@gmail.com脱敏之后为e******@gmail.com + CAR_LICENSE(DEFAULT_REGEX, 3, 1, DEFAULT_REPLACER), // 车牌号;比如:粤A66666脱敏之后为粤A6***6 + PASSWORD(DEFAULT_REGEX, 0, 0, DEFAULT_REPLACER), // 密码;比如:123456脱敏之后为****** + + // 自定义脱敏业务 + REGEX(DEFAULT_REGEX, DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, DEFAULT_REPLACER), // 自定义正则表达式 + SLIDE(DEFAULT_REGEX, DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, DEFAULT_REPLACER), // 滑动脱敏 + CUSTOM_HANDLE(DEFAULT_REGEX, DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, DEFAULT_REPLACER); // 自定义处理器 + ; + + /** + * 正则表达式 + */ + private final String regex; + + /** + * 前缀保留长度 + */ + private final int preKeep; + + /** + * 后缀保留长度 + */ + private final int suffixKeep; + + /** + * 脱敏替换字符 + */ + private final String replacer; +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DefaultDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DefaultDesensitizationHandler.java new file mode 100644 index 000000000..4f8883c50 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DefaultDesensitizationHandler.java @@ -0,0 +1,10 @@ +package cn.iocoder.yudao.framework.desensitize.handler; + +public class DefaultDesensitizationHandler implements DesensitizationHandler { + + @Override + public String handle(String origin) { + return origin; + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java new file mode 100644 index 000000000..d718aae1b --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java @@ -0,0 +1,13 @@ +package cn.iocoder.yudao.framework.desensitize.handler; + +public interface DesensitizationHandler { + + /** + * 脱敏 + * + * @param origin 原始字符串 + * @return 脱敏后的字符串 + */ + String handle(String origin); + +} From 5c8e41b84791cf92cc45896071a6b96b49ba20e3 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 12 Jan 2023 14:54:33 +0800 Subject: [PATCH 02/13] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20vo=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pom.xml | 21 ++++- .../desensitize/annotation/Desensitize.java | 40 +++------- .../annotation/RegexDesensitize.java | 33 ++++++++ .../annotation/SliderDesensitize.java | 39 ++++++++++ .../constraints/BankCardDesensitize.java | 21 +++++ .../constraints/CarLicenseDesensitize.java | 21 +++++ .../constraints/ChineseNameDesensitize.java | 21 +++++ .../constraints/EmailDesensitize.java | 21 +++++ .../constraints/FixedPhoneDesensitize.java | 21 +++++ .../constraints/IdCardDesensitize.java | 21 +++++ .../constraints/PasswordDesensitize.java | 21 +++++ .../constraints/PhoneNumberDesensitize.java | 21 +++++ .../constants/DesensitizeConstants.java | 24 ------ .../enums/DesensitizationStrategyEnum.java | 49 ------------ .../DefaultDesensitizationHandler.java | 10 --- .../handler/DesensitizationHandler.java | 2 +- .../handler/DesensitizationHandlerHolder.java | 34 +++++++++ .../handler/RegexDesensitizationHandler.java | 13 ++++ .../handler/SliderDesensitizationHandler.java | 39 ++++++++++ .../StringDesensitizeSerializer.java | 76 +++++++++++++++++++ .../handler/DesensitizationHandlerTest.java | 25 ++++++ 21 files changed, 457 insertions(+), 116 deletions(-) create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/RegexDesensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/SliderDesensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/BankCardDesensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/CarLicenseDesensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/ChineseNameDesensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/EmailDesensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/FixedPhoneDesensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/IdCardDesensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PasswordDesensitize.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PhoneNumberDesensitize.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/constants/DesensitizeConstants.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/enums/DesensitizationStrategyEnum.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DefaultDesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/serializer/StringDesensitizeSerializer.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerTest.java 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 4119f8ea7..7f15d0d4c 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml @@ -4,9 +4,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - cn.iocoder.boot yudao-framework - 1.6.6-snapshot + cn.iocoder.boot + ${revision} yudao-spring-boot-starter-biz-desensitize @@ -22,5 +22,22 @@ cn.iocoder.boot yudao-common + + + + com.fasterxml.jackson.core + jackson-annotations + + + com.fasterxml.jackson.core + jackson-databind + + + + + cn.iocoder.boot + yudao-spring-boot-starter-test + 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/annotation/Desensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/Desensitize.java index bf0f585c4..65dd2108d 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/Desensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/Desensitize.java @@ -1,7 +1,9 @@ package cn.iocoder.yudao.framework.desensitize.annotation; -import cn.iocoder.yudao.framework.desensitize.enums.DesensitizationStrategyEnum; import cn.iocoder.yudao.framework.desensitize.handler.DesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.serializer.StringDesensitizeSerializer; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; @@ -11,40 +13,18 @@ import java.lang.annotation.Target; /** - * Desensitize 注解配置会覆盖 DesensitizationStrategyEnum 配置 + * Desensitize 顶级脱敏注解 */ -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) @Documented +@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@JsonSerialize(using = StringDesensitizeSerializer.class) public @interface Desensitize { - /** - * 脱敏策略 - */ - DesensitizationStrategyEnum strategy(); - - /** - * 脱敏替换字符 - */ - String replacer(); - - /** - * 正则表达式 - */ - String regex(); - - /** - * 前缀保留长度 - */ - int preKeep(); - - /** - * 后缀保留长度 - */ - int suffixKeep(); - /** * 脱敏处理器 */ - Class handler(); + Class desensitizationHandler(); + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/RegexDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/RegexDesensitize.java new file mode 100644 index 000000000..c2df586c4 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/RegexDesensitize.java @@ -0,0 +1,33 @@ +package cn.iocoder.yudao.framework.desensitize.annotation; + +import cn.iocoder.yudao.framework.desensitize.handler.RegexDesensitizationHandler; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 正则脱敏注解 + */ +@Documented +@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationHandler = RegexDesensitizationHandler.class) +public @interface RegexDesensitize { + /** + * 匹配的正则表达式(默认匹配所有) + */ + String regex() default "^[\\s\\S]*$"; + + /** + * 替换规则,会将匹配到的字符串全部替换成 replacer + * 例如:regex=123; replacer=****** + * 原始字符串 123456789 + * 脱敏后字符串 ******456789 + */ + String replacer() default "******"; +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/SliderDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/SliderDesensitize.java new file mode 100644 index 000000000..ebafaa9bc --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/SliderDesensitize.java @@ -0,0 +1,39 @@ +package cn.iocoder.yudao.framework.desensitize.annotation; + +import cn.iocoder.yudao.framework.desensitize.handler.SliderDesensitizationHandler; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 滑动脱敏注解 + */ +@Documented +@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationHandler = SliderDesensitizationHandler.class) +public @interface SliderDesensitize { + + /** + * 后缀保留长度 + */ + int suffixKeep() default 0; + + /** + * 替换规则,会将前缀后缀保留后,全部替换成 replacer + * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; + * 原始字符串 123456 + * 脱敏后 1***56 + */ + String replacer() default "*"; + + /** + * 前缀保留长度 + */ + int prefixKeep() default 0; +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/BankCardDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/BankCardDesensitize.java new file mode 100644 index 000000000..8fa73169a --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/BankCardDesensitize.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.framework.desensitize.annotation.constraints; + +import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 银行卡号 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@SliderDesensitize(prefixKeep = 6, suffixKeep = 2, replacer = "*") // 银行卡号;比如:9988002866797031脱敏之后为998800********31 +public @interface BankCardDesensitize { +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/CarLicenseDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/CarLicenseDesensitize.java new file mode 100644 index 000000000..11f1f5814 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/CarLicenseDesensitize.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.framework.desensitize.annotation.constraints; + +import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 车牌号 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@SliderDesensitize(prefixKeep = 3, suffixKeep = 1, replacer = "*") // 车牌号;比如:粤A66666脱敏之后为粤A6***6 +public @interface CarLicenseDesensitize { +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/ChineseNameDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/ChineseNameDesensitize.java new file mode 100644 index 000000000..97d2698ed --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/ChineseNameDesensitize.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.framework.desensitize.annotation.constraints; + +import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 中文名 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@SliderDesensitize(prefixKeep = 1, suffixKeep = 0, replacer = "*") // 中文名;比如:刘子豪脱敏之后为刘** +public @interface ChineseNameDesensitize { +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/EmailDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/EmailDesensitize.java new file mode 100644 index 000000000..142e7c0f6 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/EmailDesensitize.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.framework.desensitize.annotation.constraints; + +import cn.iocoder.yudao.framework.desensitize.annotation.RegexDesensitize; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 邮箱 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@RegexDesensitize(regex = "(^.)[^@]*(@.*$)", replacer ="$1****$2") // 邮箱;比如:example@gmail.com脱敏之后为e****@gmail.com +public @interface EmailDesensitize { +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/FixedPhoneDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/FixedPhoneDesensitize.java new file mode 100644 index 000000000..f91988735 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/FixedPhoneDesensitize.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.framework.desensitize.annotation.constraints; + +import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 固定电话 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@SliderDesensitize(prefixKeep = 4, suffixKeep = 2, replacer = "*") // 固定电话;比如:01086551122脱敏之后为0108*****22 +public @interface FixedPhoneDesensitize { +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/IdCardDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/IdCardDesensitize.java new file mode 100644 index 000000000..921019c5d --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/IdCardDesensitize.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.framework.desensitize.annotation.constraints; + +import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 身份证 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@SliderDesensitize(prefixKeep = 6, suffixKeep = 2, replacer = "*") // 身份证号码;比如:530321199204074611脱敏之后为530321**********11 +public @interface IdCardDesensitize { +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PasswordDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PasswordDesensitize.java new file mode 100644 index 000000000..8d823a139 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PasswordDesensitize.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.framework.desensitize.annotation.constraints; + +import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 密码 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@SliderDesensitize(prefixKeep = 0, suffixKeep = 0, replacer = "*") // 密码;比如:123456脱敏之后为****** +public @interface PasswordDesensitize { +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PhoneNumberDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PhoneNumberDesensitize.java new file mode 100644 index 000000000..0829c6a45 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PhoneNumberDesensitize.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.framework.desensitize.annotation.constraints; + +import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 手机号 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@SliderDesensitize(prefixKeep = 3, suffixKeep = 4, replacer = "*") // 手机号;比如:13248765917脱敏之后为132****5917 +public @interface PhoneNumberDesensitize { +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/constants/DesensitizeConstants.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/constants/DesensitizeConstants.java deleted file mode 100644 index f883173c3..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/constants/DesensitizeConstants.java +++ /dev/null @@ -1,24 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.constants; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class DesensitizeConstants { - - /** - * 默认正则 - */ - public static final String DEFAULT_REGEX = null; - - /** - * 默认保持长度 - */ - public static final int DEFAULT_KEEP_LENGTH = -1; - - /** - * 默认替换字符 - */ - public static final String DEFAULT_REPLACER = "****"; - -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/enums/DesensitizationStrategyEnum.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/enums/DesensitizationStrategyEnum.java deleted file mode 100644 index 89a1f63c8..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/enums/DesensitizationStrategyEnum.java +++ /dev/null @@ -1,49 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.enums; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -import static cn.iocoder.yudao.framework.desensitize.constants.DesensitizeConstants.DEFAULT_KEEP_LENGTH; -import static cn.iocoder.yudao.framework.desensitize.constants.DesensitizeConstants.DEFAULT_REGEX; -import static cn.iocoder.yudao.framework.desensitize.constants.DesensitizeConstants.DEFAULT_REPLACER; - -@Getter -@RequiredArgsConstructor -public enum DesensitizationStrategyEnum { - // 常用脱敏业务 - PHONE_NUMBER(DEFAULT_REGEX, 3, 4, DEFAULT_REPLACER), // 手机号;比如:13248765917脱敏之后为132****5917 - FIXED_PHONE(DEFAULT_REGEX, 4, 2, DEFAULT_REPLACER), // 固定电话;比如:01086551122脱敏之后为0108*****22 - ID_CARD(DEFAULT_REGEX, 6, 2, DEFAULT_REPLACER), // 身份证号码;比如:530321199204074611脱敏之后为530321**********11 - BANK_CARD(DEFAULT_REGEX, 6, 2, DEFAULT_REPLACER), // 银行卡号;比如:9988002866797031脱敏之后为998800********31 - CHINESE_NAME(DEFAULT_REGEX, 1, 0, "**"),// 中文名;比如:刘子豪脱敏之后为刘** - ADDRESS("[\\s\\S]+区", DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, DEFAULT_REPLACER), // 地址只显示到地区,不显示详细地址;比如:广州市天河区幸福小区102号脱敏之后为广州市天河区******** - EMAIL("(^.)[^@]*(@.*$)", DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, "$1****$2"), // 邮箱;比如:example@gmail.com脱敏之后为e******@gmail.com - CAR_LICENSE(DEFAULT_REGEX, 3, 1, DEFAULT_REPLACER), // 车牌号;比如:粤A66666脱敏之后为粤A6***6 - PASSWORD(DEFAULT_REGEX, 0, 0, DEFAULT_REPLACER), // 密码;比如:123456脱敏之后为****** - - // 自定义脱敏业务 - REGEX(DEFAULT_REGEX, DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, DEFAULT_REPLACER), // 自定义正则表达式 - SLIDE(DEFAULT_REGEX, DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, DEFAULT_REPLACER), // 滑动脱敏 - CUSTOM_HANDLE(DEFAULT_REGEX, DEFAULT_KEEP_LENGTH, DEFAULT_KEEP_LENGTH, DEFAULT_REPLACER); // 自定义处理器 - ; - - /** - * 正则表达式 - */ - private final String regex; - - /** - * 前缀保留长度 - */ - private final int preKeep; - - /** - * 后缀保留长度 - */ - private final int suffixKeep; - - /** - * 脱敏替换字符 - */ - private final String replacer; -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DefaultDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DefaultDesensitizationHandler.java deleted file mode 100644 index 4f8883c50..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DefaultDesensitizationHandler.java +++ /dev/null @@ -1,10 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.handler; - -public class DefaultDesensitizationHandler implements DesensitizationHandler { - - @Override - public String handle(String origin) { - return origin; - } - -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java index d718aae1b..a5d06feb1 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java @@ -8,6 +8,6 @@ public interface DesensitizationHandler { * @param origin 原始字符串 * @return 脱敏后的字符串 */ - String handle(String origin); + String desensitize(String origin, Object... arg); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java new file mode 100644 index 000000000..23336b0cf --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java @@ -0,0 +1,34 @@ +package cn.iocoder.yudao.framework.desensitize.handler; + +import cn.hutool.core.util.ReflectUtil; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class DesensitizationHandlerHolder { + /** + * handler 缓存,默认初始化内置的处理器 + */ + private static final Map, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<>() {{ + put(RegexDesensitizationHandler.class, new RegexDesensitizationHandler()); + put(SliderDesensitizationHandler.class, new SliderDesensitizationHandler()); + }}; + + public static DesensitizationHandler getDesensitizationHandler(Class clazz) { + DesensitizationHandler handler = HANDLER_MAP.get(clazz); + if (handler != null) { + return handler; + } + synchronized (DesensitizationHandlerHolder.class) { + handler = HANDLER_MAP.get(clazz); + // 双重校验锁 + if (handler != null) { + return handler; + } + handler = ReflectUtil.newInstanceIfPossible(clazz); + HANDLER_MAP.put(clazz, handler); + } + return handler; + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java new file mode 100644 index 000000000..1846156d4 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java @@ -0,0 +1,13 @@ +package cn.iocoder.yudao.framework.desensitize.handler; + +public class RegexDesensitizationHandler implements DesensitizationHandler { + + @Override + public String desensitize(String origin, Object... arg) { + String regex = (String) arg[0]; + String replacer = (String) arg[1]; + + return origin.replaceAll(regex, replacer); + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java new file mode 100644 index 000000000..81b9ccc50 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java @@ -0,0 +1,39 @@ +package cn.iocoder.yudao.framework.desensitize.handler; + +public class SliderDesensitizationHandler implements DesensitizationHandler { + + @Override + public String desensitize(String origin, Object... arg) { + int prefixKeep = (Integer) arg[0]; + int suffixKeep = (Integer) arg[1]; + String replacer = (String) arg[2]; + + int length = origin.length(); + + // 原始字符串长度小于等于保留长度,则原始字符串全部替换 + if (prefixKeep >= length || suffixKeep >= length) { + return buildReplacerByLength(replacer, length); + } + + // 如果原始字符串小于等于前后缀保留字符串长度,则原始字符串全部替换 + if ((prefixKeep + suffixKeep) >= length) { + return buildReplacerByLength(replacer, length); + } + + int interval = length - prefixKeep - suffixKeep; + return origin.substring(0, prefixKeep) + + buildReplacerByLength(replacer, interval) + + origin.substring(prefixKeep + interval); + } + + /** + * 根据长度循环构建替换符 + * + * @param replacer 替换符 + * @param length 长度 + * @return 构建后的替换符 + */ + 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 new file mode 100644 index 000000000..a43b3e28e --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/serializer/StringDesensitizeSerializer.java @@ -0,0 +1,76 @@ +package cn.iocoder.yudao.framework.desensitize.serializer; + +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; +import cn.iocoder.yudao.framework.desensitize.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.annotation.RegexDesensitize; +import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; +import cn.iocoder.yudao.framework.desensitize.handler.DesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.handler.DesensitizationHandlerHolder; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.BeanProperty; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.ContextualSerializer; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +import java.io.IOException; +import java.lang.reflect.Field; + +/** + * 脱敏序列化器 + */ +public class StringDesensitizeSerializer extends StdSerializer implements ContextualSerializer { + private final DesensitizationHandler desensitizationHandler; + + protected StringDesensitizeSerializer(DesensitizationHandler desensitizationHandler) { + super(String.class); + this.desensitizationHandler = desensitizationHandler; + } + + + @Override + public JsonSerializer createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { + Desensitize annotation = beanProperty.getAnnotation(Desensitize.class); + if (annotation == null) { + return this; + } + + return new StringDesensitizeSerializer(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationHandler())); + } + + @Override + public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException { + if (StrUtil.isBlank(value)) { + gen.writeNull(); + return; + } + + String currentName = gen.getOutputContext().getCurrentName(); + Object currentValue = gen.getCurrentValue(); + Class currentValueClass = currentValue.getClass(); + Field field = ReflectUtil.getField(currentValueClass, currentName); + + // 滑动处理器 + SliderDesensitize sliderDesensitize = field.getAnnotation(SliderDesensitize.class); + if (sliderDesensitize != null) { + value = this.desensitizationHandler.desensitize(value, sliderDesensitize.prefixKeep(), sliderDesensitize.suffixKeep(), sliderDesensitize.replacer()); + } + + // 正则处理器 + RegexDesensitize regexDesensitize = field.getAnnotation(RegexDesensitize.class); + if (regexDesensitize != null) { + value = this.desensitizationHandler.desensitize(value, regexDesensitize.regex(), regexDesensitize.replacer()); + } + + // 自定义处理器 + Desensitize desensitize = field.getAnnotation(Desensitize.class); + if (desensitize != null) { + value = this.desensitizationHandler.desensitize(value); + } + + gen.writeString(value); + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerTest.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerTest.java new file mode 100644 index 000000000..63b4eaabf --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerTest.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.desensitize.handler; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class DesensitizationHandlerTest { + + @Test + public void testSliderDesensitizationHandler() { + DesensitizationHandler handler = DesensitizationHandlerHolder.getDesensitizationHandler(SliderDesensitizationHandler.class); + + Assertions.assertEquals("A****FG", handler.desensitize("ABCDEFG", 1, 2, "*")); + Assertions.assertEquals("芋**码", handler.desensitize("芋道源码", 1, 1, "*")); + Assertions.assertEquals("****", handler.desensitize("芋道源码", 4, 0, "*")); + } + + @Test + public void testRegexDesensitizationHandler() { + DesensitizationHandler handler = DesensitizationHandlerHolder.getDesensitizationHandler(RegexDesensitizationHandler.class); + + Assertions.assertEquals("e****@gmail.com", handler.desensitize("example@gmail.com", "(^.)[^@]*(@.*$)", "$1****$2")); + Assertions.assertEquals("***,铁***", handler.desensitize("他妈的,铁废物", "他妈的|去你大爷|卧槽|草泥马|废物", "***")); + } + +} From cd9bcda9350a28d6547af3a391fdea757ad8b8ea Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 12 Jan 2023 15:31:44 +0800 Subject: [PATCH 03/13] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20vo=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-dependencies/pom.xml | 5 ++++ .../StringDesensitizeSerializer.java | 29 ++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index 7b056c46b..be2f14dd6 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -141,6 +141,11 @@ yudao-spring-boot-starter-captcha ${revision} + + cn.iocoder.boot + yudao-spring-boot-starter-biz-desensitize + ${revision} + 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 a43b3e28e..f90475d6d 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 @@ -1,5 +1,7 @@ package cn.iocoder.yudao.framework.desensitize.serializer; +import cn.hutool.core.annotation.AnnotationUtil; +import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.desensitize.annotation.Desensitize; @@ -22,13 +24,19 @@ import java.lang.reflect.Field; * 脱敏序列化器 */ public class StringDesensitizeSerializer extends StdSerializer implements ContextualSerializer { - private final DesensitizationHandler desensitizationHandler; + private DesensitizationHandler desensitizationHandler; - protected StringDesensitizeSerializer(DesensitizationHandler desensitizationHandler) { + protected StringDesensitizeSerializer() { super(String.class); - this.desensitizationHandler = desensitizationHandler; } + public DesensitizationHandler getDesensitizationHandler() { + return desensitizationHandler; + } + + public void setDesensitizationHandler(DesensitizationHandler desensitizationHandler) { + this.desensitizationHandler = desensitizationHandler; + } @Override public JsonSerializer createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { @@ -36,8 +44,9 @@ public class StringDesensitizeSerializer extends StdSerializer implement if (annotation == null) { return this; } - - return new StringDesensitizeSerializer(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationHandler())); + StringDesensitizeSerializer serializer = new StringDesensitizeSerializer(); + serializer.setDesensitizationHandler(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationHandler())); + return serializer; } @Override @@ -53,19 +62,23 @@ public class StringDesensitizeSerializer extends StdSerializer implement Field field = ReflectUtil.getField(currentValueClass, currentName); // 滑动处理器 - SliderDesensitize sliderDesensitize = field.getAnnotation(SliderDesensitize.class); + SliderDesensitize sliderDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, SliderDesensitize.class)); if (sliderDesensitize != null) { value = this.desensitizationHandler.desensitize(value, sliderDesensitize.prefixKeep(), sliderDesensitize.suffixKeep(), sliderDesensitize.replacer()); + gen.writeString(value); + return; } // 正则处理器 - RegexDesensitize regexDesensitize = field.getAnnotation(RegexDesensitize.class); + RegexDesensitize regexDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, RegexDesensitize.class)); if (regexDesensitize != null) { value = this.desensitizationHandler.desensitize(value, regexDesensitize.regex(), regexDesensitize.replacer()); + gen.writeString(value); + return; } // 自定义处理器 - Desensitize desensitize = field.getAnnotation(Desensitize.class); + Desensitize desensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, Desensitize.class)); if (desensitize != null) { value = this.desensitizationHandler.desensitize(value); } From 70f15ab60db12282411bc6fd6e50acb7798b3f78 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 12 Jan 2023 15:43:31 +0800 Subject: [PATCH 04/13] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/desensitize/handler/DesensitizationHandler.java | 3 +++ .../desensitize/handler/DesensitizationHandlerHolder.java | 3 +++ .../desensitize/handler/RegexDesensitizationHandler.java | 3 +++ .../desensitize/handler/SliderDesensitizationHandler.java | 3 +++ 4 files changed, 12 insertions(+) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java index a5d06feb1..ba5a63bfb 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java @@ -1,5 +1,8 @@ package cn.iocoder.yudao.framework.desensitize.handler; +/** + * 脱敏处理器接口 + */ public interface DesensitizationHandler { /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java index 23336b0cf..456d2b920 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java @@ -5,6 +5,9 @@ import cn.hutool.core.util.ReflectUtil; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +/** + * 脱敏处理器 Holder + */ public class DesensitizationHandlerHolder { /** * handler 缓存,默认初始化内置的处理器 diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java index 1846156d4..63089054f 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java @@ -1,5 +1,8 @@ package cn.iocoder.yudao.framework.desensitize.handler; +/** + * 正则脱敏处理器 + */ public class RegexDesensitizationHandler implements DesensitizationHandler { @Override diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java index 81b9ccc50..2d8336d1f 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java @@ -1,5 +1,8 @@ package cn.iocoder.yudao.framework.desensitize.handler; +/** + * 滑动脱敏处理器 + */ public class SliderDesensitizationHandler implements DesensitizationHandler { @Override From 61a34ddac5478887621d05cd7cf9c6019a62f27b Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 12 Jan 2023 19:57:11 +0800 Subject: [PATCH 05/13] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20vo=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/DesensitizationHandler.java | 14 ++++++++++++- .../handler/RegexDesensitizationHandler.java | 9 ++++++++- .../handler/SliderDesensitizationHandler.java | 9 ++++++++- .../StringDesensitizeSerializer.java | 20 ++++++++++++++----- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java index ba5a63bfb..74870e771 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java @@ -1,16 +1,28 @@ package cn.iocoder.yudao.framework.desensitize.handler; +import java.lang.annotation.Annotation; + /** * 脱敏处理器接口 */ -public interface DesensitizationHandler { +public interface DesensitizationHandler { /** * 脱敏 * * @param origin 原始字符串 + * @param arg 参数 * @return 脱敏后的字符串 */ String desensitize(String origin, Object... arg); + /** + * 获取注解参数 + * + * @param anno 注解 + * @return 注解参数 + */ + default Object[] getAnnotationArgs(T anno) { + return new Object[0]; + } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java index 63089054f..57fc71178 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java @@ -1,9 +1,11 @@ package cn.iocoder.yudao.framework.desensitize.handler; +import cn.iocoder.yudao.framework.desensitize.annotation.RegexDesensitize; + /** * 正则脱敏处理器 */ -public class RegexDesensitizationHandler implements DesensitizationHandler { +public class RegexDesensitizationHandler implements DesensitizationHandler { @Override public String desensitize(String origin, Object... arg) { @@ -13,4 +15,9 @@ public class RegexDesensitizationHandler implements DesensitizationHandler { return origin.replaceAll(regex, replacer); } + @Override + public Object[] getAnnotationArgs(RegexDesensitize anno) { + return new Object[]{anno.regex(), anno.replacer()}; + } + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java index 2d8336d1f..47887da39 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java @@ -1,9 +1,11 @@ package cn.iocoder.yudao.framework.desensitize.handler; +import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; + /** * 滑动脱敏处理器 */ -public class SliderDesensitizationHandler implements DesensitizationHandler { +public class SliderDesensitizationHandler implements DesensitizationHandler { @Override public String desensitize(String origin, Object... arg) { @@ -29,6 +31,11 @@ public class SliderDesensitizationHandler implements DesensitizationHandler { origin.substring(prefixKeep + interval); } + @Override + public Object[] getAnnotationArgs(SliderDesensitize anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } + /** * 根据长度循环构建替换符 * 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 f90475d6d..956be23f3 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 @@ -18,6 +18,7 @@ import com.fasterxml.jackson.databind.ser.ContextualSerializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import java.io.IOException; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; /** @@ -64,7 +65,7 @@ public class StringDesensitizeSerializer extends StdSerializer implement // 滑动处理器 SliderDesensitize sliderDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, SliderDesensitize.class)); if (sliderDesensitize != null) { - value = this.desensitizationHandler.desensitize(value, sliderDesensitize.prefixKeep(), sliderDesensitize.suffixKeep(), sliderDesensitize.replacer()); + value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(sliderDesensitize)); gen.writeString(value); return; } @@ -72,15 +73,24 @@ public class StringDesensitizeSerializer extends StdSerializer implement // 正则处理器 RegexDesensitize regexDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, RegexDesensitize.class)); if (regexDesensitize != null) { - value = this.desensitizationHandler.desensitize(value, regexDesensitize.regex(), regexDesensitize.replacer()); + value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(regexDesensitize)); gen.writeString(value); return; } // 自定义处理器 - Desensitize desensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, Desensitize.class)); - if (desensitize != null) { - value = this.desensitizationHandler.desensitize(value); + Desensitize[] annotations = AnnotationUtil.getCombinationAnnotations(field, Desensitize.class); + if (ArrayUtil.isEmpty(annotations)) { + gen.writeString(value); + return; + } + + for (Annotation annotation : field.getAnnotations()) { + if (AnnotationUtil.hasAnnotation(annotation.annotationType(), Desensitize.class)) { + value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(annotation)); + gen.writeString(value); + return; + } } gen.writeString(value); From c60f8d6da293fadf36ffd7fd0ddbb0793beca99b Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Mon, 16 Jan 2023 21:45:18 +0800 Subject: [PATCH 06/13] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20vo=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constraints/BankCardDesensitize.java | 21 ---------- .../constraints/CarLicenseDesensitize.java | 21 ---------- .../constraints/ChineseNameDesensitize.java | 21 ---------- .../constraints/EmailDesensitize.java | 21 ---------- .../constraints/FixedPhoneDesensitize.java | 21 ---------- .../constraints/IdCardDesensitize.java | 21 ---------- .../constraints/PasswordDesensitize.java | 21 ---------- .../constraints/PhoneNumberDesensitize.java | 21 ---------- .../base}/DesensitizationHandlerHolder.java | 9 ++-- .../base}/annotation/Desensitize.java | 9 ++-- .../base/handler/DesensitizationHandler.java | 19 +++++++++ .../regex/annotation/EmailDesensitize.java | 34 +++++++++++++++ .../regex}/annotation/RegexDesensitize.java | 7 ++-- .../AbstractRegexDesensitizationHandler.java | 25 +++++++++++ .../DefaultRegexDesensitizationHandler.java | 14 +++++++ .../handler/EmailDesensitizationHandler.java | 11 +++++ .../core/slider/annotation/BankCard.java | 41 +++++++++++++++++++ .../core/slider/annotation/CarLicense.java | 41 +++++++++++++++++++ .../core/slider/annotation/ChineseName.java | 41 +++++++++++++++++++ .../core/slider/annotation/FixedPhone.java | 41 +++++++++++++++++++ .../core/slider/annotation/IdCard.java | 41 +++++++++++++++++++ .../core/slider/annotation/Password.java | 41 +++++++++++++++++++ .../core/slider/annotation/PhoneNumber.java | 41 +++++++++++++++++++ .../slider/annotation/Slider.java} | 9 ++-- .../AbstractDesensitizationHandler.java} | 31 +++++++------- .../handler/BankCardDesensitization.java | 11 +++++ .../handler/CarLicenseDesensitization.java | 10 +++++ .../handler/ChineseNameDesensitization.java | 10 +++++ .../DefaultDesensitizationHandler.java | 14 +++++++ .../handler/FixedPhoneDesensitization.java | 10 +++++ .../slider/handler/IdCardDesensitization.java | 10 +++++ .../handler/PasswordDesensitization.java | 10 +++++ .../handler/PhoneNumberDesensitization.java | 11 +++++ .../handler/DesensitizationHandler.java | 28 ------------- .../handler/RegexDesensitizationHandler.java | 23 ----------- .../StringDesensitizeSerializer.java | 29 +++---------- .../handler/DesensitizationHandlerTest.java | 25 ----------- 37 files changed, 518 insertions(+), 296 deletions(-) delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/BankCardDesensitize.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/CarLicenseDesensitize.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/ChineseNameDesensitize.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/EmailDesensitize.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/FixedPhoneDesensitize.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/IdCardDesensitize.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PasswordDesensitize.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PhoneNumberDesensitize.java rename yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/{handler => core/base}/DesensitizationHandlerHolder.java (66%) rename yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/{ => core/base}/annotation/Desensitize.java (71%) create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java rename yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/{ => core/regex}/annotation/RegexDesensitize.java (71%) create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java rename yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/{annotation/SliderDesensitize.java => core/slider/annotation/Slider.java} (70%) rename yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/{handler/SliderDesensitizationHandler.java => core/slider/handler/AbstractDesensitizationHandler.java} (60%) create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerTest.java diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/BankCardDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/BankCardDesensitize.java deleted file mode 100644 index 8fa73169a..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/BankCardDesensitize.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.annotation.constraints; - -import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; -import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 银行卡号 - */ -@Documented -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside -@SliderDesensitize(prefixKeep = 6, suffixKeep = 2, replacer = "*") // 银行卡号;比如:9988002866797031脱敏之后为998800********31 -public @interface BankCardDesensitize { -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/CarLicenseDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/CarLicenseDesensitize.java deleted file mode 100644 index 11f1f5814..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/CarLicenseDesensitize.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.annotation.constraints; - -import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; -import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 车牌号 - */ -@Documented -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside -@SliderDesensitize(prefixKeep = 3, suffixKeep = 1, replacer = "*") // 车牌号;比如:粤A66666脱敏之后为粤A6***6 -public @interface CarLicenseDesensitize { -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/ChineseNameDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/ChineseNameDesensitize.java deleted file mode 100644 index 97d2698ed..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/ChineseNameDesensitize.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.annotation.constraints; - -import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; -import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 中文名 - */ -@Documented -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside -@SliderDesensitize(prefixKeep = 1, suffixKeep = 0, replacer = "*") // 中文名;比如:刘子豪脱敏之后为刘** -public @interface ChineseNameDesensitize { -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/EmailDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/EmailDesensitize.java deleted file mode 100644 index 142e7c0f6..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/EmailDesensitize.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.annotation.constraints; - -import cn.iocoder.yudao.framework.desensitize.annotation.RegexDesensitize; -import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 邮箱 - */ -@Documented -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside -@RegexDesensitize(regex = "(^.)[^@]*(@.*$)", replacer ="$1****$2") // 邮箱;比如:example@gmail.com脱敏之后为e****@gmail.com -public @interface EmailDesensitize { -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/FixedPhoneDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/FixedPhoneDesensitize.java deleted file mode 100644 index f91988735..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/FixedPhoneDesensitize.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.annotation.constraints; - -import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; -import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 固定电话 - */ -@Documented -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside -@SliderDesensitize(prefixKeep = 4, suffixKeep = 2, replacer = "*") // 固定电话;比如:01086551122脱敏之后为0108*****22 -public @interface FixedPhoneDesensitize { -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/IdCardDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/IdCardDesensitize.java deleted file mode 100644 index 921019c5d..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/IdCardDesensitize.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.annotation.constraints; - -import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; -import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 身份证 - */ -@Documented -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside -@SliderDesensitize(prefixKeep = 6, suffixKeep = 2, replacer = "*") // 身份证号码;比如:530321199204074611脱敏之后为530321**********11 -public @interface IdCardDesensitize { -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PasswordDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PasswordDesensitize.java deleted file mode 100644 index 8d823a139..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PasswordDesensitize.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.annotation.constraints; - -import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; -import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 密码 - */ -@Documented -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside -@SliderDesensitize(prefixKeep = 0, suffixKeep = 0, replacer = "*") // 密码;比如:123456脱敏之后为****** -public @interface PasswordDesensitize { -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PhoneNumberDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PhoneNumberDesensitize.java deleted file mode 100644 index 0829c6a45..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/constraints/PhoneNumberDesensitize.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.annotation.constraints; - -import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; -import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 手机号 - */ -@Documented -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside -@SliderDesensitize(prefixKeep = 3, suffixKeep = 4, replacer = "*") // 手机号;比如:13248765917脱敏之后为132****5917 -public @interface PhoneNumberDesensitize { -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerHolder.java similarity index 66% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java rename to yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerHolder.java index 456d2b920..5787eae56 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerHolder.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerHolder.java @@ -1,6 +1,9 @@ -package cn.iocoder.yudao.framework.desensitize.handler; +package cn.iocoder.yudao.framework.desensitize.core.base; import cn.hutool.core.util.ReflectUtil; +import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.core.regex.handler.DefaultRegexDesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.DefaultDesensitizationHandler; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -13,8 +16,8 @@ public class DesensitizationHandlerHolder { * handler 缓存,默认初始化内置的处理器 */ private static final Map, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<>() {{ - put(RegexDesensitizationHandler.class, new RegexDesensitizationHandler()); - put(SliderDesensitizationHandler.class, new SliderDesensitizationHandler()); + put(DefaultRegexDesensitizationHandler.class, new DefaultRegexDesensitizationHandler()); + put(DefaultDesensitizationHandler.class, new DefaultDesensitizationHandler()); }}; public static DesensitizationHandler getDesensitizationHandler(Class clazz) { diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/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 similarity index 71% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/Desensitize.java rename to yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/Desensitize.java index 65dd2108d..888330ebe 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/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 @@ -1,6 +1,6 @@ -package cn.iocoder.yudao.framework.desensitize.annotation; +package cn.iocoder.yudao.framework.desensitize.core.base.annotation; -import cn.iocoder.yudao.framework.desensitize.handler.DesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; import cn.iocoder.yudao.framework.desensitize.serializer.StringDesensitizeSerializer; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.databind.annotation.JsonSerialize; @@ -16,7 +16,7 @@ import java.lang.annotation.Target; * Desensitize 顶级脱敏注解 */ @Documented -@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) +@Target({ ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @JsonSerialize(using = StringDesensitizeSerializer.class) @@ -25,6 +25,5 @@ public @interface Desensitize { /** * 脱敏处理器 */ - Class desensitizationHandler(); - + Class desensitizationBy(); } 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 new file mode 100644 index 000000000..9c10fb0ac --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.framework.desensitize.core.base.handler; + +import java.lang.annotation.Annotation; + +/** + * 脱敏处理器接口 + */ +public interface DesensitizationHandler { + + /** + * 脱敏 + * + * @param origin 原始字符串 + * @param anno 注解信息 + * @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/annotation/EmailDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java new file mode 100644 index 000000000..746e5503b --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java @@ -0,0 +1,34 @@ +package cn.iocoder.yudao.framework.desensitize.core.regex.annotation; + +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.regex.handler.EmailDesensitizationHandler; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 邮箱 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationBy = EmailDesensitizationHandler.class) // 邮箱;比如:example@gmail.com脱敏之后为e****@gmail.com +public @interface EmailDesensitize { + /** + * 匹配的正则表达式(默认匹配所有) + */ + String regex() default "(^.)[^@]*(@.*$)"; + + /** + * 替换规则,会将匹配到的字符串全部替换成 replacer + * 例如:regex=123; replacer=****** + * 原始字符串 123456789 + * 脱敏后字符串 ******456789 + */ + String replacer() default "$1****$2"; +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/RegexDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java similarity index 71% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/RegexDesensitize.java rename to yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java index c2df586c4..7ee6fadd5 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/RegexDesensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java @@ -1,6 +1,7 @@ -package cn.iocoder.yudao.framework.desensitize.annotation; +package cn.iocoder.yudao.framework.desensitize.core.regex.annotation; -import cn.iocoder.yudao.framework.desensitize.handler.RegexDesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.regex.handler.DefaultRegexDesensitizationHandler; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import java.lang.annotation.Documented; @@ -16,7 +17,7 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationHandler = RegexDesensitizationHandler.class) +@Desensitize(desensitizationBy = DefaultRegexDesensitizationHandler.class) public @interface RegexDesensitize { /** * 匹配的正则表达式(默认匹配所有) 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 new file mode 100644 index 000000000..17c86055e --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.desensitize.core.regex.handler; + +import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; + +import java.lang.annotation.Annotation; + +public abstract class AbstractRegexDesensitizationHandler implements DesensitizationHandler { + + @Override + public String desensitize(String origin, T anno) { + Object[] args = getArgs(anno); + String regex = (String) args[0]; + String replacer = (String) args[1]; + + return origin.replaceAll(regex, replacer); + } + + /** + * 获取注解的参数 + * + * @param anno 注解信息 + * @return 注解的参数 + */ + abstract Object[] getArgs(T anno); +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java new file mode 100644 index 000000000..a21734909 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.framework.desensitize.core.regex.handler; + +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.RegexDesensitize; + +/** + * 正则脱敏处理器 + */ +public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler { + + @Override + Object[] getArgs(RegexDesensitize anno) { + return new Object[]{anno.regex(), anno.replacer()}; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java new file mode 100644 index 000000000..9336351da --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java @@ -0,0 +1,11 @@ +package cn.iocoder.yudao.framework.desensitize.core.regex.handler; + +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize; + +public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { + + @Override + Object[] getArgs(EmailDesensitize anno) { + return new Object[]{anno.regex(), anno.replacer()}; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java new file mode 100644 index 000000000..4402ab7d6 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; + +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.BankCardDesensitization; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 银行卡号 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationBy = BankCardDesensitization.class)// 银行卡号;比如:9988002866797031脱敏之后为998800********31 +public @interface BankCard { + + /** + * 前缀保留长度 + */ + int prefixKeep() default 6; + + /** + * 后缀保留长度 + */ + int suffixKeep() default 2; + + /** + * 替换规则,会将前缀后缀保留后,全部替换成 replacer + * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; + * 原始字符串 123456 + * 脱敏后 1***56 + */ + String replacer() default "*"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java new file mode 100644 index 000000000..898c9d4a7 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; + +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.CarLicenseDesensitization; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 车牌号 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationBy = CarLicenseDesensitization.class) // 车牌号;比如:粤A66666脱敏之后为粤A6***6 +public @interface CarLicense { + + /** + * 前缀保留长度 + */ + int prefixKeep() default 3; + + /** + * 后缀保留长度 + */ + int suffixKeep() default 1; + + /** + * 替换规则,会将前缀后缀保留后,全部替换成 replacer + * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; + * 原始字符串 123456 + * 脱敏后 1***56 + */ + String replacer() default "*"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java new file mode 100644 index 000000000..5b7e7e638 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; + +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.ChineseNameDesensitization; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 中文名 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationBy = ChineseNameDesensitization.class) // 中文名;比如:刘子豪脱敏之后为刘** +public @interface ChineseName { + + /** + * 前缀保留长度 + */ + int prefixKeep() default 1; + + /** + * 后缀保留长度 + */ + int suffixKeep() default 0; + + /** + * 替换规则,会将前缀后缀保留后,全部替换成 replacer + * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; + * 原始字符串 123456 + * 脱敏后 1***56 + */ + String replacer() default "*"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java new file mode 100644 index 000000000..7833377c4 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; + +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.FixedPhoneDesensitization; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 固定电话 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationBy = FixedPhoneDesensitization.class) // 固定电话;比如:01086551122脱敏之后为0108*****22 +public @interface FixedPhone { + + /** + * 前缀保留长度 + */ + int prefixKeep() default 4; + + /** + * 后缀保留长度 + */ + int suffixKeep() default 2; + + /** + * 替换规则,会将前缀后缀保留后,全部替换成 replacer + * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; + * 原始字符串 123456 + * 脱敏后 1***56 + */ + String replacer() default "*"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java new file mode 100644 index 000000000..1f8c7a489 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; + +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.IdCardDesensitization; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 身份证 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationBy = IdCardDesensitization.class) // 身份证号码;比如:530321199204074611脱敏之后为530321**********11 +public @interface IdCard { + + /** + * 前缀保留长度 + */ + int prefixKeep() default 6; + + /** + * 后缀保留长度 + */ + int suffixKeep() default 2; + + /** + * 替换规则,会将前缀后缀保留后,全部替换成 replacer + * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; + * 原始字符串 123456 + * 脱敏后 1***56 + */ + String replacer() default "*"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java new file mode 100644 index 000000000..148189ffe --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; + +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PasswordDesensitization; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 密码 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationBy = PasswordDesensitization.class) // 密码;比如:123456脱敏之后为****** +public @interface Password { + + /** + * 前缀保留长度 + */ + int prefixKeep() default 0; + + /** + * 后缀保留长度 + */ + int suffixKeep() default 0; + + /** + * 替换规则,会将前缀后缀保留后,全部替换成 replacer + * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; + * 原始字符串 123456 + * 脱敏后 1***56 + */ + String replacer() default "*"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java new file mode 100644 index 000000000..2ad620c0e --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; + +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PhoneNumberDesensitization; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 手机号 + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@Desensitize(desensitizationBy = PhoneNumberDesensitization.class) // 手机号;比如:13248765917脱敏之后为132****5917 +public @interface PhoneNumber { + + /** + * 前缀保留长度 + */ + int prefixKeep() default 3; + + /** + * 后缀保留长度 + */ + int suffixKeep() default 4; + + /** + * 替换规则,会将前缀后缀保留后,全部替换成 replacer + * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; + * 原始字符串 123456 + * 脱敏后 1***56 + */ + String replacer() default "*"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/SliderDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java similarity index 70% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/SliderDesensitize.java rename to yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java index ebafaa9bc..c752df310 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/annotation/SliderDesensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java @@ -1,6 +1,7 @@ -package cn.iocoder.yudao.framework.desensitize.annotation; +package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; -import cn.iocoder.yudao.framework.desensitize.handler.SliderDesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.handler.DefaultDesensitizationHandler; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import java.lang.annotation.Documented; @@ -16,8 +17,8 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationHandler = SliderDesensitizationHandler.class) -public @interface SliderDesensitize { +@Desensitize(desensitizationBy = DefaultDesensitizationHandler.class) +public @interface Slider { /** * 后缀保留长度 diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java similarity index 60% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java rename to yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java index 47887da39..92b8c0d6c 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/SliderDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java @@ -1,17 +1,17 @@ -package cn.iocoder.yudao.framework.desensitize.handler; +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; -import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; -/** - * 滑动脱敏处理器 - */ -public class SliderDesensitizationHandler implements DesensitizationHandler { +import java.lang.annotation.Annotation; + +public abstract class AbstractDesensitizationHandler implements DesensitizationHandler { @Override - public String desensitize(String origin, Object... arg) { - int prefixKeep = (Integer) arg[0]; - int suffixKeep = (Integer) arg[1]; - String replacer = (String) arg[2]; + public String desensitize(String origin, T anno) { + Object[] args = getArgs(anno); + int prefixKeep = (Integer) args[0]; + int suffixKeep = (Integer) args[1]; + String replacer = (String) args[2]; int length = origin.length(); @@ -31,10 +31,13 @@ public class SliderDesensitizationHandler implements DesensitizationHandler { + + @Override + Object[] getArgs(BankCard anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } +} \ 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/slider/handler/CarLicenseDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java new file mode 100644 index 000000000..d7cef7d2f --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java @@ -0,0 +1,10 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense; + +public class CarLicenseDesensitization extends AbstractDesensitizationHandler { + @Override + Object[] getArgs(CarLicense anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java new file mode 100644 index 000000000..ba0a6607b --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java @@ -0,0 +1,10 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName; + +public class ChineseNameDesensitization extends AbstractDesensitizationHandler { + @Override + Object[] getArgs(ChineseName anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java new file mode 100644 index 000000000..3be33a0b8 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider; + +/** + * 滑动脱敏处理器 + */ +public class DefaultDesensitizationHandler extends AbstractDesensitizationHandler { + + @Override + Object[] getArgs(Slider anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java new file mode 100644 index 000000000..8fb71289f --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java @@ -0,0 +1,10 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone; + +public class FixedPhoneDesensitization extends AbstractDesensitizationHandler { + @Override + Object[] getArgs(FixedPhone anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java new file mode 100644 index 000000000..ce76b357e --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java @@ -0,0 +1,10 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard; + +public class IdCardDesensitization extends AbstractDesensitizationHandler { + @Override + Object[] getArgs(IdCard anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java new file mode 100644 index 000000000..35a656c3d --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java @@ -0,0 +1,10 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password; + +public class PasswordDesensitization extends AbstractDesensitizationHandler { + @Override + Object[] getArgs(Password anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java new file mode 100644 index 000000000..0525cb874 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java @@ -0,0 +1,11 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber; + +public class PhoneNumberDesensitization extends AbstractDesensitizationHandler { + + @Override + Object[] getArgs(PhoneNumber anno) { + return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java deleted file mode 100644 index 74870e771..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandler.java +++ /dev/null @@ -1,28 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.handler; - -import java.lang.annotation.Annotation; - -/** - * 脱敏处理器接口 - */ -public interface DesensitizationHandler { - - /** - * 脱敏 - * - * @param origin 原始字符串 - * @param arg 参数 - * @return 脱敏后的字符串 - */ - String desensitize(String origin, Object... arg); - - /** - * 获取注解参数 - * - * @param anno 注解 - * @return 注解参数 - */ - default Object[] getAnnotationArgs(T anno) { - return new Object[0]; - } -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java deleted file mode 100644 index 57fc71178..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/handler/RegexDesensitizationHandler.java +++ /dev/null @@ -1,23 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.handler; - -import cn.iocoder.yudao.framework.desensitize.annotation.RegexDesensitize; - -/** - * 正则脱敏处理器 - */ -public class RegexDesensitizationHandler implements DesensitizationHandler { - - @Override - public String desensitize(String origin, Object... arg) { - String regex = (String) arg[0]; - String replacer = (String) arg[1]; - - return origin.replaceAll(regex, replacer); - } - - @Override - public Object[] getAnnotationArgs(RegexDesensitize anno) { - return new Object[]{anno.regex(), anno.replacer()}; - } - -} 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 956be23f3..0fbdb04f1 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 @@ -4,11 +4,9 @@ import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; -import cn.iocoder.yudao.framework.desensitize.annotation.Desensitize; -import cn.iocoder.yudao.framework.desensitize.annotation.RegexDesensitize; -import cn.iocoder.yudao.framework.desensitize.annotation.SliderDesensitize; -import cn.iocoder.yudao.framework.desensitize.handler.DesensitizationHandler; -import cn.iocoder.yudao.framework.desensitize.handler.DesensitizationHandlerHolder; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.core.base.DesensitizationHandlerHolder; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.BeanProperty; import com.fasterxml.jackson.databind.JsonMappingException; @@ -46,7 +44,7 @@ public class StringDesensitizeSerializer extends StdSerializer implement return this; } StringDesensitizeSerializer serializer = new StringDesensitizeSerializer(); - serializer.setDesensitizationHandler(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationHandler())); + serializer.setDesensitizationHandler(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationBy())); return serializer; } @@ -62,22 +60,6 @@ public class StringDesensitizeSerializer extends StdSerializer implement Class currentValueClass = currentValue.getClass(); Field field = ReflectUtil.getField(currentValueClass, currentName); - // 滑动处理器 - SliderDesensitize sliderDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, SliderDesensitize.class)); - if (sliderDesensitize != null) { - value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(sliderDesensitize)); - gen.writeString(value); - return; - } - - // 正则处理器 - RegexDesensitize regexDesensitize = ArrayUtil.firstNonNull(AnnotationUtil.getCombinationAnnotations(field, RegexDesensitize.class)); - if (regexDesensitize != null) { - value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(regexDesensitize)); - gen.writeString(value); - return; - } - // 自定义处理器 Desensitize[] annotations = AnnotationUtil.getCombinationAnnotations(field, Desensitize.class); if (ArrayUtil.isEmpty(annotations)) { @@ -86,8 +68,9 @@ public class StringDesensitizeSerializer extends StdSerializer implement } for (Annotation annotation : field.getAnnotations()) { + if (AnnotationUtil.hasAnnotation(annotation.annotationType(), Desensitize.class)) { - value = this.desensitizationHandler.desensitize(value, this.desensitizationHandler.getAnnotationArgs(annotation)); + value = this.desensitizationHandler.desensitize(value, annotation); gen.writeString(value); return; } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerTest.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerTest.java deleted file mode 100644 index 63b4eaabf..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/handler/DesensitizationHandlerTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.handler; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class DesensitizationHandlerTest { - - @Test - public void testSliderDesensitizationHandler() { - DesensitizationHandler handler = DesensitizationHandlerHolder.getDesensitizationHandler(SliderDesensitizationHandler.class); - - Assertions.assertEquals("A****FG", handler.desensitize("ABCDEFG", 1, 2, "*")); - Assertions.assertEquals("芋**码", handler.desensitize("芋道源码", 1, 1, "*")); - Assertions.assertEquals("****", handler.desensitize("芋道源码", 4, 0, "*")); - } - - @Test - public void testRegexDesensitizationHandler() { - DesensitizationHandler handler = DesensitizationHandlerHolder.getDesensitizationHandler(RegexDesensitizationHandler.class); - - Assertions.assertEquals("e****@gmail.com", handler.desensitize("example@gmail.com", "(^.)[^@]*(@.*$)", "$1****$2")); - Assertions.assertEquals("***,铁***", handler.desensitize("他妈的,铁废物", "他妈的|去你大爷|卧槽|草泥马|废物", "***")); - } - -} From e5ab9b71b65c11c7ad300c2b225d98a19348b349 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Mon, 16 Jan 2023 21:50:09 +0800 Subject: [PATCH 07/13] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/regex/annotation/EmailDesensitize.java | 9 +++------ .../desensitize/core/slider/annotation/BankCard.java | 7 ++----- .../desensitize/core/slider/annotation/CarLicense.java | 7 ++----- .../desensitize/core/slider/annotation/ChineseName.java | 7 ++----- .../desensitize/core/slider/annotation/FixedPhone.java | 7 ++----- .../desensitize/core/slider/annotation/IdCard.java | 7 ++----- .../desensitize/core/slider/annotation/Password.java | 7 ++----- .../desensitize/core/slider/annotation/PhoneNumber.java | 7 ++----- 8 files changed, 17 insertions(+), 41 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java index 746e5503b..ebd754b1e 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java @@ -17,18 +17,15 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = EmailDesensitizationHandler.class) // 邮箱;比如:example@gmail.com脱敏之后为e****@gmail.com +@Desensitize(desensitizationBy = EmailDesensitizationHandler.class) public @interface EmailDesensitize { /** - * 匹配的正则表达式(默认匹配所有) + * 匹配的正则表达式 */ String regex() default "(^.)[^@]*(@.*$)"; /** - * 替换规则,会将匹配到的字符串全部替换成 replacer - * 例如:regex=123; replacer=****** - * 原始字符串 123456789 - * 脱敏后字符串 ******456789 + * 替换规则,邮箱;比如:example@gmail.com脱敏之后为e****@gmail.com */ String replacer() default "$1****$2"; } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java index 4402ab7d6..4c41569e8 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java @@ -17,7 +17,7 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = BankCardDesensitization.class)// 银行卡号;比如:9988002866797031脱敏之后为998800********31 +@Desensitize(desensitizationBy = BankCardDesensitization.class) public @interface BankCard { /** @@ -31,10 +31,7 @@ public @interface BankCard { int suffixKeep() default 2; /** - * 替换规则,会将前缀后缀保留后,全部替换成 replacer - * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; - * 原始字符串 123456 - * 脱敏后 1***56 + * 替换规则,银行卡号;比如:9988002866797031脱敏之后为998800********31 */ String replacer() default "*"; diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java index 898c9d4a7..fb89ddb08 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java @@ -17,7 +17,7 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = CarLicenseDesensitization.class) // 车牌号;比如:粤A66666脱敏之后为粤A6***6 +@Desensitize(desensitizationBy = CarLicenseDesensitization.class) public @interface CarLicense { /** @@ -31,10 +31,7 @@ public @interface CarLicense { int suffixKeep() default 1; /** - * 替换规则,会将前缀后缀保留后,全部替换成 replacer - * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; - * 原始字符串 123456 - * 脱敏后 1***56 + * 替换规则,车牌号;比如:粤A66666脱敏之后为粤A6***6 */ String replacer() default "*"; diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java index 5b7e7e638..3b4a1a530 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java @@ -17,7 +17,7 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = ChineseNameDesensitization.class) // 中文名;比如:刘子豪脱敏之后为刘** +@Desensitize(desensitizationBy = ChineseNameDesensitization.class) public @interface ChineseName { /** @@ -31,10 +31,7 @@ public @interface ChineseName { int suffixKeep() default 0; /** - * 替换规则,会将前缀后缀保留后,全部替换成 replacer - * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; - * 原始字符串 123456 - * 脱敏后 1***56 + * 替换规则,中文名;比如:刘子豪脱敏之后为刘** */ String replacer() default "*"; diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java index 7833377c4..663388d89 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java @@ -17,7 +17,7 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = FixedPhoneDesensitization.class) // 固定电话;比如:01086551122脱敏之后为0108*****22 +@Desensitize(desensitizationBy = FixedPhoneDesensitization.class) public @interface FixedPhone { /** @@ -31,10 +31,7 @@ public @interface FixedPhone { int suffixKeep() default 2; /** - * 替换规则,会将前缀后缀保留后,全部替换成 replacer - * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; - * 原始字符串 123456 - * 脱敏后 1***56 + * 替换规则,固定电话;比如:01086551122脱敏之后为0108*****22 */ String replacer() default "*"; diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java index 1f8c7a489..ac2ba9cd3 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java @@ -17,7 +17,7 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = IdCardDesensitization.class) // 身份证号码;比如:530321199204074611脱敏之后为530321**********11 +@Desensitize(desensitizationBy = IdCardDesensitization.class) public @interface IdCard { /** @@ -31,10 +31,7 @@ public @interface IdCard { int suffixKeep() default 2; /** - * 替换规则,会将前缀后缀保留后,全部替换成 replacer - * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; - * 原始字符串 123456 - * 脱敏后 1***56 + * 替换规则,身份证号码;比如:530321199204074611脱敏之后为530321**********11 */ String replacer() default "*"; diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java index 148189ffe..c4bb6e1eb 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java @@ -17,7 +17,7 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = PasswordDesensitization.class) // 密码;比如:123456脱敏之后为****** +@Desensitize(desensitizationBy = PasswordDesensitization.class) public @interface Password { /** @@ -31,10 +31,7 @@ public @interface Password { int suffixKeep() default 0; /** - * 替换规则,会将前缀后缀保留后,全部替换成 replacer - * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; - * 原始字符串 123456 - * 脱敏后 1***56 + * 替换规则,密码;比如:123456脱敏之后为****** */ String replacer() default "*"; diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java index 2ad620c0e..c78354d15 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java @@ -17,7 +17,7 @@ import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = PhoneNumberDesensitization.class) // 手机号;比如:13248765917脱敏之后为132****5917 +@Desensitize(desensitizationBy = PhoneNumberDesensitization.class) public @interface PhoneNumber { /** @@ -31,10 +31,7 @@ public @interface PhoneNumber { int suffixKeep() default 4; /** - * 替换规则,会将前缀后缀保留后,全部替换成 replacer - * 例如:prefixKeep = 1; suffixKeep = 2; replacer = "*"; - * 原始字符串 123456 - * 脱敏后 1***56 + * 替换规则,手机号;比如:13248765917脱敏之后为132****5917 */ String replacer() default "*"; From 32d7d6ab2975f11e8d31ad4f7407a3023c7061df Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 17 Jan 2023 08:11:57 +0800 Subject: [PATCH 08/13] =?UTF-8?q?code=20review=20=E8=84=B1=E6=95=8F?= =?UTF-8?q?=E7=BB=84=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); } From 7b3fca28447d2947136048b5d5052462ffb44d96 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Tue, 17 Jan 2023 10:52:37 +0800 Subject: [PATCH 09/13] =?UTF-8?q?feat:=20=E8=84=B1=E6=95=8F=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-dependencies/pom.xml | 2 +- yudao-framework/pom.xml | 2 +- .../AbstractRegexDesensitizationHandler.java | 27 ---------- .../handler/BankCardDesensitization.java | 11 ---- .../handler/CarLicenseDesensitization.java | 10 ---- .../handler/ChineseNameDesensitization.java | 10 ---- .../DefaultDesensitizationHandler.java | 14 ----- .../handler/FixedPhoneDesensitization.java | 10 ---- .../slider/handler/IdCardDesensitization.java | 10 ---- .../handler/PasswordDesensitization.java | 10 ---- .../handler/PhoneNumberDesensitization.java | 11 ---- .../pom.xml | 12 +---- .../base/DesensitizationHandlerFactory.java} | 15 ++---- .../core/base/annotation/DesensitizeBy.java} | 17 +++---- .../base/handler/DesensitizationHandler.java | 6 +-- .../StringDesensitizeSerializer.java | 51 ++++++++++--------- .../regex/annotation/EmailDesensitize.java | 9 ++-- .../regex/annotation/RegexDesensitize.java | 7 ++- .../AbstractRegexDesensitizationHandler.java | 37 ++++++++++++++ .../DefaultRegexDesensitizationHandler.java | 14 +++-- .../handler/EmailDesensitizationHandler.java | 14 ++++- .../core/slider/annotation/BankCard.java | 6 ++- .../core/slider/annotation/CarLicense.java | 6 ++- .../core/slider/annotation/ChineseName.java | 6 ++- .../core/slider/annotation/FixedPhone.java | 6 ++- .../core/slider/annotation/IdCard.java | 6 ++- .../core/slider/annotation/Password.java | 6 ++- .../core/slider/annotation/PhoneNumber.java | 6 ++- .../core/slider/annotation/Slider.java | 6 ++- .../AbstractDesensitizationHandler.java | 49 +++++++++++++----- .../handler/BankCardDesensitization.java | 27 ++++++++++ .../handler/CarLicenseDesensitization.java | 25 +++++++++ .../handler/ChineseNameDesensitization.java | 25 +++++++++ .../DefaultDesensitizationHandler.java | 25 +++++++++ .../handler/FixedPhoneDesensitization.java | 25 +++++++++ .../slider/handler/IdCardDesensitization.java | 25 +++++++++ .../handler/PasswordDesensitization.java | 25 +++++++++ .../handler/PhoneNumberDesensitization.java | 26 ++++++++++ 38 files changed, 390 insertions(+), 209 deletions(-) delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/pom.xml (70%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerHolder.java => yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java} (60%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/Desensitize.java => yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/DesensitizeBy.java} (50%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java (62%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize => yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base}/serializer/StringDesensitizeSerializer.java (71%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java (85%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java (90%) create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java (54%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java (52%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java (90%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java (90%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java (90%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java (90%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java (91%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java (90%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PhoneNumber.java (90%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java (91%) rename yudao-framework/{yudao-spring-boot-starter-biz-desensitize => yudao-spring-boot-starter-desensitize}/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java (53%) create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index be2f14dd6..f57f7a944 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -143,7 +143,7 @@ cn.iocoder.boot - yudao-spring-boot-starter-biz-desensitize + yudao-spring-boot-starter-desensitize ${revision} diff --git a/yudao-framework/pom.xml b/yudao-framework/pom.xml index 0f20b1a78..12244f5ce 100644 --- a/yudao-framework/pom.xml +++ b/yudao-framework/pom.xml @@ -41,7 +41,7 @@ yudao-spring-boot-starter-flowable yudao-spring-boot-starter-captcha yudao-spring-boot-starter-websocket - yudao-spring-boot-starter-biz-desensitize + yudao-spring-boot-starter-desensitize yudao-framework 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 deleted file mode 100644 index a9d495adf..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.regex.handler; - -import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; - -import java.lang.annotation.Annotation; - -public abstract class AbstractRegexDesensitizationHandler implements DesensitizationHandler { - - @Override - public String desensitize(String origin, T anno) { - Object[] args = getArgs(anno); - String regex = (String) args[0]; - String replacer = (String) args[1]; - - return origin.replaceAll(regex, replacer); - } - - // TODO @城:是不是抽象两个方法,一个拿 regex;一个拿 replacer - - /** - * 获取注解的参数 - * - * @param anno 注解信息 - * @return 注解的参数 - */ - abstract Object[] getArgs(T anno); -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java deleted file mode 100644 index 942ca258f..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java +++ /dev/null @@ -1,11 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; - -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard; - -public class BankCardDesensitization extends AbstractDesensitizationHandler { - - @Override - Object[] getArgs(BankCard anno) { - return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; - } -} \ 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/slider/handler/CarLicenseDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java deleted file mode 100644 index d7cef7d2f..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java +++ /dev/null @@ -1,10 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; - -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense; - -public class CarLicenseDesensitization extends AbstractDesensitizationHandler { - @Override - Object[] getArgs(CarLicense anno) { - return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; - } -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java deleted file mode 100644 index ba0a6607b..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java +++ /dev/null @@ -1,10 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; - -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName; - -public class ChineseNameDesensitization extends AbstractDesensitizationHandler { - @Override - Object[] getArgs(ChineseName anno) { - return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; - } -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java deleted file mode 100644 index 3be33a0b8..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java +++ /dev/null @@ -1,14 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; - -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider; - -/** - * 滑动脱敏处理器 - */ -public class DefaultDesensitizationHandler extends AbstractDesensitizationHandler { - - @Override - Object[] getArgs(Slider anno) { - return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; - } -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java deleted file mode 100644 index 8fb71289f..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java +++ /dev/null @@ -1,10 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; - -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone; - -public class FixedPhoneDesensitization extends AbstractDesensitizationHandler { - @Override - Object[] getArgs(FixedPhone anno) { - return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; - } -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java deleted file mode 100644 index ce76b357e..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java +++ /dev/null @@ -1,10 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; - -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard; - -public class IdCardDesensitization extends AbstractDesensitizationHandler { - @Override - Object[] getArgs(IdCard anno) { - return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; - } -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java deleted file mode 100644 index 35a656c3d..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java +++ /dev/null @@ -1,10 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; - -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password; - -public class PasswordDesensitization extends AbstractDesensitizationHandler { - @Override - Object[] getArgs(Password anno) { - return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; - } -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java b/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java deleted file mode 100644 index 0525cb874..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java +++ /dev/null @@ -1,11 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.slider.handler; - -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber; - -public class PhoneNumberDesensitization extends AbstractDesensitizationHandler { - - @Override - Object[] getArgs(PhoneNumber anno) { - return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()}; - } -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml b/yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml similarity index 70% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml rename to yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml index 1afb387b8..53879bd0f 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml @@ -9,16 +9,8 @@ ${revision} - - yudao-spring-boot-starter-biz-desensitize - - - - - 11 - 11 - UTF-8 - + yudao-spring-boot-starter-desensitize + 脱敏组件 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java similarity index 60% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerHolder.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java index 95c193f6b..abd756ef2 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java @@ -2,33 +2,28 @@ package cn.iocoder.yudao.framework.desensitize.core.base; import cn.hutool.core.util.ReflectUtil; import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; -import cn.iocoder.yudao.framework.desensitize.core.regex.handler.DefaultRegexDesensitizationHandler; -import cn.iocoder.yudao.framework.desensitize.core.slider.handler.DefaultDesensitizationHandler; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -// TODO @城:DesensitizationHandlerHolder =》 DesensitizationHandlerFactory? Holder 没有太多工厂的味道哈 /** * 脱敏处理器 Holder + * + * @author gaibu */ -public class DesensitizationHandlerHolder { +public class DesensitizationHandlerFactory { /** * handler 缓存,默认初始化内置的处理器 */ - private static final Map, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<>() {{ - // TODO @城:这 2 个,是不是不用呀?需要的时候初始化 - put(DefaultRegexDesensitizationHandler.class, new DefaultRegexDesensitizationHandler()); - put(DefaultDesensitizationHandler.class, new DefaultDesensitizationHandler()); - }}; + private static final Map, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap, DesensitizationHandler>(); public static DesensitizationHandler getDesensitizationHandler(Class clazz) { DesensitizationHandler handler = HANDLER_MAP.get(clazz); if (handler != null) { return handler; } - synchronized (DesensitizationHandlerHolder.class) { + synchronized (DesensitizationHandlerFactory.class) { handler = HANDLER_MAP.get(clazz); // 双重校验锁 if (handler != null) { 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/DesensitizeBy.java similarity index 50% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/Desensitize.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/DesensitizeBy.java index b95ccd68d..d06e633a6 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/annotation/DesensitizeBy.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.framework.desensitize.core.base.annotation; import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; -import cn.iocoder.yudao.framework.desensitize.serializer.StringDesensitizeSerializer; +import cn.iocoder.yudao.framework.desensitize.core.base.serializer.StringDesensitizeSerializer; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.databind.annotation.JsonSerialize; @@ -11,21 +11,20 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -// TODO @城:每个接口上,author 写下哈。 -// TODO @城:Desensitize = 》DesensitizeBy -// TODO @城:Desensitize 类注释,方便读者阅读理解 /** - * Desensitize 顶级脱敏注解 + * 顶级脱敏注解,自定义注解需要使用此注解。 + * + * @author gaibu */ @Documented @Target(ElementType.ANNOTATION_TYPE) @Retention(RetentionPolicy.RUNTIME) -@JacksonAnnotationsInside // TODO @城:这个的作用,也可以写下 -@JsonSerialize(using = StringDesensitizeSerializer.class) // TODO @城:这个的作用,也可以写下 -public @interface Desensitize { +@JacksonAnnotationsInside // 此注解是其他所有 jackson 注解的元注解,打上了此注解的注解表明是 jackson 注解的一部分 +@JsonSerialize(using = StringDesensitizeSerializer.class) // 指定序列化器 +public @interface DesensitizeBy { /** * 脱敏处理器 */ - Class desensitizationBy(); // TODO @城:desensitizationBy -> handler + Class 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java similarity index 62% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java index a4dd24665..e59d96cec 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java @@ -10,10 +10,10 @@ public interface DesensitizationHandler { /** * 脱敏 * - * @param origin 原始字符串 - * @param anno 注解信息 // TODO 不要这样的缩写哈,anno -> annotation + * @param origin 原始字符串 + * @param annotation 注解信息 * @return 脱敏后的字符串 */ - String desensitize(String origin, T anno); + String desensitize(String origin, T annotation); } 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java similarity index 71% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/serializer/StringDesensitizeSerializer.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java index accfa77dd..85dee2f53 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java @@ -1,12 +1,12 @@ -package cn.iocoder.yudao.framework.desensitize.serializer; +package cn.iocoder.yudao.framework.desensitize.core.base.serializer; import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; -import cn.iocoder.yudao.framework.desensitize.core.base.DesensitizationHandlerHolder; +import cn.iocoder.yudao.framework.desensitize.core.base.DesensitizationHandlerFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.BeanProperty; import com.fasterxml.jackson.databind.JsonMappingException; @@ -14,40 +14,36 @@ import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.ContextualSerializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import lombok.Getter; +import lombok.Setter; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Field; -// TODO @城:挪到 base/serializer 包下 /** * 脱敏序列化器 + * + * @author gaibu */ public class StringDesensitizeSerializer extends StdSerializer implements ContextualSerializer { + @Getter + @Setter private DesensitizationHandler desensitizationHandler; protected StringDesensitizeSerializer() { super(String.class); } - // TODO @城:get 和 set 方法是必须的么?如果是的话,可以换成 lombok 注解哈,简洁一点~ - public DesensitizationHandler getDesensitizationHandler() { - return desensitizationHandler; - } - - public void setDesensitizationHandler(DesensitizationHandler desensitizationHandler) { - this.desensitizationHandler = desensitizationHandler; - } - @Override public JsonSerializer createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { - Desensitize annotation = beanProperty.getAnnotation(Desensitize.class); + DesensitizeBy annotation = beanProperty.getAnnotation(DesensitizeBy.class); if (annotation == null) { return this; } StringDesensitizeSerializer serializer = new StringDesensitizeSerializer(); - serializer.setDesensitizationHandler(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationBy())); + serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler())); return serializer; } @@ -57,21 +53,17 @@ public class StringDesensitizeSerializer extends StdSerializer implement gen.writeNull(); return; } - - // TODO @城:抽个 private getField 方法。让这个方法的逻辑主干,更清晰 - String currentName = gen.getOutputContext().getCurrentName(); - Object currentValue = gen.getCurrentValue(); - Class currentValueClass = currentValue.getClass(); - Field field = ReflectUtil.getField(currentValueClass, currentName); + // 获取序列化字段 + Field field = getField(gen); // 自定义处理器 - Desensitize[] annotations = AnnotationUtil.getCombinationAnnotations(field, Desensitize.class); + DesensitizeBy[] annotations = AnnotationUtil.getCombinationAnnotations(field, DesensitizeBy.class); if (ArrayUtil.isEmpty(annotations)) { gen.writeString(value); return; } for (Annotation annotation : field.getAnnotations()) { - if (AnnotationUtil.hasAnnotation(annotation.annotationType(), Desensitize.class)) { + if (AnnotationUtil.hasAnnotation(annotation.annotationType(), DesensitizeBy.class)) { value = this.desensitizationHandler.desensitize(value, annotation); gen.writeString(value); return; @@ -80,4 +72,17 @@ public class StringDesensitizeSerializer extends StdSerializer implement gen.writeString(value); } + /** + * 获取字段 + * + * @param gen JsonGenerator + * @return 字段 + */ + private Field getField(JsonGenerator gen) { + String currentName = gen.getOutputContext().getCurrentName(); + Object currentValue = gen.getCurrentValue(); + Class currentValueClass = currentValue.getClass(); + return ReflectUtil.getField(currentValueClass, currentName); + } + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java similarity index 85% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java index ebd754b1e..303320b70 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.regex.handler.EmailDesensitizationHandler; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,17 +12,20 @@ import java.lang.annotation.Target; /** * 邮箱 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = EmailDesensitizationHandler.class) +@DesensitizeBy(handler = EmailDesensitizationHandler.class) public @interface EmailDesensitize { + /** * 匹配的正则表达式 */ - String regex() default "(^.)[^@]*(@.*$)"; + String regex() default "(^.)[^@]*(@.*$)"; /** * 替换规则,邮箱;比如:example@gmail.com脱敏之后为e****@gmail.com diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java similarity index 90% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java index 7ee6fadd5..6c457fd0c 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.regex.handler.DefaultRegexDesensitizationHandler; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,13 +12,16 @@ import java.lang.annotation.Target; /** * 正则脱敏注解 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = DefaultRegexDesensitizationHandler.class) +@DesensitizeBy(handler = DefaultRegexDesensitizationHandler.class) public @interface RegexDesensitize { + /** * 匹配的正则表达式(默认匹配所有) */ 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 new file mode 100644 index 000000000..158c6de3d --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java @@ -0,0 +1,37 @@ +package cn.iocoder.yudao.framework.desensitize.core.regex.handler; + +import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; + +import java.lang.annotation.Annotation; + +/** + * 正则表达式脱敏处理器抽象类,已实现通用的方法 + * + * @author gaibu + */ +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); + } + + /** + * 获取注解上的 regex 参数 + * + * @param annotation 注解信息 + * @return 正则表达式 + */ + abstract String getRegex(T annotation); + + /** + * 获取注解上的 replacer 参数 + * + * @param annotation 注解信息 + * @return 待替换的字符串 + */ + abstract String getReplacer(T annotation); +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-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 similarity index 54% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java index a21734909..b3a788aa9 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-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,12 +3,20 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler; import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.RegexDesensitize; /** - * 正则脱敏处理器 + * 默认正则脱敏处理器 + * + * @author gaibu */ public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler { + @Override - Object[] getArgs(RegexDesensitize anno) { - return new Object[]{anno.regex(), anno.replacer()}; + String getRegex(RegexDesensitize annotation) { + return annotation.regex(); + } + + @Override + String getReplacer(RegexDesensitize annotation) { + return annotation.replacer(); } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-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 similarity index 52% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java index 9336351da..5313381c3 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-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 @@ -2,10 +2,20 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler; import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize; +/** + * 邮箱脱敏处理器 + * + * @author gaibu + */ public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { @Override - Object[] getArgs(EmailDesensitize anno) { - return new Object[]{anno.regex(), anno.replacer()}; + String getRegex(EmailDesensitize annotation) { + return annotation.regex(); + } + + @Override + String getReplacer(EmailDesensitize annotation) { + return annotation.replacer(); } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java similarity index 90% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java index 4c41569e8..762ce3118 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.BankCardDesensitization; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,12 +12,14 @@ import java.lang.annotation.Target; /** * 银行卡号 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = BankCardDesensitization.class) +@DesensitizeBy(handler = BankCardDesensitization.class) public @interface BankCard { /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java similarity index 90% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java index fb89ddb08..d3d09ad42 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.CarLicenseDesensitization; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,12 +12,14 @@ import java.lang.annotation.Target; /** * 车牌号 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = CarLicenseDesensitization.class) +@DesensitizeBy(handler = CarLicenseDesensitization.class) public @interface CarLicense { /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java similarity index 90% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java index 3b4a1a530..92e46e594 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.ChineseNameDesensitization; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,12 +12,14 @@ import java.lang.annotation.Target; /** * 中文名 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = ChineseNameDesensitization.class) +@DesensitizeBy(handler = ChineseNameDesensitization.class) public @interface ChineseName { /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java similarity index 90% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java index 663388d89..867552164 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.FixedPhoneDesensitization; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,12 +12,14 @@ import java.lang.annotation.Target; /** * 固定电话 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = FixedPhoneDesensitization.class) +@DesensitizeBy(handler = FixedPhoneDesensitization.class) public @interface FixedPhone { /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java similarity index 91% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java index ac2ba9cd3..7b976bac6 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.IdCardDesensitization; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,12 +12,14 @@ import java.lang.annotation.Target; /** * 身份证 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = IdCardDesensitization.class) +@DesensitizeBy(handler = IdCardDesensitization.class) public @interface IdCard { /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-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 similarity index 90% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java index c4bb6e1eb..bbe067ab7 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-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 @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PasswordDesensitization; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,12 +12,14 @@ import java.lang.annotation.Target; /** * 密码 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = PasswordDesensitization.class) +@DesensitizeBy(handler = PasswordDesensitization.class) public @interface Password { /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-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/PhoneNumber.java similarity index 90% rename from yudao-framework/yudao-spring-boot-starter-biz-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/PhoneNumber.java index c78354d15..87d7893fe 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-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/PhoneNumber.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PhoneNumberDesensitization; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,12 +12,14 @@ import java.lang.annotation.Target; /** * 手机号 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = PhoneNumberDesensitization.class) +@DesensitizeBy(handler = PhoneNumberDesensitization.class) public @interface PhoneNumber { /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java similarity index 91% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java index c752df310..4d3cac7a3 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; -import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; +import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.DefaultDesensitizationHandler; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; @@ -12,12 +12,14 @@ import java.lang.annotation.Target; /** * 滑动脱敏注解 + * + * @author gaibu */ @Documented @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside -@Desensitize(desensitizationBy = DefaultDesensitizationHandler.class) +@DesensitizeBy(handler = DefaultDesensitizationHandler.class) public @interface Slider { /** 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java similarity index 53% rename from yudao-framework/yudao-spring-boot-starter-biz-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java index 0b25cdbe5..bd5ca926d 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-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractDesensitizationHandler.java @@ -4,14 +4,18 @@ import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationH import java.lang.annotation.Annotation; +/** + * 滑动脱敏处理器抽象类,已实现通用的方法 + * + * @author gaibu + */ public abstract class AbstractDesensitizationHandler implements DesensitizationHandler { @Override - public String desensitize(String origin, T anno) { - Object[] args = getArgs(anno); - int prefixKeep = (Integer) args[0]; - int suffixKeep = (Integer) args[1]; - String replacer = (String) args[2]; + public String desensitize(String origin, T annotation) { + int prefixKeep = getPrefixKeep(annotation); + int suffixKeep = getSuffixKeep(annotation); + String replacer = getReplacer(annotation); int length = origin.length(); // 情况一:原始字符串长度小于等于保留长度,则原始字符串全部替换 @@ -19,26 +23,41 @@ public abstract class AbstractDesensitizationHandler imple 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 @城:类似,子类直接获取到参数哈 /** - * 获取注解的参数 + * 前缀保留长度 * - * @param anno 注解信息 - * @return 注解的参数 + * @param annotation 注解信息 + * @return 前缀保留长度 */ - abstract Object[] getArgs(T anno); + abstract Integer getPrefixKeep(T annotation); + + /** + * 后缀保留长度 + * + * @param annotation 注解信息 + * @return 后缀保留长度 + */ + abstract Integer getSuffixKeep(T annotation); + + /** + * 替换符 + * + * @param annotation 注解信息 + * @return 替换符 + */ + abstract String getReplacer(T annotation); /** * 根据长度循环构建替换符 @@ -48,7 +67,11 @@ public abstract class AbstractDesensitizationHandler imple * @return 构建后的替换符 */ private String buildReplacerByLength(String replacer, int length) { - return String.valueOf(replacer).repeat(Math.max(0, 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 new file mode 100644 index 000000000..76e63a6b1 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java @@ -0,0 +1,27 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard; + +/** + * 银行卡脱敏处理器 + * + * @author gaibu + */ +public class BankCardDesensitization extends AbstractDesensitizationHandler { + + @Override + Integer getPrefixKeep(BankCard annotation) { + return annotation.prefixKeep(); + } + + @Override + Integer getSuffixKeep(BankCard annotation) { + return annotation.suffixKeep(); + } + + @Override + String getReplacer(BankCard annotation) { + return annotation.replacer(); + } + +} \ No newline at end of file diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java new file mode 100644 index 000000000..742851a31 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense; + +/** + * 车牌号脱敏处理器 + * + * @author gaibu + */ +public class CarLicenseDesensitization extends AbstractDesensitizationHandler { + @Override + Integer getPrefixKeep(CarLicense annotation) { + return annotation.prefixKeep(); + } + + @Override + Integer getSuffixKeep(CarLicense annotation) { + return annotation.suffixKeep(); + } + + @Override + String getReplacer(CarLicense 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/ChineseNameDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java new file mode 100644 index 000000000..f7314867d --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName; + +/** + * 中文姓名脱敏处理器 + * + * @author gaibu + */ +public class ChineseNameDesensitization extends AbstractDesensitizationHandler { + @Override + Integer getPrefixKeep(ChineseName annotation) { + return annotation.prefixKeep(); + } + + @Override + Integer getSuffixKeep(ChineseName annotation) { + return annotation.suffixKeep(); + } + + @Override + String getReplacer(ChineseName 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/DefaultDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java new file mode 100644 index 000000000..0c498b071 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider; + +/** + * 滑动脱敏处理器 + * + * @author gaibu + */ +public class DefaultDesensitizationHandler extends AbstractDesensitizationHandler { + @Override + Integer getPrefixKeep(Slider annotation) { + return annotation.prefixKeep(); + } + + @Override + Integer getSuffixKeep(Slider annotation) { + return annotation.suffixKeep(); + } + + @Override + String getReplacer(Slider 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/FixedPhoneDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java new file mode 100644 index 000000000..50672ce35 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone; + +/** + * 固定电话脱敏处理器 + * + * @author gaibu + */ +public class FixedPhoneDesensitization extends AbstractDesensitizationHandler { + @Override + Integer getPrefixKeep(FixedPhone annotation) { + return annotation.prefixKeep(); + } + + @Override + Integer getSuffixKeep(FixedPhone annotation) { + return annotation.suffixKeep(); + } + + @Override + String getReplacer(FixedPhone 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/IdCardDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java new file mode 100644 index 000000000..657a19129 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard; + +/** + * 身份证脱敏处理器 + * + * @author gaibu + */ +public class IdCardDesensitization extends AbstractDesensitizationHandler { + @Override + Integer getPrefixKeep(IdCard annotation) { + return annotation.prefixKeep(); + } + + @Override + Integer getSuffixKeep(IdCard annotation) { + return annotation.suffixKeep(); + } + + @Override + String getReplacer(IdCard 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 new file mode 100644 index 000000000..d3561ce5d --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password; + +/** + * 密码脱敏处理器 + * + * @author gaibu + */ +public class PasswordDesensitization extends AbstractDesensitizationHandler { + @Override + Integer getPrefixKeep(Password annotation) { + return annotation.prefixKeep(); + } + + @Override + Integer getSuffixKeep(Password annotation) { + return annotation.suffixKeep(); + } + + @Override + String getReplacer(Password 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/PhoneNumberDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java new file mode 100644 index 000000000..e6c11bd44 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PhoneNumberDesensitization.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber; + +/** + * 手机号脱敏处理器 + * + * @author gaibu + */ +public class PhoneNumberDesensitization extends AbstractDesensitizationHandler { + + @Override + Integer getPrefixKeep(PhoneNumber annotation) { + return annotation.prefixKeep(); + } + + @Override + Integer getSuffixKeep(PhoneNumber annotation) { + return annotation.suffixKeep(); + } + + @Override + String getReplacer(PhoneNumber annotation) { + return annotation.replacer(); + } +} From 42bc0d1519783e877c3524c5296c31fab61b63a0 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Tue, 17 Jan 2023 11:28:33 +0800 Subject: [PATCH 10/13] =?UTF-8?q?feat:=20=E8=84=B1=E6=95=8F=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{EmailDesensitize.java => Email.java} | 2 +- .../{RegexDesensitize.java => Regex.java} | 2 +- .../DefaultRegexDesensitizationHandler.java | 8 +- .../handler/EmailDesensitizationHandler.java | 8 +- .../core/slider/DesensitizeTest.java | 88 +++++++++++++++++++ .../core/slider/annotation/Address.java | 27 ++++++ .../core/slider/handler/AddressHandler.java | 11 +++ 7 files changed, 136 insertions(+), 10 deletions(-) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/{EmailDesensitize.java => Email.java} (96%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/{RegexDesensitize.java => Regex.java} (96%) create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java index 303320b70..02d7043fe 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = EmailDesensitizationHandler.class) -public @interface EmailDesensitize { +public @interface Email { /** * 匹配的正则表达式 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java index 6c457fd0c..e5d2558fa 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = DefaultRegexDesensitizationHandler.class) -public @interface RegexDesensitize { +public @interface Regex { /** * 匹配的正则表达式(默认匹配所有) 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 b3a788aa9..9fa5d80d7 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 @@ -1,22 +1,22 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler; -import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.RegexDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex; /** * 默认正则脱敏处理器 * * @author gaibu */ -public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler { +public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler { @Override - String getRegex(RegexDesensitize annotation) { + String getRegex(Regex annotation) { return annotation.regex(); } @Override - String getReplacer(RegexDesensitize annotation) { + String getReplacer(Regex annotation) { return annotation.replacer(); } } 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 5313381c3..b6692d448 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java @@ -1,21 +1,21 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler; -import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email; /** * 邮箱脱敏处理器 * * @author gaibu */ -public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { +public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { @Override - String getRegex(EmailDesensitize annotation) { + String getRegex(Email annotation) { return annotation.regex(); } @Override - String getReplacer(EmailDesensitize annotation) { + String getReplacer(Email annotation) { return annotation.replacer(); } } 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/slider/DesensitizeTest.java new file mode 100644 index 000000000..03db9cb72 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java @@ -0,0 +1,88 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider; + +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.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.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; + +public class DesensitizeTest extends BaseMockitoUnitTest { + + @Test + public void test() { + DesensitizeDemo desensitizeDemo = new DesensitizeDemo(); + desensitizeDemo.setUserName("芋道源码"); + desensitizeDemo.setBankCard("9988002866797031"); + desensitizeDemo.setCarLicense("粤A66666"); + desensitizeDemo.setFixedPhone("01086551122"); + desensitizeDemo.setIdCard("530321199204074611"); + desensitizeDemo.setPassword("123456"); + desensitizeDemo.setPhoneNumber("13248765917"); + desensitizeDemo.setSlider1("ABCDEFG"); + desensitizeDemo.setSlider2("ABCDEFG"); + desensitizeDemo.setSlider3("ABCDEFG"); + desensitizeDemo.setEmail("1@eamil.com"); + desensitizeDemo.setRegex("你好,我是芋道源码"); + desensitizeDemo.setAddress("北京市海淀区上地十街10号"); + desensitizeDemo.setOrigin("芋道源码"); + + DesensitizeDemo d = JsonUtils.parseObject(JsonUtils.toJsonString(desensitizeDemo), DesensitizeDemo.class); + assertEquals("芋***", d.getUserName()); + assertEquals("998800********31", d.getBankCard()); + assertEquals("粤A6***6", d.getCarLicense()); + assertEquals("0108*****22", d.getFixedPhone()); + assertEquals("530321**********11", d.getIdCard()); + assertEquals("******", d.getPassword()); + assertEquals("132****5917", d.getPhoneNumber()); + assertEquals("#######", d.getSlider1()); + assertEquals("ABC*EFG", d.getSlider2()); + assertEquals("*******", d.getSlider3()); + assertEquals("1****@eamil.com", d.getEmail()); + assertEquals("你好,我是*", d.getRegex()); + assertEquals("北京市海淀区上地十街10号*", d.getAddress()); + assertEquals("芋道源码", d.getOrigin()); + } + + @Data + public static class DesensitizeDemo { + @ChineseName + private String userName; + @BankCard + private String bankCard; + @CarLicense + private String carLicense; + @FixedPhone + private String fixedPhone; + @IdCard + private String idCard; + @Password + private String password; + @PhoneNumber + private String phoneNumber; + @Slider(prefixKeep = 6,suffixKeep = 1,replacer = "#") + private String slider1; + @Slider(prefixKeep = 3,suffixKeep = 3) + private String slider2; + @Slider(prefixKeep = 10) + private String slider3; + @Email + private String email; + @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/slider/annotation/Address.java new file mode 100644 index 000000000..0cd338e1f --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java @@ -0,0 +1,27 @@ +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.AddressHandler; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 地址 + * + * @author gaibu + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@DesensitizeBy(handler = AddressHandler.class) +public @interface Address { + + String replacer() default "*"; + +} 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/slider/handler/AddressHandler.java new file mode 100644 index 000000000..ef1a2bbf9 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java @@ -0,0 +1,11 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Address; + +public class AddressHandler implements DesensitizationHandler
{ + @Override + public String desensitize(String origin, Address annotation) { + return origin + annotation.replacer(); + } +} From 6593ec4214ba27b87e890b759cc1b0a4db2e4236 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 19 Jan 2023 10:25:43 +0800 Subject: [PATCH 11/13] =?UTF-8?q?code=20review=20=E8=84=B1=E6=95=8F?= =?UTF-8?q?=E7=BB=84=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 | 2 +- .../base/DesensitizationHandlerFactory.java | 4 +++ .../core/base/annotation/DesensitizeBy.java | 4 ++- .../base/handler/DesensitizationHandler.java | 2 ++ .../StringDesensitizeSerializer.java | 15 +++++--- .../desensitize/core/package-info.java | 4 +++ .../core/regex/annotation/Email.java | 7 ++-- .../core/regex/annotation/Regex.java | 1 + .../AbstractRegexDesensitizationHandler.java | 5 +-- .../DefaultRegexDesensitizationHandler.java | 3 +- .../handler/EmailDesensitizationHandler.java | 3 +- .../{PhoneNumber.java => Mobile.java} | 6 ++-- .../core/slider/annotation/Password.java | 4 ++- .../AbstractDesensitizationHandler.java | 33 ++++++++--------- .../handler/BankCardDesensitization.java | 4 +-- .../handler/CarLicenseDesensitization.java | 2 +- .../handler/ChineseNameDesensitization.java | 4 ++- .../DefaultDesensitizationHandler.java | 2 +- .../handler/FixedPhoneDesensitization.java | 2 +- .../slider/handler/IdCardDesensitization.java | 2 +- ...zation.java => MobileDesensitization.java} | 12 +++---- .../handler/PasswordDesensitization.java | 2 +- .../core/{slider => }/DesensitizeTest.java | 36 ++++++++++++------- .../core/{slider => }/annotation/Address.java | 7 ++-- .../{slider => }/handler/AddressHandler.java | 12 +++++-- .../iocoder/yudao/framework/package-info.java | 3 ++ 26 files changed, 116 insertions(+), 65 deletions(-) create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/package-info.java rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/{PhoneNumber.java => Mobile.java} (88%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/{PhoneNumberDesensitization.java => MobileDesensitization.java} (54%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/{slider => }/DesensitizeTest.java (81%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/{slider => }/annotation/Address.java (70%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/{slider => }/handler/AddressHandler.java (50%) diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml b/yudao-framework/yudao-spring-boot-starter-desensitize/pom.xml index 53879bd0f..c4a96ebe1 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 abd756ef2..4c75cd957 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 d06e633a6..0166cb6ef 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 e59d96cec..470a0becf 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 85dee2f53..3f85f88bd 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 000000000..d56282535 --- /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 02d7043fe..686367c2f 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 e5d2558fa..d0fddea6a 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 158c6de3d..f43431b1d 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 9fa5d80d7..718380570 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 b6692d448..ff77755e9 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 87d7893fe..ca2f4f041 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 bbe067ab7..f96e13b21 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 bd5ca926d..e41c57319 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 76e63a6b1..ca2e00c89 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 d3561ce5d..26a7de496 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 03db9cb72..18f4faee2 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 0cd338e1f..735d25b34 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 ef1a2bbf9..7a8455f8d 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 8c69219d0..2dc531692 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; From 2aa208f44139270604724f2c505198829be1e2d8 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 19 Jan 2023 10:35:54 +0800 Subject: [PATCH 12/13] =?UTF-8?q?feat:=20=E8=84=B1=E6=95=8F=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/DesensitizationHandlerFactory.java | 42 ------------------- .../StringDesensitizeSerializer.java | 5 +-- .../{Email.java => EmailDesensitize.java} | 3 +- .../handler/EmailDesensitizationHandler.java | 10 ++--- .../desensitize/core/DesensitizeTest.java | 4 +- 5 files changed, 10 insertions(+), 54 deletions(-) delete mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/{Email.java => EmailDesensitize.java} (87%) diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java deleted file mode 100644 index 4c75cd957..000000000 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/DesensitizationHandlerFactory.java +++ /dev/null @@ -1,42 +0,0 @@ -package cn.iocoder.yudao.framework.desensitize.core.base; - -import cn.hutool.Hutool; -import cn.hutool.core.lang.Singleton; -import cn.hutool.core.util.ReflectUtil; -import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * 脱敏处理器 Holder - * - * @author gaibu - */ -public class DesensitizationHandlerFactory { - - /** - * handler 缓存,默认初始化内置的处理器 - */ - private static final Map, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap, DesensitizationHandler>(); - - // TODO @唐:可以考虑,使用 hutool 提供的 Singleton.get() - public static DesensitizationHandler getDesensitizationHandler(Class clazz) { - DesensitizationHandler handler = HANDLER_MAP.get(clazz); - if (handler != null) { - return handler; - } - // 不存在,则进行创建 - synchronized (DesensitizationHandlerFactory.class) { - handler = HANDLER_MAP.get(clazz); - // 双重校验锁 - if (handler != null) { - return handler; - } - handler = ReflectUtil.newInstanceIfPossible(clazz); - HANDLER_MAP.put(clazz, handler); - } - return handler; - } - -} diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java index 3f85f88bd..2c15a747d 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/serializer/StringDesensitizeSerializer.java @@ -1,15 +1,14 @@ package cn.iocoder.yudao.framework.desensitize.core.base.serializer; import cn.hutool.core.annotation.AnnotationUtil; +import cn.hutool.core.lang.Singleton; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy; import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; -import cn.iocoder.yudao.framework.desensitize.core.base.DesensitizationHandlerFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.BeanProperty; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.ContextualSerializer; @@ -47,7 +46,7 @@ public class StringDesensitizeSerializer extends StdSerializer implement } // 创建一个 StringDesensitizeSerializer 对象,使用 DesensitizeBy 对应的处理器 StringDesensitizeSerializer serializer = new StringDesensitizeSerializer(); - serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler())); + serializer.setDesensitizationHandler(Singleton.get(annotation.handler())); return serializer; } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java similarity index 87% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java index 686367c2f..2e7ae3498 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java @@ -10,7 +10,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -// TODO @唐:名字还是 Email=》EmailDesensitize 合适一点,避免和 Validator 的注解有点冲突 /** * 邮箱脱敏注解 * @@ -21,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = EmailDesensitizationHandler.class) -public @interface Email { +public @interface EmailDesensitize { /** * 匹配的正则表达式 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java index ff77755e9..8d1867a64 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java @@ -1,21 +1,21 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler; -import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email; +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize; /** - * {@link Email} 的脱敏处理器 + * {@link EmailDesensitize} 的脱敏处理器 * * @author gaibu */ -public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { +public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { @Override - String getRegex(Email annotation) { + String getRegex(EmailDesensitize annotation) { return annotation.regex(); } @Override - String getReplacer(Email annotation) { + String getReplacer(EmailDesensitize annotation) { return annotation.replacer(); } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java index 18f4faee2..400943833 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.framework.desensitize.core; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; -import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email; +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize; import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex; import cn.iocoder.yudao.framework.desensitize.core.annotation.Address; import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard; @@ -85,7 +85,7 @@ public class DesensitizeTest extends BaseMockitoUnitTest { private String slider2; @Slider(prefixKeep = 10) private String slider3; - @Email + @EmailDesensitize private String email; @Regex(regex = "芋道源码", replacer = "*") private String regex; From 729ec31dab4144ec70be764363213e4d50c0d8c2 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 19 Jan 2023 20:16:03 +0800 Subject: [PATCH 13/13] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E8=84=B1?= =?UTF-8?q?=E6=95=8F=E6=B3=A8=E8=A7=A3=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{Regex.java => RegexDesensitize.java} | 2 +- .../DefaultRegexDesensitizationHandler.java | 10 ++--- ...BankCard.java => BankCardDesensitize.java} | 2 +- ...icense.java => CarLicenseDesensitize.java} | 2 +- ...eName.java => ChineseNameDesensitize.java} | 2 +- ...dPhone.java => FixedPhoneDesensitize.java} | 2 +- .../{IdCard.java => IdCardDesensitize.java} | 2 +- .../{Mobile.java => MobileDesensitize.java} | 2 +- ...Password.java => PasswordDesensitize.java} | 2 +- .../{Slider.java => SliderDesensitize.java} | 2 +- .../handler/BankCardDesensitization.java | 12 +++--- .../handler/CarLicenseDesensitization.java | 12 +++--- .../handler/ChineseNameDesensitization.java | 12 +++--- .../DefaultDesensitizationHandler.java | 12 +++--- .../handler/FixedPhoneDesensitization.java | 12 +++--- .../slider/handler/IdCardDesensitization.java | 12 +++--- .../slider/handler/MobileDesensitization.java | 12 +++--- .../handler/PasswordDesensitization.java | 12 +++--- .../desensitize/core/DesensitizeTest.java | 40 +++++++++---------- 19 files changed, 82 insertions(+), 82 deletions(-) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/{Regex.java => RegexDesensitize.java} (96%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/{BankCard.java => BankCardDesensitize.java} (96%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/{CarLicense.java => CarLicenseDesensitize.java} (95%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/{ChineseName.java => ChineseNameDesensitize.java} (95%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/{FixedPhone.java => FixedPhoneDesensitize.java} (96%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/{IdCard.java => IdCardDesensitize.java} (96%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/{Mobile.java => MobileDesensitize.java} (96%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/{Password.java => PasswordDesensitize.java} (96%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/{Slider.java => SliderDesensitize.java} (96%) 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/RegexDesensitize.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java index d0fddea6a..4ab7c7415 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/RegexDesensitize.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = DefaultRegexDesensitizationHandler.class) -public @interface Regex { +public @interface RegexDesensitize { /** * 匹配的正则表达式(默认匹配所有) 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 718380570..f92414e0c 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 @@ -1,21 +1,21 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler; -import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex; +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.RegexDesensitize; /** - * {@link Regex} 的正则脱敏处理器 + * {@link RegexDesensitize} 的正则脱敏处理器 * * @author gaibu */ -public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler { +public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler { @Override - String getRegex(Regex annotation) { + String getRegex(RegexDesensitize annotation) { return annotation.regex(); } @Override - String getReplacer(Regex annotation) { + String getReplacer(RegexDesensitize 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/BankCard.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCardDesensitize.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCardDesensitize.java index 762ce3118..ca04b2b13 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCard.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCardDesensitize.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = BankCardDesensitization.class) -public @interface BankCard { +public @interface BankCardDesensitize { /** * 前缀保留长度 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicenseDesensitize.java similarity index 95% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicenseDesensitize.java index d3d09ad42..82a99db20 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicense.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicenseDesensitize.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = CarLicenseDesensitization.class) -public @interface CarLicense { +public @interface CarLicenseDesensitize { /** * 前缀保留长度 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseNameDesensitize.java similarity index 95% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseNameDesensitize.java index 92e46e594..73a0d0ee5 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseName.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseNameDesensitize.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = ChineseNameDesensitization.class) -public @interface ChineseName { +public @interface ChineseNameDesensitize { /** * 前缀保留长度 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhoneDesensitize.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhoneDesensitize.java index 867552164..e847954ca 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhone.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhoneDesensitize.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = FixedPhoneDesensitization.class) -public @interface FixedPhone { +public @interface FixedPhoneDesensitize { /** * 前缀保留长度 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCardDesensitize.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCardDesensitize.java index 7b976bac6..05fb73ab9 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCard.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCardDesensitize.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = IdCardDesensitization.class) -public @interface IdCard { +public @interface IdCardDesensitize { /** * 前缀保留长度 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Mobile.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/MobileDesensitize.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Mobile.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/MobileDesensitize.java index ca2f4f041..90dbcacc7 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Mobile.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/MobileDesensitize.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = MobileDesensitization.class) -public @interface Mobile { +public @interface MobileDesensitize { /** * 前缀保留长度 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/PasswordDesensitize.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Password.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PasswordDesensitize.java index f96e13b21..99ca5d12c 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/PasswordDesensitize.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = PasswordDesensitization.class) -public @interface Password { +public @interface PasswordDesensitize { /** * 前缀保留长度 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/SliderDesensitize.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/SliderDesensitize.java index 4d3cac7a3..e13e9c47c 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Slider.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/SliderDesensitize.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = DefaultDesensitizationHandler.class) -public @interface Slider { +public @interface SliderDesensitize { /** * 后缀保留长度 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 ca2e00c89..1447f1a38 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 @@ -1,26 +1,26 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCardDesensitize; /** - * {@link BankCard} 的脱敏处理器 + * {@link BankCardDesensitize} 的脱敏处理器 * * @author gaibu */ -public class BankCardDesensitization extends AbstractDesensitizationHandler { +public class BankCardDesensitization extends AbstractDesensitizationHandler { @Override - Integer getPrefixKeep(BankCard annotation) { + Integer getPrefixKeep(BankCardDesensitize annotation) { return annotation.prefixKeep(); } @Override - Integer getSuffixKeep(BankCard annotation) { + Integer getSuffixKeep(BankCardDesensitize annotation) { return annotation.suffixKeep(); } @Override - String getReplacer(BankCard annotation) { + String getReplacer(BankCardDesensitize 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/CarLicenseDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java index eafe634b7..3b10ef7ef 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java @@ -1,25 +1,25 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicenseDesensitize; /** - * {@link CarLicense} 的脱敏处理器 + * {@link CarLicenseDesensitize} 的脱敏处理器 * * @author gaibu */ -public class CarLicenseDesensitization extends AbstractDesensitizationHandler { +public class CarLicenseDesensitization extends AbstractDesensitizationHandler { @Override - Integer getPrefixKeep(CarLicense annotation) { + Integer getPrefixKeep(CarLicenseDesensitize annotation) { return annotation.prefixKeep(); } @Override - Integer getSuffixKeep(CarLicense annotation) { + Integer getSuffixKeep(CarLicenseDesensitize annotation) { return annotation.suffixKeep(); } @Override - String getReplacer(CarLicense annotation) { + String getReplacer(CarLicenseDesensitize 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/ChineseNameDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java index 050843033..f75984dfa 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java @@ -1,26 +1,26 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseNameDesensitize; /** - * {@link ChineseName} 的脱敏处理器 + * {@link ChineseNameDesensitize} 的脱敏处理器 * * @author gaibu */ -public class ChineseNameDesensitization extends AbstractDesensitizationHandler { +public class ChineseNameDesensitization extends AbstractDesensitizationHandler { @Override - Integer getPrefixKeep(ChineseName annotation) { + Integer getPrefixKeep(ChineseNameDesensitize annotation) { return annotation.prefixKeep(); } @Override - Integer getSuffixKeep(ChineseName annotation) { + Integer getSuffixKeep(ChineseNameDesensitize annotation) { return annotation.suffixKeep(); } @Override - String getReplacer(ChineseName annotation) { + String getReplacer(ChineseNameDesensitize 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/DefaultDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java index 187a22f11..1a114624b 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java @@ -1,25 +1,25 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.SliderDesensitize; /** - * {@link Slider} 的脱敏处理器 + * {@link SliderDesensitize} 的脱敏处理器 * * @author gaibu */ -public class DefaultDesensitizationHandler extends AbstractDesensitizationHandler { +public class DefaultDesensitizationHandler extends AbstractDesensitizationHandler { @Override - Integer getPrefixKeep(Slider annotation) { + Integer getPrefixKeep(SliderDesensitize annotation) { return annotation.prefixKeep(); } @Override - Integer getSuffixKeep(Slider annotation) { + Integer getSuffixKeep(SliderDesensitize annotation) { return annotation.suffixKeep(); } @Override - String getReplacer(Slider annotation) { + String getReplacer(SliderDesensitize 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/FixedPhoneDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java index 69b07cadf..c617e46d3 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java @@ -1,25 +1,25 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhoneDesensitize; /** - * {@link FixedPhone} 的脱敏处理器 + * {@link FixedPhoneDesensitize} 的脱敏处理器 * * @author gaibu */ -public class FixedPhoneDesensitization extends AbstractDesensitizationHandler { +public class FixedPhoneDesensitization extends AbstractDesensitizationHandler { @Override - Integer getPrefixKeep(FixedPhone annotation) { + Integer getPrefixKeep(FixedPhoneDesensitize annotation) { return annotation.prefixKeep(); } @Override - Integer getSuffixKeep(FixedPhone annotation) { + Integer getSuffixKeep(FixedPhoneDesensitize annotation) { return annotation.suffixKeep(); } @Override - String getReplacer(FixedPhone annotation) { + String getReplacer(FixedPhoneDesensitize 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/IdCardDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java index dccf3e834..35db2ccd9 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java @@ -1,25 +1,25 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCardDesensitize; /** - * {@link IdCard} 的脱敏处理器 + * {@link IdCardDesensitize} 的脱敏处理器 * * @author gaibu */ -public class IdCardDesensitization extends AbstractDesensitizationHandler { +public class IdCardDesensitization extends AbstractDesensitizationHandler { @Override - Integer getPrefixKeep(IdCard annotation) { + Integer getPrefixKeep(IdCardDesensitize annotation) { return annotation.prefixKeep(); } @Override - Integer getSuffixKeep(IdCard annotation) { + Integer getSuffixKeep(IdCardDesensitize annotation) { return annotation.suffixKeep(); } @Override - String getReplacer(IdCard annotation) { + String getReplacer(IdCardDesensitize 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/MobileDesensitization.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/MobileDesensitization.java index e5ddb49c2..6c31606c5 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/MobileDesensitization.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/MobileDesensitization.java @@ -1,26 +1,26 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Mobile; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.MobileDesensitize; /** - * {@link Mobile} 的脱敏处理器 + * {@link MobileDesensitize} 的脱敏处理器 * * @author gaibu */ -public class MobileDesensitization extends AbstractDesensitizationHandler { +public class MobileDesensitization extends AbstractDesensitizationHandler { @Override - Integer getPrefixKeep(Mobile annotation) { + Integer getPrefixKeep(MobileDesensitize annotation) { return annotation.prefixKeep(); } @Override - Integer getSuffixKeep(Mobile annotation) { + Integer getSuffixKeep(MobileDesensitize annotation) { return annotation.suffixKeep(); } @Override - String getReplacer(Mobile annotation) { + String getReplacer(MobileDesensitize 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 26a7de496..16013f87b 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 @@ -1,25 +1,25 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PasswordDesensitize; /** - * {@link Password} 的码脱敏处理器 + * {@link PasswordDesensitize} 的码脱敏处理器 * * @author gaibu */ -public class PasswordDesensitization extends AbstractDesensitizationHandler { +public class PasswordDesensitization extends AbstractDesensitizationHandler { @Override - Integer getPrefixKeep(Password annotation) { + Integer getPrefixKeep(PasswordDesensitize annotation) { return annotation.prefixKeep(); } @Override - Integer getSuffixKeep(Password annotation) { + Integer getSuffixKeep(PasswordDesensitize annotation) { return annotation.suffixKeep(); } @Override - String getReplacer(Password annotation) { + String getReplacer(PasswordDesensitize annotation) { return annotation.replacer(); } } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java index 400943833..c308a0eb5 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/DesensitizeTest.java @@ -2,16 +2,16 @@ 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.EmailDesensitize; -import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex; +import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.RegexDesensitize; 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.Mobile; -import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCardDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicenseDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseNameDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhoneDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCardDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PasswordDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.MobileDesensitize; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.SliderDesensitize; import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; import lombok.Data; import org.junit.jupiter.api.Test; @@ -65,29 +65,29 @@ public class DesensitizeTest extends BaseMockitoUnitTest { @Data public static class DesensitizeDemo { - @ChineseName + @ChineseNameDesensitize private String nickname; - @BankCard + @BankCardDesensitize private String bankCard; - @CarLicense + @CarLicenseDesensitize private String carLicense; - @FixedPhone + @FixedPhoneDesensitize private String fixedPhone; - @IdCard + @IdCardDesensitize private String idCard; - @Password + @PasswordDesensitize private String password; - @Mobile + @MobileDesensitize private String phoneNumber; - @Slider(prefixKeep = 6, suffixKeep = 1, replacer = "#") + @SliderDesensitize(prefixKeep = 6, suffixKeep = 1, replacer = "#") private String slider1; - @Slider(prefixKeep = 3, suffixKeep = 3) + @SliderDesensitize(prefixKeep = 3, suffixKeep = 3) private String slider2; - @Slider(prefixKeep = 10) + @SliderDesensitize(prefixKeep = 10) private String slider3; @EmailDesensitize private String email; - @Regex(regex = "芋道源码", replacer = "*") + @RegexDesensitize(regex = "芋道源码", replacer = "*") private String regex; @Address private String address;