数据脱敏
parent
9ea83bb7dc
commit
6233a50911
|
@ -22,6 +22,20 @@
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>cloud-common-core</artifactId>
|
<artifactId>cloud-common-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--hutool-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-core</artifactId>
|
||||||
|
<version>${hutool.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--json模块-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-json</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.muyu.domain.dto;
|
||||||
|
|
||||||
|
import com.muyu.domain.enumerate.Desensitization;
|
||||||
|
import com.muyu.domain.enumerate.DesensitizationTypeEnum;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @PackageName:com.muyu.domain
|
||||||
|
* @ClassName:AnnotainDTO
|
||||||
|
* @Description:
|
||||||
|
* @author: ¥陈思豪¥
|
||||||
|
* @date: 2024/8/21 16:10
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class AnnotationDTO implements Serializable {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手机号
|
||||||
|
*/
|
||||||
|
@Desensitization(type = DesensitizationTypeEnum.MOBILE_PHONE)
|
||||||
|
private String phone;
|
||||||
|
/**
|
||||||
|
* 邮箱
|
||||||
|
*/
|
||||||
|
@Desensitization(type = DesensitizationTypeEnum.EMAIL)
|
||||||
|
private String email;
|
||||||
|
/**
|
||||||
|
* 身份证
|
||||||
|
*/
|
||||||
|
@Desensitization(type = DesensitizationTypeEnum.ID_CARD)
|
||||||
|
private String idCard;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.muyu.domain.enumerate;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
import com.muyu.domain.serialize.DesensitizationSerialize;
|
||||||
|
import kotlin.annotation.AnnotationRetention;
|
||||||
|
import kotlin.annotation.Retention;
|
||||||
|
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @PackageName:com.muyu.domain.enumerate
|
||||||
|
* @ClassName:Desensitization
|
||||||
|
* @Description:
|
||||||
|
* @author: ¥陈思豪¥
|
||||||
|
* @date: 2024/8/21 14:04
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@Retention(AnnotationRetention.RUNTIME)
|
||||||
|
@JacksonAnnotationsInside
|
||||||
|
@JsonSerialize(using = DesensitizationSerialize.class)
|
||||||
|
public @interface Desensitization {
|
||||||
|
/**
|
||||||
|
* 脱敏规则
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
DesensitizationTypeEnum type() default DesensitizationTypeEnum.EMAIL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始位置(包含)
|
||||||
|
*/
|
||||||
|
int startInclude() default 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 结束位置(不包含)
|
||||||
|
*/
|
||||||
|
int endExclude() default 0;
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.muyu.domain.enumerate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @PackageName:com.muyu.domain
|
||||||
|
* @ClassName:SensitiveType
|
||||||
|
* @Description: 数据脱敏
|
||||||
|
* @author: ¥陈思豪¥
|
||||||
|
* @date: 2024/8/21 13:59
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
public enum DesensitizationTypeEnum {
|
||||||
|
//名称
|
||||||
|
NAME,
|
||||||
|
//中文名
|
||||||
|
CHINESE_NAME,
|
||||||
|
//身份证号
|
||||||
|
ID_CARD,
|
||||||
|
//手机号
|
||||||
|
MOBILE_PHONE,
|
||||||
|
//地址
|
||||||
|
ADDRESS,
|
||||||
|
//电子邮件
|
||||||
|
EMAIL,
|
||||||
|
//密码
|
||||||
|
PASSWORD,
|
||||||
|
//银行卡
|
||||||
|
BANK_CARD
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
package com.muyu.domain.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.muyu.domain.enumerate.DesensitizationTypeEnum;
|
||||||
|
import com.muyu.domain.enumerate.Desensitization;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@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 NAME:
|
||||||
|
jsonGenerator.writeString(DesensitizedUtil.chineseName(String.valueOf(str)));
|
||||||
|
break;
|
||||||
|
// 身份证
|
||||||
|
case ID_CARD:
|
||||||
|
jsonGenerator.writeString(DesensitizedUtil.idCardNum(String.valueOf(str), 1, 2));
|
||||||
|
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 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) {
|
||||||
|
int i = desensitization.endExclude();
|
||||||
|
return new DesensitizationSerialize(desensitization.type(), desensitization.startInclude(),
|
||||||
|
desensitization.endExclude());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
|
||||||
|
}
|
||||||
|
return serializerProvider.findNullValueSerializer(null);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.muyu.cloud.etl.controller;
|
||||||
|
|
||||||
|
import com.muyu.cloud.etl.service.AnnotationDTOService;
|
||||||
|
import com.muyu.common.core.web.controller.BaseController;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @PackageName:com.muyu.cloud.etl.controller
|
||||||
|
* @ClassName:SensitiveController
|
||||||
|
* @Description:
|
||||||
|
* @author: ¥陈思豪¥
|
||||||
|
* @date: 2024/8/21 15:23
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class SensitiveController extends BaseController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AnnotationDTOService annotationDTOService;
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.muyu.cloud.etl.controller;
|
||||||
|
|
||||||
|
import com.muyu.domain.dto.AnnotationDTO;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @PackageName:com.muyu.cloud.etl.controller
|
||||||
|
* @ClassName:TextController
|
||||||
|
* @Description:
|
||||||
|
* @author: ¥陈思豪¥
|
||||||
|
* @date: 2024/8/21 16:32
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/test")
|
||||||
|
public class TextController {
|
||||||
|
|
||||||
|
@GetMapping("/test-annotation")
|
||||||
|
public AnnotationDTO testAnnotation(){
|
||||||
|
AnnotationDTO annotationDTO = new AnnotationDTO();
|
||||||
|
annotationDTO.setPhone("1111111111111");
|
||||||
|
annotationDTO.setEmail("1437200980@qq.com");
|
||||||
|
annotationDTO.setIdCard("1342543654544322754635");
|
||||||
|
return annotationDTO;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.muyu.cloud.etl.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.muyu.domain.dto.AnnotationDTO;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface AnnotationDTOMapper extends BaseMapper<AnnotationDTO>{
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.muyu.cloud.etl.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.muyu.domain.dto.AnnotationDTO;
|
||||||
|
|
||||||
|
|
||||||
|
public interface AnnotationDTOService extends IService<AnnotationDTO> {
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.muyu.cloud.etl.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.muyu.cloud.etl.mapper.AnnotationDTOMapper;
|
||||||
|
import com.muyu.cloud.etl.service.AnnotationDTOService;
|
||||||
|
import com.muyu.domain.dto.AnnotationDTO;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @PackageName:com.muyu.cloud.etl.service.impl
|
||||||
|
* @ClassName:SensitiveServiceImpl
|
||||||
|
* @Description:
|
||||||
|
* @author: ¥陈思豪¥
|
||||||
|
* @date: 2024/8/21 15:24
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class AnnotationDTOServiceImpl extends ServiceImpl<AnnotationDTOMapper, AnnotationDTO> implements AnnotationDTOService {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ nacos:
|
||||||
addr: 47.116.184.54:8848
|
addr: 47.116.184.54:8848
|
||||||
user-name: nacos
|
user-name: nacos
|
||||||
password: nacos
|
password: nacos
|
||||||
namespace: cloud-2112
|
namespace: text
|
||||||
# Spring
|
# Spring
|
||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue