dongyukun
2024-12-31 6eeac9efdb16f92d19536bf23a2d1471705fe752
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.desensitize.core.base.serializer;
H 2
3 import cn.hutool.core.annotation.AnnotationUtil;
4 import cn.hutool.core.lang.Singleton;
5 import cn.hutool.core.util.ArrayUtil;
6 import cn.hutool.core.util.ReflectUtil;
7 import cn.hutool.core.util.StrUtil;
8 import com.iailab.framework.desensitize.core.base.annotation.DesensitizeBy;
9 import com.iailab.framework.desensitize.core.base.handler.DesensitizationHandler;
10 import com.fasterxml.jackson.core.JsonGenerator;
11 import com.fasterxml.jackson.databind.BeanProperty;
12 import com.fasterxml.jackson.databind.JsonSerializer;
13 import com.fasterxml.jackson.databind.SerializerProvider;
14 import com.fasterxml.jackson.databind.ser.ContextualSerializer;
15 import com.fasterxml.jackson.databind.ser.std.StdSerializer;
16 import lombok.Getter;
17 import lombok.Setter;
18
19 import java.io.IOException;
20 import java.lang.annotation.Annotation;
21 import java.lang.reflect.Field;
22
23 /**
24  * 脱敏序列化器
25  *
26  * 实现 JSON 返回数据时,使用 {@link DesensitizationHandler} 对声明脱敏注解的字段,进行脱敏处理。
27  *
28  * @author gaibu
29  */
30 @SuppressWarnings("rawtypes")
31 public class StringDesensitizeSerializer extends StdSerializer<String> implements ContextualSerializer {
32
33     @Getter
34     @Setter
35     private DesensitizationHandler desensitizationHandler;
36
37     protected StringDesensitizeSerializer() {
38         super(String.class);
39     }
40
41     @Override
42     public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) {
43         DesensitizeBy annotation = beanProperty.getAnnotation(DesensitizeBy.class);
44         if (annotation == null) {
45             return this;
46         }
47         // 创建一个 StringDesensitizeSerializer 对象,使用 DesensitizeBy 对应的处理器
48         StringDesensitizeSerializer serializer = new StringDesensitizeSerializer();
49         serializer.setDesensitizationHandler(Singleton.get(annotation.handler()));
50         return serializer;
51     }
52
53     @Override
54     @SuppressWarnings("unchecked")
55     public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
56         if (StrUtil.isBlank(value)) {
57             gen.writeNull();
58             return;
59         }
60         // 获取序列化字段
61         Field field = getField(gen);
62
63         // 自定义处理器
64         DesensitizeBy[] annotations = AnnotationUtil.getCombinationAnnotations(field, DesensitizeBy.class);
65         if (ArrayUtil.isEmpty(annotations)) {
66             gen.writeString(value);
67             return;
68         }
69         for (Annotation annotation : field.getAnnotations()) {
70             if (AnnotationUtil.hasAnnotation(annotation.annotationType(), DesensitizeBy.class)) {
71                 value = this.desensitizationHandler.desensitize(value, annotation);
72                 gen.writeString(value);
73                 return;
74             }
75         }
76         gen.writeString(value);
77     }
78
79     /**
80      * 获取字段
81      *
82      * @param generator JsonGenerator
83      * @return 字段
84      */
85     private Field getField(JsonGenerator generator) {
86         String currentName = generator.getOutputContext().getCurrentName();
87         Object currentValue = generator.getCurrentValue();
88         Class<?> currentValueClass = currentValue.getClass();
89         return ReflectUtil.getField(currentValueClass, currentName);
90     }
91
92 }