Spring数据脱敏续

前言

如你所见, 该篇是jackson序列化操作的再次应用。将数据库中的 FreeMarker 表达式处理为变量返回给前端。同时将前端提交的变量处理为 FreeMarker 表达式存入数据库。

案例

1
2
3
public enum FieldType {
FREE_MARKER
}
1
2
3
4
5
6
7
8
9
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = FieldSerialize.class)
@JsonDeserialize(using = FieldDeserialize.class)
public @interface FieldHandler {

public FieldType value() default FieldType.FREE_MARKER;

}

自定义序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@Component
public class FieldSerialize extends JsonSerializer<String> implements ContextualSerializer {

private static FreeMarkerDictService freeMarkerDictService;

private FieldType fieldType;

public FieldSerialize() {
this.fieldType = FieldType.FREE_MARKER;
// SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}

public FieldSerialize(FieldType fieldType) {
this.fieldType = fieldType;
}

@Autowired
public FieldSerialize(FreeMarkerDictService freeMarkerDictService) {
this.freeMarkerDictService = freeMarkerDictService;
}

@Override
public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
List<AiFreeMarkerDict> freeMarkerDicts = freeMarkerDictService.list();
switch (fieldType) {
case FREE_MARKER:
jsonGenerator.writeString(FieldUtils.freemarkerExpression2Variable(s, freeMarkerDicts));
break;
}
}

@Override
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
if (beanProperty != null) { // 为空直接跳过
if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) { // 非 String 类直接跳过
FieldHandler fieldHandler = beanProperty.getAnnotation(FieldHandler.class);
if (fieldHandler == null) {
fieldHandler = beanProperty.getContextAnnotation(FieldHandler.class);
}
if (fieldHandler != null) {
return new FieldSerialize(fieldHandler.value());
}
}
return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
}
return serializerProvider.findNullValueSerializer(beanProperty);
}
}

反序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@Component
public class FieldDeserialize extends JsonDeserializer<String> implements ContextualDeserializer {

private static FreeMarkerDictService freeMarkerDictService;

private FieldType fieldType;

public FieldDeserialize() {
this.fieldType = FieldType.FREE_MARKER;
// SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}

public FieldDeserialize(FieldType fieldType) {
this.fieldType = fieldType;
}

@Autowired
public FieldDeserialize(FreeMarkerDictService freeMarkerDictService) {
this.freeMarkerDictService = freeMarkerDictService;
}

@Override
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
List<AiFreeMarkerDict> freeMarkerDicts = freeMarkerDictService.list();
String freeMarkerVariable = jsonParser.getText();
return FieldUtils.freemarkerVariable2Expression(freeMarkerVariable, freeMarkerDicts);
}

@Override
public JsonDeserializer<?> createContextual(DeserializationContext deserializationContext, BeanProperty beanProperty) throws JsonMappingException {
if (beanProperty != null) { // 为空直接跳过
if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) { // 非 String 类直接跳过
FieldHandler fieldHandler = beanProperty.getAnnotation(FieldHandler.class);
if (fieldHandler == null) {
fieldHandler = beanProperty.getContextAnnotation(FieldHandler.class);
}
if (fieldHandler != null) {
return new FieldDeserialize(fieldHandler.value());
}
}
return deserializationContext.findRootValueDeserializer(beanProperty.getType());
}
return deserializationContext.findRootValueDeserializer(beanProperty.getType());
}

}

工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class FieldUtils {

// public static String freemarkerExpression2Variable(final String expression, final Map<String, AiFreeMarkerDict> freeMarkerDataMap) {
// return freeMarkerDataMap.get(expression).getLabel();
// }
//
// public static String freemarkerVariable2Expression(final String variable, final Map<String, AiFreeMarkerDict> freeMarkerDataMap) {
// return freeMarkerDataMap.get(variable).getExpression();
// }

public static String freemarkerExpression2Variable(String expression, final List<AiFreeMarkerDict> freeMarkerDictList) {
if (CollectionUtils.isNotEmpty(freeMarkerDictList)) {
for (AiFreeMarkerDict freeMarkerDict : freeMarkerDictList) {
if (expression.contains(freeMarkerDict.getExpression())) {
expression = expression.replace(freeMarkerDict.getExpression(), freeMarkerDict.getVariable());
}
}
}
return expression;
}

public static String freemarkerVariable2Expression(String variable, final List<AiFreeMarkerDict> freeMarkerDataList) {
if (CollectionUtils.isNotEmpty(freeMarkerDataList)) {
for (AiFreeMarkerDict freeMarkerDict : freeMarkerDataList) {
if (variable.contains(freeMarkerDict.getVariable())) {
variable = variable.replace(freeMarkerDict.getVariable(), freeMarkerDict.getExpression());
}
}
return variable;
}
throw new FreeMarkerException("非法变量");
}
}

使用只要在需要处理的字段上添加注解即可

1
2
3
@FieldHandler
@ApiModelProperty(value = "提问内容")
private String content;

最终效果: 数据库如下数据

1
<#if memory.Country??>${memory.Country}</#if>

api接口返回

1
${memory.Country}

最后

本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。