|
@@ -1,12 +1,12 @@
|
|
|
-package cn.iocoder.yudao.framework.desensitize.serializer;
|
|
|
+package cn.iocoder.yudao.framework.desensitize.core.base.serializer;
|
|
|
|
|
|
import cn.hutool.core.annotation.AnnotationUtil;
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
import cn.hutool.core.util.ReflectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize;
|
|
|
+import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
|
|
|
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
|
|
|
-import cn.iocoder.yudao.framework.desensitize.core.base.DesensitizationHandlerHolder;
|
|
|
+import cn.iocoder.yudao.framework.desensitize.core.base.DesensitizationHandlerFactory;
|
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
|
|
import com.fasterxml.jackson.databind.BeanProperty;
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
@@ -14,40 +14,36 @@ import com.fasterxml.jackson.databind.JsonSerializer;
|
|
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
|
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
|
|
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
|
|
+import lombok.Getter;
|
|
|
+import lombok.Setter;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.lang.annotation.Annotation;
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
|
-// TODO @城:挪到 base/serializer 包下
|
|
|
/**
|
|
|
* 脱敏序列化器
|
|
|
+ *
|
|
|
+ * @author gaibu
|
|
|
*/
|
|
|
public class StringDesensitizeSerializer extends StdSerializer<String> implements ContextualSerializer {
|
|
|
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
private DesensitizationHandler desensitizationHandler;
|
|
|
|
|
|
protected StringDesensitizeSerializer() {
|
|
|
super(String.class);
|
|
|
}
|
|
|
|
|
|
- // TODO @城:get 和 set 方法是必须的么?如果是的话,可以换成 lombok 注解哈,简洁一点~
|
|
|
- public DesensitizationHandler getDesensitizationHandler() {
|
|
|
- return desensitizationHandler;
|
|
|
- }
|
|
|
-
|
|
|
- public void setDesensitizationHandler(DesensitizationHandler desensitizationHandler) {
|
|
|
- this.desensitizationHandler = desensitizationHandler;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
|
|
|
- Desensitize annotation = beanProperty.getAnnotation(Desensitize.class);
|
|
|
+ DesensitizeBy annotation = beanProperty.getAnnotation(DesensitizeBy.class);
|
|
|
if (annotation == null) {
|
|
|
return this;
|
|
|
}
|
|
|
StringDesensitizeSerializer serializer = new StringDesensitizeSerializer();
|
|
|
- serializer.setDesensitizationHandler(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationBy()));
|
|
|
+ serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler()));
|
|
|
return serializer;
|
|
|
}
|
|
|
|
|
@@ -57,21 +53,17 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
|
|
|
gen.writeNull();
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- // TODO @城:抽个 private getField 方法。让这个方法的逻辑主干,更清晰
|
|
|
- String currentName = gen.getOutputContext().getCurrentName();
|
|
|
- Object currentValue = gen.getCurrentValue();
|
|
|
- Class<?> currentValueClass = currentValue.getClass();
|
|
|
- Field field = ReflectUtil.getField(currentValueClass, currentName);
|
|
|
+ // 获取序列化字段
|
|
|
+ Field field = getField(gen);
|
|
|
|
|
|
// 自定义处理器
|
|
|
- Desensitize[] annotations = AnnotationUtil.getCombinationAnnotations(field, Desensitize.class);
|
|
|
+ DesensitizeBy[] annotations = AnnotationUtil.getCombinationAnnotations(field, DesensitizeBy.class);
|
|
|
if (ArrayUtil.isEmpty(annotations)) {
|
|
|
gen.writeString(value);
|
|
|
return;
|
|
|
}
|
|
|
for (Annotation annotation : field.getAnnotations()) {
|
|
|
- if (AnnotationUtil.hasAnnotation(annotation.annotationType(), Desensitize.class)) {
|
|
|
+ if (AnnotationUtil.hasAnnotation(annotation.annotationType(), DesensitizeBy.class)) {
|
|
|
value = this.desensitizationHandler.desensitize(value, annotation);
|
|
|
gen.writeString(value);
|
|
|
return;
|
|
@@ -80,4 +72,17 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
|
|
|
gen.writeString(value);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取字段
|
|
|
+ *
|
|
|
+ * @param gen JsonGenerator
|
|
|
+ * @return 字段
|
|
|
+ */
|
|
|
+ private Field getField(JsonGenerator gen) {
|
|
|
+ String currentName = gen.getOutputContext().getCurrentName();
|
|
|
+ Object currentValue = gen.getCurrentValue();
|
|
|
+ Class<?> currentValueClass = currentValue.getClass();
|
|
|
+ return ReflectUtil.getField(currentValueClass, currentName);
|
|
|
+ }
|
|
|
+
|
|
|
}
|