千家信息网

基于序列化怎么实现jackson

发表于:2024-11-24 作者:千家信息网编辑
千家信息网最后更新 2024年11月24日,这篇文章主要介绍"基于序列化怎么实现jackson",在日常操作中,相信很多人在基于序列化怎么实现jackson问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"基于序列化
千家信息网最后更新 2024年11月24日基于序列化怎么实现jackson

这篇文章主要介绍"基于序列化怎么实现jackson",在日常操作中,相信很多人在基于序列化怎么实现jackson问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"基于序列化怎么实现jackson"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

基于序列化的实现

Fastjson实现

实现思路:

  1. 自定义注解,可让用户自定义脱敏方式,用于实体类的属性

  2. 基于ValueFilter进行 属性注解拦截,并多value进行替换脱敏

  3. 使用json序列化对象是指定自定义序列化Filter

核心代码如下

自定义注解Desensitization

@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface Desensitization {    /**     * 脱敏规则     *     * @return     */    DesensitionEnum desensitionEnum();}

脱敏方法

/** * 脱敏的函数接口 */public interface Desensitizer extends Function {}@Getterpublic enum DesensitionEnum {    /**     * 用户名脱敏     */    USERNAME("userName", "用户名",s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")),    /**     * 身份证号码脱敏     */    ID_CARD("idCard", "15或者18身份证号",s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2")),    /**     * 手机号脱敏     */    PHONE("phone", "手机号",s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),    /**     * 地址脱敏      */    ADDRESS("address", "地址脱敏",s -> s.replaceAll("(\\S{8})\\S{4}(\\S*)\\S{4}", "$1****$2****"));    String fieldName;    String fieldDescribe;    private final Desensitizer desensitizer;    DesensitionEnum(String fieldName, String fieldDescribe, Desensitizer desensitizer) {        this.fieldName = fieldName;        this.fieldDescribe = fieldDescribe;        this.desensitizer = desensitizer;    }}

具体拦截

public class FastjsonDesensitizeFilter implements ValueFilter {    @Override    public Object process(Object object, String name, Object value) {        if (null == value || !(value instanceof String) || ((String) value).length() == 0) {            return value;        }        try {            Field field = object.getClass().getDeclaredField(name);            Desensitization desensitization;            if (String.class != field.getType() || (desensitization = field.getAnnotation(Desensitization.class)) == null) {                return value;            }            return desensitization.desensitionEnum().getDesensitizer().apply((String) value);        } catch (Exception e) {            return value;        }    }}

Jackson实现

解释:

  1. 获取对象属性上的注解,根据属性得到相应的脱敏规则类型

  2. 按照规则类型进行value替换

自定义注解

@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@JsonSerialize(using = JacksonDesensitize.class)@JacksonAnnotationsInsidepublic @interface Desensitization {    /**     * 脱敏规则     *     * @return     */    DesensitionEnum desensitionEnum();}package com.demo.desensitization;import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.core.JsonProcessingException;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 java.io.IOException;import java.util.Objects;public class JacksonDesensitize extends JsonSerializer implements        ContextualSerializer {    private DesensitionEnum desensitionEnum;    @Override    public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {        jsonGenerator.writeString(desensitionEnum.getDesensitizer().apply((String) value));    }    @Override    public JsonSerializer createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {        if (beanProperty != null) {            // 非 String 直接跳过            if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {                // 获取注解信息                Desensitization annotation = beanProperty.getAnnotation(Desensitization.class);                if (annotation == null) {                    annotation = beanProperty.getContextAnnotation(Desensitization.class);                }                if (annotation != null) {                    // 获得注解上的值并赋值                    this.desensitionEnum = annotation.desensitionEnum();                    return this;                }            }            return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);        }        return serializerProvider.findNullValueSerializer(null);    }}

执行结果

package com.demo.desensitization;import lombok.Data;import java.io.Serializable;@Datapublic class UserDTO implements Serializable {    @Desensitization(desensitionEnum=DesensitionEnum.ID_CARD)    private String idCard;    @Desensitization(desensitionEnum=DesensitionEnum.USERNAME)    private String username;    @Desensitization(desensitionEnum=DesensitionEnum.PHONE)    private String phone;    @Desensitization(desensitionEnum=DesensitionEnum.ADDRESS)    private String address;}    public static void testFast(){        FastJsonConfig fastJsonConfig = new FastJsonConfig();        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect);        fastJsonConfig.setSerializeFilters(new FastjsonDesensitizeFilter());        UserDTO userDTO=new UserDTO();        userDTO.setUsername("张三");        userDTO.setPhone("15669057552");        userDTO.setIdCard("370826198901133299");        userDTO.setAddress("浙江省杭州市江干区火车东站");        String s = JSON.toJSONString(userDTO,new FastjsonDesensitizeFilter());        System.out.println(s);    }    public static void testJackson() throws JsonProcessingException {        UserDTO userDTO=new UserDTO();        userDTO.setUsername("张三");        userDTO.setPhone("15669057552");        userDTO.setIdCard("370826198901133299");        userDTO.setAddress("浙江省杭州市江干区火车东站");        ObjectMapper objectMapper = new ObjectMapper();        String value = objectMapper.writeValueAsString(userDTO);        System.out.println(value);    }

最终输出结果

{"idCard":"3708****3299","username":"张*","phone":"156****7552","address":"浙江省杭州市江干区火车东站"}

到此,关于"基于序列化怎么实现jackson"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0