|
|
|
|
@ -1,31 +1,43 @@
|
|
|
|
|
package com.ruoyi.common.sensitive.config;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
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.constant.UserConstants;
|
|
|
|
|
import com.ruoyi.common.core.context.SecurityContextHolder;
|
|
|
|
|
import com.ruoyi.common.sensitive.annotation.Sensitive;
|
|
|
|
|
import com.ruoyi.common.sensitive.enums.DesensitizedType;
|
|
|
|
|
import tools.jackson.core.JacksonException;
|
|
|
|
|
import tools.jackson.core.JsonGenerator;
|
|
|
|
|
import tools.jackson.databind.BeanProperty;
|
|
|
|
|
import tools.jackson.databind.DatabindException;
|
|
|
|
|
import tools.jackson.databind.SerializationContext;
|
|
|
|
|
import tools.jackson.databind.ValueSerializer;
|
|
|
|
|
import tools.jackson.databind.ser.std.StdSerializer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据脱敏序列化过滤
|
|
|
|
|
*
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
public class SensitiveJsonSerializer extends JsonSerializer<String> implements ContextualSerializer
|
|
|
|
|
public class SensitiveJsonSerializer extends StdSerializer<String>
|
|
|
|
|
{
|
|
|
|
|
private DesensitizedType desensitizedType;
|
|
|
|
|
private final DesensitizedType desensitizedType;
|
|
|
|
|
|
|
|
|
|
public SensitiveJsonSerializer()
|
|
|
|
|
{
|
|
|
|
|
super(String.class);
|
|
|
|
|
this.desensitizedType = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SensitiveJsonSerializer(DesensitizedType desensitizedType)
|
|
|
|
|
{
|
|
|
|
|
super(String.class);
|
|
|
|
|
this.desensitizedType = desensitizedType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException
|
|
|
|
|
public void serialize(String value, JsonGenerator gen, SerializationContext ctxt) throws JacksonException
|
|
|
|
|
{
|
|
|
|
|
if (desensitization())
|
|
|
|
|
if (desensitizedType != null && desensitization())
|
|
|
|
|
{
|
|
|
|
|
gen.writeString(desensitizedType.desensitizer().apply(value));
|
|
|
|
|
}
|
|
|
|
|
@ -36,16 +48,14 @@ public class SensitiveJsonSerializer extends JsonSerializer<String> implements C
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
|
|
|
|
|
throws JsonMappingException
|
|
|
|
|
public ValueSerializer<?> createContextual(SerializationContext ctxt, BeanProperty property) throws DatabindException
|
|
|
|
|
{
|
|
|
|
|
Sensitive annotation = property.getAnnotation(Sensitive.class);
|
|
|
|
|
if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass()))
|
|
|
|
|
{
|
|
|
|
|
this.desensitizedType = annotation.desensitizedType();
|
|
|
|
|
return this;
|
|
|
|
|
return new SensitiveJsonSerializer(annotation.desensitizedType());
|
|
|
|
|
}
|
|
|
|
|
return prov.findValueSerializer(property.getType(), property);
|
|
|
|
|
return ctxt.findValueSerializer(property.getType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|