feat: 支持 vo 返回的脱敏

This commit is contained in:
gaibu 2023-01-10 15:10:16 +08:00
parent bbb27df5e9
commit e637bff8cd
7 changed files with 173 additions and 0 deletions

View File

@ -41,6 +41,7 @@
<module>yudao-spring-boot-starter-flowable</module>
<module>yudao-spring-boot-starter-captcha</module>
<module>yudao-spring-boot-starter-websocket</module>
<module>yudao-spring-boot-starter-biz-desensitize</module>
</modules>
<artifactId>yudao-framework</artifactId>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-framework</artifactId>
<version>1.6.6-snapshot</version>
</parent>
<artifactId>yudao-spring-boot-starter-biz-desensitize</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-common</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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<? extends DesensitizationHandler> handler();
}

View File

@ -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 = "****";
}

View File

@ -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;
}

View File

@ -0,0 +1,10 @@
package cn.iocoder.yudao.framework.desensitize.handler;
public class DefaultDesensitizationHandler implements DesensitizationHandler {
@Override
public String handle(String origin) {
return origin;
}
}

View File

@ -0,0 +1,13 @@
package cn.iocoder.yudao.framework.desensitize.handler;
public interface DesensitizationHandler {
/**
* 脱敏
*
* @param origin 原始字符串
* @return 脱敏后的字符串
*/
String handle(String origin);
}