2022-01-21 15:06:26 +08:00
|
|
|
|
# 数据脱敏工具
|
2022-01-21 11:54:13 +08:00
|
|
|
|
|
2022-01-21 15:06:26 +08:00
|
|
|
|
#### 一、介绍
|
|
|
|
|
1. 支持13种类型数据的脱敏,例如:邮箱地址、手机号、身份证、银行卡、车牌号、姓名、家庭地址、ip地址、生日、密码等
|
|
|
|
|
2. 配置灵活,同时支持全局脱敏和局部脱敏
|
|
|
|
|
3. 任意对象都可以被脱敏
|
|
|
|
|
4. 支持jsonpath表达式,可灵活控制同一个对象不同层级关系时既要脱敏又要不脱敏的场景
|
|
|
|
|
---
|
|
|
|
|
#### 二、软件架构
|
2022-01-21 11:54:13 +08:00
|
|
|
|
|
2022-01-21 15:06:26 +08:00
|
|
|
|
待补充
|
2022-01-21 11:54:13 +08:00
|
|
|
|
|
2022-01-21 15:06:26 +08:00
|
|
|
|
---
|
2022-01-21 11:54:13 +08:00
|
|
|
|
|
|
|
|
|
|
2022-01-21 15:06:26 +08:00
|
|
|
|
#### 三、快速入门
|
2022-01-21 11:54:13 +08:00
|
|
|
|
|
2022-01-21 15:07:53 +08:00
|
|
|
|
##### 1. 添加依赖
|
2022-01-21 11:54:13 +08:00
|
|
|
|
```
|
|
|
|
|
<dependency>
|
|
|
|
|
<groupId>io.github.applesline</groupId>
|
|
|
|
|
<artifactId>desensitize</artifactId>
|
|
|
|
|
<version>1.0.0-SNAPSHOT</version>
|
|
|
|
|
</dependency>
|
|
|
|
|
```
|
2022-01-21 15:07:53 +08:00
|
|
|
|
##### 2. 打开脱敏开关 @EnableDesensitize
|
2022-01-21 11:54:13 +08:00
|
|
|
|
```
|
|
|
|
|
import org.applesline.desensitize.annotation.EnableDesensitize;
|
|
|
|
|
import org.springframework.boot.SpringApplication;
|
|
|
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
|
|
|
|
|
|
|
|
@SpringBootApplication
|
|
|
|
|
@EnableDesensitize
|
|
|
|
|
public class DemoApplication {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SpringApplication.run(DemoApplication.class, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
2022-01-21 15:07:53 +08:00
|
|
|
|
##### 3. 针对方法的局部脱敏 @Desensitize
|
2022-01-21 11:54:13 +08:00
|
|
|
|
```
|
2022-01-21 15:06:26 +08:00
|
|
|
|
@RequestMapping("queryResult")
|
2022-01-21 11:54:13 +08:00
|
|
|
|
@Desensitize(fieldMapping = @FieldMapping(fields = {"_mobile"},type = DesensitizeType.MOBILE))
|
2022-01-21 15:06:26 +08:00
|
|
|
|
public Result queryResult() {
|
|
|
|
|
return new Result("123456789@163.com","17622233344");
|
2022-01-21 11:54:13 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-21 15:07:53 +08:00
|
|
|
|
##### 4. 脱敏效果展示(响应体中的_mobile字段被脱敏了)
|
2022-01-21 13:32:30 +08:00
|
|
|
|
```
|
|
|
|
|
{
|
2022-01-21 15:06:26 +08:00
|
|
|
|
"_email": "123456789@163.com",
|
2022-01-21 13:32:30 +08:00
|
|
|
|
"_mobile": "176****3344"
|
|
|
|
|
}
|
|
|
|
|
```
|
2022-01-21 15:06:26 +08:00
|
|
|
|
---
|
2022-01-21 13:32:30 +08:00
|
|
|
|
|
2022-01-21 15:06:26 +08:00
|
|
|
|
#### 四、场景示例
|
|
|
|
|
##### 场景一:多字段脱敏
|
2022-01-21 11:54:13 +08:00
|
|
|
|
|
2022-01-21 15:06:26 +08:00
|
|
|
|
###### 1.1 配置示例
|
|
|
|
|
```
|
|
|
|
|
@Desensitize(
|
|
|
|
|
fieldMapping = {
|
|
|
|
|
@FieldMapping(fields = {"_mobile"},type = DesensitizeType.MOBILE),
|
|
|
|
|
@FieldMapping(fields = {"_email"},type = DesensitizeType.EMAIL)}
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
###### 1.2 脱敏效果
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
"_email": "123******@163.com",
|
|
|
|
|
"_mobile": "176****3344"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##### 场景二:多字段匹配同一种脱敏算法
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###### 2.1 配置示例
|
|
|
|
|
```
|
|
|
|
|
@Desensitize(
|
|
|
|
|
fieldMapping = {
|
|
|
|
|
@FieldMapping(fields = {"_mobile","phone"},type = DesensitizeType.MOBILE),
|
|
|
|
|
@FieldMapping(fields = {"_email"},type = DesensitizeType.EMAIL)}
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
###### 2.2 脱敏效果
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
"phone": "123**********.com",
|
|
|
|
|
"result": {
|
|
|
|
|
"_email": "123******@163.com",
|
|
|
|
|
"_mobile": "176****3344"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##### 场景三:忽略字段名相同层级不同的字段
|
|
|
|
|
###### 3.1 注解配置
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
@Desensitize(
|
|
|
|
|
fieldMapping = {
|
|
|
|
|
@FieldMapping(fields = {"_mobile","phone"},type = DesensitizeType.MOBILE),
|
|
|
|
|
@FieldMapping(fields = {"_email"},type = DesensitizeType.EMAIL)},
|
|
|
|
|
ignoreByJpe = "$._mobile"
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
###### 3.2 脱敏效果
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
"_mobile": "17622233345",
|
|
|
|
|
"result": {
|
|
|
|
|
"_email": "123******@163.com",
|
|
|
|
|
"_mobile": "176****3344"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
##### 场景四:全局配置脱敏规则局部方法中使用
|
|
|
|
|
###### 4.1 启动类中配置全局规则
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
@SpringBootApplication
|
|
|
|
|
@EnableDesensitize(
|
|
|
|
|
fieldMapping = {
|
|
|
|
|
@FieldMapping(fields = {"_mobile","phone"},type = DesensitizeType.MOBILE),
|
|
|
|
|
@FieldMapping(fields = {"_email"},type = DesensitizeType.EMAIL)},
|
|
|
|
|
ignoreByJpe = "$._mobile"
|
|
|
|
|
)
|
|
|
|
|
public class DemoApplication {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SpringApplication.run(DemoApplication.class, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
###### 4.2 局部方法中使用全局规则
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
@Desensitize
|
|
|
|
|
public ApiResult queryResult() {
|
|
|
|
|
return new ApiResult("17622233345",new Result("123456789@163.com","17622233344"));
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
###### 4.3 脱敏效果
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
"_mobile": "17622233345",
|
|
|
|
|
"result": {
|
|
|
|
|
"_email": "123******@163.com",
|
|
|
|
|
"_mobile": "176****3344"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##### 场景五:局部方法中覆盖全局配置的脱敏规则
|
|
|
|
|
###### 5.1 启动类中配置全局规则
|
|
|
|
|
```
|
|
|
|
|
@SpringBootApplication
|
|
|
|
|
@EnableDesensitize(
|
|
|
|
|
fieldMapping = {
|
|
|
|
|
@FieldMapping(fields = {"_mobile","phone"},type = DesensitizeType.MOBILE),
|
|
|
|
|
@FieldMapping(fields = {"_email"},type = DesensitizeType.EMAIL)},
|
|
|
|
|
ignoreByJpe = "$._mobile"
|
|
|
|
|
)
|
|
|
|
|
public class DemoApplication {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SpringApplication.run(DemoApplication.class, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
###### 5.2 局部方法中覆盖全局规则
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
@Desensitize( fieldMapping = @FieldMapping(fields = {"_mobile"},type = DesensitizeType.MOBILE) )
|
|
|
|
|
public ApiResult queryResult() {
|
|
|
|
|
return new ApiResult("17622233345",new Result("123456789@163.com","17622233344"));
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
###### 5.3 脱敏效果
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
"_mobile": "176****3345",
|
|
|
|
|
"result": {
|
|
|
|
|
"_email": "123456789@163.com",
|
|
|
|
|
"_mobile": "176****3344"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2022-01-21 15:12:22 +08:00
|
|
|
|
##### 场景中依赖的实体类结构
|
|
|
|
|
```
|
|
|
|
|
class ApiResult {
|
|
|
|
|
private String _mobile;
|
|
|
|
|
private Result result;
|
|
|
|
|
|
|
|
|
|
public ApiResult(String _mobile, Result result) {
|
|
|
|
|
this._mobile = _mobile;
|
|
|
|
|
this.result = result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setter...
|
|
|
|
|
getter...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Result {
|
|
|
|
|
private String _email;
|
|
|
|
|
private String _mobile;
|
|
|
|
|
|
|
|
|
|
public Result(String _email, String _mobile) {
|
|
|
|
|
this._email = _email;
|
|
|
|
|
this._mobile = _mobile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setter...
|
|
|
|
|
getter...
|
|
|
|
|
}
|
|
|
|
|
```
|
2022-01-22 00:06:44 +08:00
|
|
|
|
---
|
|
|
|
|
#### 五、联系作者
|
|
|
|
|
使用过程中有任何问题欢迎前来骚扰~_~
|
|
|
|
|
|
|
|
|
|
邮箱地址:applesline@163.com
|
2022-01-21 11:54:13 +08:00
|
|
|
|
|
|
|
|
|
|
2022-01-21 15:06:26 +08:00
|
|
|
|
|
2022-01-21 11:54:13 +08:00
|
|
|
|
|