SpringBoot 使用自定义jackson注解实现数据脱敏

  1. 自定义jackson数据脱敏注解

/**
 * @author qx
 * @date 2023-01-09
 * @Descripion: 自定义jackson数据脱敏注解
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = DesensitizationJsonSerializer.class)
public @interface Desensitization {

    //脱敏策略
    DesensitizationStrategy strategy();
}
  1. 定制数据脱敏策略

/**
 * @author qx
 * @date 2023-01-09
 * @Descripion: 数据脱敏策略,针对不同的数据定制特定的策略
 */
public enum DesensitizationStrategy {
    /**
     * 用户名
     */
    USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")),
    /**
     * 身份证
     */
    ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2")),
    /**
     * 手机号
     */
    PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
    /**
     * 地址
     */
    ADDRESS(s -> s.replaceAll("(\\S{4})\\S{2}(\\S*)\\S{2}", "$1****"));


    private final Function desensitizer;

    DesensitizationStrategy(Function desensitizer){
        this.desensitizer = desensitizer;
    }

    public Function desensitizer(){
        return desensitizer;
    }
}
  1. 定制JSON序列号实现 对使用自定义脱敏注解的字段进行数据脱敏

/**
 * @author qx
 * @date 2023-01-09
 * @Descripion: 自定义Json序列化 用于对使用脱敏注解的字段进行数据脱敏
 */
public class DesensitizationJsonSerializer extends JsonSerializer implements ContextualSerializer {

    private DesensitizationStrategy strategy;

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws
            IOException {
        gen.writeString(strategy.desensitizer().apply(value));
    }
    /**
     * 获取属性上的注解属性
     */
    @Override
    public JsonSerializer createContextual(SerializerProvider prov, BeanProperty property) throws
            JsonMappingException {
        Desensitization annotation = property.getAnnotation(Desensitization.class);
        if (Objects.nonNull(annotation)&&Objects.equals(String.class,
                property.getType().getRawClass())) {
            this.strategy = annotation.strategy();
            return this;
        }
        return prov.findValueSerializer(property.getType(), property);
    }
}
  1. 定义一个实体类 对其数据脱敏

/**
 * @author qx
 * @date 2023-01-09
 * @Descripion: 自定义实体类
 */
@Getter
@Setter
public class Person {

    /**
     * 姓名
     */
    @Desensitization(strategy = DesensitizationStrategy.USERNAME)
    private String realName;

    /**
     * 地址
     */
    @Desensitization(strategy = DesensitizationStrategy.ADDRESS)
    private String address;

    /**
     * 电话号码
     */
    @Desensitization(strategy = DesensitizationStrategy.PHONE)
    private String phoneNumber;

    /**
     * 身份证号码
     */
    @Desensitization(strategy = DesensitizationStrategy.ID_CARD)
    private String idCard;



}
  1. 测试

/**
 * @author qx
 * @date 2023-01-09
 * @Descripion: 数据脱敏测试
 */
@RestController
public class TestController {

    @GetMapping("/test")
    public Person test(){
        Person user = new Person();
        user.setRealName("qx");
        user.setPhoneNumber("18888886666");
        user.setAddress("广西桂林市七星区");
        user.setIdCard("450322210227355544");
        return user;
    }
}
  1. 浏览器运行

SpringBoot 使用自定义jackson注解实现数据脱敏_第1张图片

你可能感兴趣的:(SpringBoot,spring,boot,后端,java)