Browse Source

一个注解实现数据脱敏

pull/2/head
马府强 1 year ago
parent
commit
bfedc04c94
  1. 68
      tool-desensitization/pom.xml
  2. 19
      tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/ToolDesensitizationApplication.java
  3. 38
      tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/annotation/Desensitization.java
  4. 36
      tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/api/TestApi.java
  5. 39
      tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/dto/TestAnnotationDTO.java
  6. 22
      tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/dto/TestDTO.java
  7. 33
      tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/enums/DesensitizationTypeEnum.java
  8. 108
      tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/serialize/DesensitizationSerialize.java
  9. 23
      tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/serialize/TestJacksonSerialize.java
  10. 1
      tool-desensitization/src/main/resources/application.properties
  11. 16
      tool-desensitization/src/test/java/com/jiaqing/tooldesensitization/ToolDesensitizationApplicationTests.java
  12. 91
      tool-desensitization/tool-desensitization.iml

68
tool-desensitization/pom.xml

@ -0,0 +1,68 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jiaqing</groupId>
<artifactId>tool-desensitization</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tool-desensitization</name>
<description>数据脱敏</description>
<properties>
<java.version>1.8</java.version>
<hutool.version>5.8.5</hutool.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--json模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

19
tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/ToolDesensitizationApplication.java

@ -0,0 +1,19 @@
package com.jiaqing.tooldesensitization;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @description: 一个注解解决数据脱敏问题
* @return:
* @author: woniu
* @time: 2023/4/17 21:11
*/
@SpringBootApplication
public class ToolDesensitizationApplication {
public static void main(String[] args) {
SpringApplication.run(ToolDesensitizationApplication.class, args);
}
}

38
tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/annotation/Desensitization.java

@ -0,0 +1,38 @@
package com.jiaqing.tooldesensitization.annotation;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.jiaqing.tooldesensitization.enums.DesensitizationTypeEnum;
import com.jiaqing.tooldesensitization.serialize.DesensitizationSerialize;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 一个注解解决数据脱敏问题
* @description: 数据脱敏注解
* @author: woniu
* @time: 2023/4/17 21:35
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JacksonAnnotationsInside
@JsonSerialize(using = DesensitizationSerialize.class)
public @interface Desensitization {
/**
* 脱敏数据类型只要在CUSTOMER的时候startInclude和endExclude生效
*/
DesensitizationTypeEnum type() default DesensitizationTypeEnum.CUSTOMER;
/**
* 开始位置包含
*/
int startInclude() default 0;
/**
* 结束位置不包含
*/
int endExclude() default 0;
}

36
tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/api/TestApi.java

@ -0,0 +1,36 @@
package com.jiaqing.tooldesensitization.api;
import com.jiaqing.tooldesensitization.dto.TestAnnotationDTO;
import com.jiaqing.tooldesensitization.dto.TestDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: woniu
* @Description 测试api
* @ClassName TestApi
* @Date 2023/4/23 20:16
**/
@RestController
@RequestMapping("/test")
public class TestApi {
@GetMapping
public TestDTO test(){
TestDTO testDTO = new TestDTO();
testDTO.setPhone("17677772345");
return testDTO;
}
@GetMapping("/test-annotation")
public TestAnnotationDTO testAnnotation(){
TestAnnotationDTO testAnnotationDTO = new TestAnnotationDTO();
testAnnotationDTO.setPhone("17677772345");
testAnnotationDTO.setCustom("111111111111111111");
testAnnotationDTO.setEmail("1433926101@qq.com");
testAnnotationDTO.setIdCard("4444199810015555");
return testAnnotationDTO;
}
}

39
tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/dto/TestAnnotationDTO.java

@ -0,0 +1,39 @@
package com.jiaqing.tooldesensitization.dto;
import com.jiaqing.tooldesensitization.annotation.Desensitization;
import com.jiaqing.tooldesensitization.enums.DesensitizationTypeEnum;
import lombok.Data;
import java.io.Serializable;
/**
* @author: woniu
* @Description 测试DTO
* @ClassName TestDTO
* @Date 2023/4/23 19:43
**/
@Data
public class TestAnnotationDTO implements Serializable {
/**
* 自定义
*/
@Desensitization(type = DesensitizationTypeEnum.CUSTOMER,startInclude = 5,endExclude = 10)
private String custom;
/**
* 手机号
*/
@Desensitization(type = DesensitizationTypeEnum.MOBILE_PHONE)
private String phone;
/**
* 邮箱
*/
@Desensitization(type = DesensitizationTypeEnum.EMAIL)
private String email;
/**
* 身份证
*/
@Desensitization(type = DesensitizationTypeEnum.ID_CARD)
private String idCard;
}

22
tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/dto/TestDTO.java

@ -0,0 +1,22 @@
package com.jiaqing.tooldesensitization.dto;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.jiaqing.tooldesensitization.serialize.TestJacksonSerialize;
import lombok.Data;
import java.io.Serializable;
/**
* @author: woniu
* @Description 测试DTO
* @ClassName TestDTO
* @Date 2023/4/23 19:43
**/
@Data
public class TestDTO implements Serializable {
/**
* 手机号
*/
@JsonSerialize(using = TestJacksonSerialize.class)
private String phone;
}

33
tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/enums/DesensitizationTypeEnum.java

@ -0,0 +1,33 @@
package com.jiaqing.tooldesensitization.enums;
/**
* @author: woniu
* @Description 数据脱敏枚举
* @ClassName DesensitizedTypeEnum
* @Date 2023/4/17 21:39
**/
public enum DesensitizationTypeEnum {
//自定义
CUSTOMER,
//用户id
USER_ID,
//中文名
CHINESE_NAME,
//身份证号
ID_CARD,
//座机号
FIXED_PHONE,
//手机号
MOBILE_PHONE,
//地址
ADDRESS,
//电子邮件
EMAIL,
//密码
PASSWORD,
//中国大陆车牌,包含普通车辆、新能源车辆
CAR_LICENSE,
//银行卡
BANK_CARD
}

108
tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/serialize/DesensitizationSerialize.java

@ -0,0 +1,108 @@
package com.jiaqing.tooldesensitization.serialize;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.DesensitizedUtil;
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.jiaqing.tooldesensitization.annotation.Desensitization;
import com.jiaqing.tooldesensitization.enums.DesensitizationTypeEnum;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import java.io.IOException;
import java.util.Objects;
/**
* @author: woniu
* @Description 脱敏序列化
* @ClassName DesensitizedSerialize
* @Date 2023/4/17 21:57
**/
@NoArgsConstructor
@AllArgsConstructor
public class DesensitizationSerialize extends JsonSerializer<String> implements ContextualSerializer {
private DesensitizationTypeEnum type;
private Integer startInclude;
private Integer endExclude;
@Override
public void serialize(String str, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
switch (type) {
// 自定义类型脱敏
case CUSTOMER:
jsonGenerator.writeString(CharSequenceUtil.hide(str,startInclude,endExclude));
break;
// userId脱敏
case USER_ID:
jsonGenerator.writeString(String.valueOf(DesensitizedUtil.userId()));
break;
// 中文姓名脱敏
case CHINESE_NAME:
jsonGenerator.writeString(DesensitizedUtil.chineseName(String.valueOf(str)));
break;
// 身份证脱敏
case ID_CARD:
jsonGenerator.writeString(DesensitizedUtil.idCardNum(String.valueOf(str), 1, 2));
break;
// 固定电话脱敏
case FIXED_PHONE:
jsonGenerator.writeString(DesensitizedUtil.fixedPhone(String.valueOf(str)));
break;
// 手机号脱敏
case MOBILE_PHONE:
jsonGenerator.writeString(DesensitizedUtil.mobilePhone(String.valueOf(str)));
break;
// 地址脱敏
case ADDRESS:
jsonGenerator.writeString(DesensitizedUtil.address(String.valueOf(str), 8));
break;
// 邮箱脱敏
case EMAIL:
jsonGenerator.writeString(DesensitizedUtil.email(String.valueOf(str)));
break;
// 密码脱敏
case PASSWORD:
jsonGenerator.writeString(DesensitizedUtil.password(String.valueOf(str)));
break;
// 中国车牌脱敏
case CAR_LICENSE:
jsonGenerator.writeString(DesensitizedUtil.carLicense(String.valueOf(str)));
break;
// 银行卡脱敏
case BANK_CARD:
jsonGenerator.writeString(DesensitizedUtil.bankCard(String.valueOf(str)));
break;
default:
}
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
if (beanProperty != null) {
// 判断数据类型是否为String类型
if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
// 获取定义的注解
Desensitization desensitization = beanProperty.getAnnotation(Desensitization.class);
// 为null
if (desensitization == null) {
desensitization = beanProperty.getContextAnnotation(Desensitization.class);
}
// 不为null
if (desensitization != null) {
// 创建定义的序列化类的实例并且返回,入参为注解定义的type,开始位置,结束位置。
return new DesensitizationSerialize(desensitization.type(), desensitization.startInclude(),
desensitization.endExclude());
}
}
return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
}
return serializerProvider.findNullValueSerializer(null);
}
}

23
tool-desensitization/src/main/java/com/jiaqing/tooldesensitization/serialize/TestJacksonSerialize.java

@ -0,0 +1,23 @@
package com.jiaqing.tooldesensitization.serialize;
import cn.hutool.core.util.DesensitizedUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.SneakyThrows;
/**
* @author: woniu
* @Description 脱敏序列化
* @ClassName DesensitizedSerialize
* @Date 2023/4/17 21:57
**/
public class TestJacksonSerialize extends JsonSerializer<String> {
@Override
@SneakyThrows
public void serialize(String str, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
// 使用我们的hutool工具类进行手机号脱敏
jsonGenerator.writeString(DesensitizedUtil.fixedPhone(String.valueOf(str)));
}
}

1
tool-desensitization/src/main/resources/application.properties

@ -0,0 +1 @@

16
tool-desensitization/src/test/java/com/jiaqing/tooldesensitization/ToolDesensitizationApplicationTests.java

@ -0,0 +1,16 @@
package com.jiaqing.tooldesensitization;
import cn.hutool.core.util.DesensitizedUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ToolDesensitizationApplicationTests {
@Test
void contextLoads() {
String phoneNumber = "17677772345";
System.out.println(DesensitizedUtil.mobilePhone(phoneNumber));
}
}

91
tool-desensitization/tool-desensitization.iml

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
<facet type="web" name="Web">
<configuration>
<webroots />
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
</sourceRoots>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.7.10" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.7.10" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.26" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.7.10" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.7.10" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.11" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.11" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.17.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.17.2" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.36" level="project" />
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.26" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.26" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.30" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.7.10" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.7.10" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.73" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.73" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.73" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.3.26" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.26" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.3.26" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.26" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.26" level="project" />
<orderEntry type="library" name="Maven: cn.hutool:hutool-core:5.8.5" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.26" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.7.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.7.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.7.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.7.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.4.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:2.4.9" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:9.3" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.36" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: jakarta.xml.bind:jakarta.xml.bind-api:2.3.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: jakarta.activation:jakarta.activation-api:1.2.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:3.22.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest:2.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:4.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.12.23" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.12.23" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:3.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:4.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.3.26" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.9.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.7.10" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.13.5" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.13.5" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.13.5" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.5" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.5" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.5" level="project" />
</component>
</module>
Loading…
Cancel
Save