|
|
@@ -1,13 +1,13 @@
|
|
|
package com.ruoyi.common.config.serializer;
|
|
|
|
|
|
-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 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;
|
|
|
import com.ruoyi.common.annotation.Sensitive;
|
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
import com.ruoyi.common.enums.DesensitizedType;
|
|
|
@@ -18,14 +18,26 @@ import com.ruoyi.common.utils.SecurityUtils;
|
|
|
*
|
|
|
* @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());
|
|
|
}
|
|
|
|
|
|
/**
|