add 增加 数据脱敏 功能
parent
b12b98ab1c
commit
bf019b405e
@ -0,0 +1,24 @@
|
|||||||
|
package com.ruoyi.common.core.annotation;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
import com.ruoyi.common.core.enums.SensitiveStrategy;
|
||||||
|
import com.ruoyi.common.core.jackson.SensitiveJsonSerializer;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据脱敏注解
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@JacksonAnnotationsInside
|
||||||
|
@JsonSerialize(using = SensitiveJsonSerializer.class)
|
||||||
|
public @interface Sensitive {
|
||||||
|
SensitiveStrategy strategy();
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package com.ruoyi.common.core.enums;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.DesensitizedUtil;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脱敏策略
|
||||||
|
*
|
||||||
|
* @author Yjoioooo
|
||||||
|
*/
|
||||||
|
@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,46 @@
|
|||||||
|
package com.ruoyi.common.core.jackson;
|
||||||
|
|
||||||
|
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.annotation.Sensitive;
|
||||||
|
import com.ruoyi.common.core.enums.SensitiveStrategy;
|
||||||
|
import com.ruoyi.common.core.service.SensitiveService;
|
||||||
|
import com.ruoyi.common.core.utils.SpringUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据脱敏json序列化工具
|
||||||
|
*
|
||||||
|
* @author Yjoioooo
|
||||||
|
*/
|
||||||
|
public class SensitiveJsonSerializer extends JsonSerializer<String> implements ContextualSerializer {
|
||||||
|
|
||||||
|
private SensitiveStrategy strategy;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||||
|
SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class);
|
||||||
|
if (sensitiveService.isSensitive()) {
|
||||||
|
gen.writeString(value);
|
||||||
|
} else {
|
||||||
|
gen.writeString(strategy.desensitizer().apply(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package com.ruoyi.common.core.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脱敏服务
|
||||||
|
* 默认管理员不过滤
|
||||||
|
* 需自行根据业务重写实现
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
* @version 3.6.0
|
||||||
|
*/
|
||||||
|
public interface SensitiveService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否脱敏
|
||||||
|
*/
|
||||||
|
boolean isSensitive();
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
package com.ruoyi.system.service.impl;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.service.SensitiveService;
|
||||||
|
import com.ruoyi.common.satoken.utils.LoginHelper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脱敏服务
|
||||||
|
* 默认管理员不过滤
|
||||||
|
* 需自行根据业务重写实现
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SysSensitiveServiceImpl implements SensitiveService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否脱敏
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isSensitive() {
|
||||||
|
return LoginHelper.isAdmin();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue