forked from dyf/fys-Multi-tenant
fys
This commit is contained in:
25
fys-common/fys-common-sensitive/pom.xml
Normal file
25
fys-common/fys-common-sensitive/pom.xml
Normal file
@ -0,0 +1,25 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<groupId>com.fuyuanshen</groupId>
|
||||
<artifactId>fys-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>fys-common-sensitive</artifactId>
|
||||
|
||||
<description>
|
||||
fys-common-sensitive 脱敏模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fuyuanshen</groupId>
|
||||
<artifactId>fys-common-json</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,34 @@
|
||||
package com.fuyuanshen.common.sensitive.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fuyuanshen.common.sensitive.core.SensitiveStrategy;
|
||||
import com.fuyuanshen.common.sensitive.handler.SensitiveHandler;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 数据脱敏注解
|
||||
*
|
||||
* @author zhujie
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@JacksonAnnotationsInside
|
||||
@JsonSerialize(using = SensitiveHandler.class)
|
||||
public @interface Sensitive {
|
||||
SensitiveStrategy strategy();
|
||||
|
||||
/**
|
||||
* 角色标识符 多个角色满足一个即可
|
||||
*/
|
||||
String[] roleKey() default {};
|
||||
|
||||
/**
|
||||
* 权限标识符 多个权限满足一个即可
|
||||
*/
|
||||
String[] perms() default {};
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.fuyuanshen.common.sensitive.core;
|
||||
|
||||
/**
|
||||
* 脱敏服务
|
||||
* 默认管理员不过滤
|
||||
* 需自行根据业务重写实现
|
||||
*
|
||||
* @author Lion Li
|
||||
* @version 3.6.0
|
||||
*/
|
||||
public interface SensitiveService {
|
||||
|
||||
/**
|
||||
* 是否脱敏
|
||||
*/
|
||||
boolean isSensitive(String[] roleKey, String[] perms);
|
||||
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
package com.fuyuanshen.common.sensitive.core;
|
||||
|
||||
import cn.hutool.core.util.DesensitizedUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 脱敏策略
|
||||
*
|
||||
* @author Yjoioooo
|
||||
* @version 3.6.0
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public enum SensitiveStrategy {
|
||||
|
||||
/**
|
||||
* 身份证脱敏
|
||||
*/
|
||||
ID_CARD(s -> DesensitizedUtil.idCardNum(s, 3, 4)),
|
||||
|
||||
/**
|
||||
* 手机号脱敏
|
||||
*/
|
||||
PHONE(DesensitizedUtil::mobilePhone),
|
||||
|
||||
/**
|
||||
* 地址脱敏
|
||||
*/
|
||||
ADDRESS(s -> DesensitizedUtil.address(s, 8)),
|
||||
|
||||
/**
|
||||
* 邮箱脱敏
|
||||
*/
|
||||
EMAIL(DesensitizedUtil::email),
|
||||
|
||||
/**
|
||||
* 银行卡
|
||||
*/
|
||||
BANK_CARD(DesensitizedUtil::bankCard),
|
||||
|
||||
/**
|
||||
* 中文名
|
||||
*/
|
||||
CHINESE_NAME(DesensitizedUtil::chineseName),
|
||||
|
||||
/**
|
||||
* 固定电话
|
||||
*/
|
||||
FIXED_PHONE(DesensitizedUtil::fixedPhone),
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
USER_ID(s -> String.valueOf(DesensitizedUtil.userId())),
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
PASSWORD(DesensitizedUtil::password),
|
||||
|
||||
/**
|
||||
* ipv4
|
||||
*/
|
||||
IPV4(DesensitizedUtil::ipv4),
|
||||
|
||||
/**
|
||||
* ipv6
|
||||
*/
|
||||
IPV6(DesensitizedUtil::ipv6),
|
||||
|
||||
/**
|
||||
* 中国大陆车牌,包含普通车辆、新能源车辆
|
||||
*/
|
||||
CAR_LICENSE(DesensitizedUtil::carLicense),
|
||||
|
||||
/**
|
||||
* 只显示第一个字符
|
||||
*/
|
||||
FIRST_MASK(DesensitizedUtil::firstMask),
|
||||
|
||||
/**
|
||||
* 清空为""
|
||||
*/
|
||||
CLEAR(s -> DesensitizedUtil.clear()),
|
||||
|
||||
/**
|
||||
* 清空为null
|
||||
*/
|
||||
CLEAR_TO_NULL(s -> DesensitizedUtil.clearToNull());
|
||||
|
||||
//可自行添加其他脱敏策略
|
||||
|
||||
private final Function<String, String> desensitizer;
|
||||
|
||||
public Function<String, String> desensitizer() {
|
||||
return desensitizer;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.fuyuanshen.common.sensitive.handler;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
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.fuyuanshen.common.core.utils.SpringUtils;
|
||||
import com.fuyuanshen.common.sensitive.annotation.Sensitive;
|
||||
import com.fuyuanshen.common.sensitive.core.SensitiveService;
|
||||
import com.fuyuanshen.common.sensitive.core.SensitiveStrategy;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 数据脱敏json序列化工具
|
||||
*
|
||||
* @author Yjoioooo
|
||||
*/
|
||||
@Slf4j
|
||||
public class SensitiveHandler extends JsonSerializer<String> implements ContextualSerializer {
|
||||
|
||||
private SensitiveStrategy strategy;
|
||||
private String[] roleKey;
|
||||
private String[] perms;
|
||||
|
||||
@Override
|
||||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
try {
|
||||
SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class);
|
||||
if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive(roleKey, perms)) {
|
||||
gen.writeString(strategy.desensitizer().apply(value));
|
||||
} else {
|
||||
gen.writeString(value);
|
||||
}
|
||||
} catch (BeansException e) {
|
||||
log.error("脱敏实现不存在, 采用默认处理 => {}", e.getMessage());
|
||||
gen.writeString(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
|
||||
Sensitive annotation = property.getAnnotation(Sensitive.class);
|
||||
if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass())) {
|
||||
this.strategy = annotation.strategy();
|
||||
this.roleKey = annotation.roleKey();
|
||||
this.perms = annotation.perms();
|
||||
return this;
|
||||
}
|
||||
return prov.findValueSerializer(property.getType(), property);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user