最近因为项目中有一些有关个人的信息,所以在前端交互的过程中,需要将数据脱敏处理,之前使用过 jsckson 做此操作,现在项目中使用的是 fastjson,查找资料之后做一个记录。
一.使用 fastjson
- 首先
/**
---> com.alibaba.fastjson.serializer.ValueFilter
自定义类实现 ValueFilter
@Param object 当前字段所属的对象
@Param name 当前字段名
@Param value 当前字段值
*/
@Override
public Object process(Object object, String name, Object value) {
// 在这里处理 value 然后将处理之后的数据返回去
return value;
}
- 第二 配置 FastJsonHttpMessageConverter 消息转换器
private FastJsonHttpMessageConverter responseFastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 将定制好的过滤器加在 fastjson 过滤器执行链中
fastJsonConfig.setSerializeFilters(new SensitiveDataRemoveFilter());
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
return fastJsonHttpMessageConverter;
}
- 第三 将消息转换器放在 springboot 的信息转化链中
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = responseFastJsonHttpMessageConverter();
converters.add(fastJsonHttpMessageConverter);
}
2. 使用 jackjson
此处格式化 BigDecimal 为例
- 首先创建 BigDecimalFormat
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@JacksonAnnotationsInside
@JsonSerialize(using = DecimalSerialize.class)
public @interface BigDecimalFormat {
String value() default "";
}
- 创建 DecimalSerialize 作为 BigDecimal 专用序列化器。需要继承 com.fasterxml.jackson.databind.JsonSerializer 以及实现 com.fasterxml.jackson.databind.ser.ContextualSerializer
public class DecimalSerialize extends JsonSerializer<BigDecimal> implements ContextualSerializer {
private String param;
public DecimalSerialize() {
this("");
}
public DecimalSerialize(String param) {
this.param = param;
}
/**
具体实现序列化操作的地方
*/
@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
String format = null;
//是否为空
if (value != null) {
//格式化是否为空
if (null != param && !"".equals(param)) {
DecimalFormat decimalFormat = new DecimalFormat(param);
format = decimalFormat.format(value);
} else {
format = value.toString();
}
}
gen.writeString(format);
}
/**
com.fasterxml.jackson.databind.ser.ContextualSerializer#createContextual
返回一个 序列化器
作用: 通过该方法判断是否需要使用其他序列化器来序列化该值
*/
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
// 为空直接跳过
if (property != null) {
//非BigDecimal类型跳过
if (Objects.equals(property.getType().getRawClass(), BigDecimal.class)) {
BigDecimalFormat format = property.getAnnotation(BigDecimalFormat.class);
if (format == null) {
format = property.getContextAnnotation(BigDecimalFormat.class);
}
if (format != null) {
// 如果能得到注解,就将注解的 value 传入 ImageURLSerialize
return new DecimalSerialize(format.value());
}
}
return prov.findValueSerializer(property.getType(), property);
}
return prov.findNullValueSerializer(null);
}
}
- 将 BigDecimalFormat 注解标注在需要格式化的字段上,在返给前端的时候,数据就会被格式化