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] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20vo=20=E8=BF=94?= =?UTF-8?q?=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); + +}