mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-24 07:19:46 +08:00
update 重构 抽取脱敏功能 ruoyi-common-sensitive 成为独立模块
This commit is contained in:
26
ruoyi-common/ruoyi-common-sensitive/pom.xml
Normal file
26
ruoyi-common/ruoyi-common-sensitive/pom.xml
Normal 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">
|
||||
<parent>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-common-sensitive</artifactId>
|
||||
|
||||
<description>
|
||||
ruoyi-common-sensitive 脱敏模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,24 @@
|
||||
package com.ruoyi.common.sensitive.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.ruoyi.common.sensitive.core.SensitiveStrategy;
|
||||
import com.ruoyi.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();
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.ruoyi.common.sensitive.core;
|
||||
|
||||
/**
|
||||
* 脱敏服务
|
||||
* 默认管理员不过滤
|
||||
* 需自行根据业务重写实现
|
||||
*
|
||||
* @author Lion Li
|
||||
* @version 3.6.0
|
||||
*/
|
||||
public interface SensitiveService {
|
||||
|
||||
/**
|
||||
* 是否脱敏
|
||||
*/
|
||||
boolean isSensitive();
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.ruoyi.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);
|
||||
|
||||
//可自行添加其他脱敏策略
|
||||
|
||||
private final Function<String, String> desensitizer;
|
||||
|
||||
public Function<String, String> desensitizer() {
|
||||
return desensitizer;
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.ruoyi.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.ruoyi.common.core.utils.SpringUtils;
|
||||
import com.ruoyi.common.sensitive.annotation.Sensitive;
|
||||
import com.ruoyi.common.sensitive.core.SensitiveService;
|
||||
import com.ruoyi.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;
|
||||
|
||||
@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()) {
|
||||
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();
|
||||
return this;
|
||||
}
|
||||
return prov.findValueSerializer(property.getType(), property);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user