加油,打工人,今天又是美好的一天

十四郎君

一日不思量,也攒眉千度

java使用注解实现字段脱敏

最近进行接口迁移的时候,有些敏感信息需要脱敏透出,但一个一个的调用脱敏工具类比较冗余,为了降低代码耦合,就想着用注解实现下

正文

1. 定义一个注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveJsonSerializer.class)
public @interface Sensitive {
    //脱敏策略
    SensitiveStrategy strategy();
}

2. 定制一个脱敏策略

根据业务需求,实现脱敏规则(详细脱敏规则就不放出来了,各位可以自行百度,或者在hutool上找)

public enum SensitiveStrategy {
    USERNAME(SenstiveFieldUtil::senstiveName),

    IDCLARD(SenstiveFieldUtil::senstiveIdCard),

    PHONE(SenstiveFieldUtil::senstivePhone),

    ADDRESS(SenstiveFieldUtil::senstiveAddress);

    private final Function<String, String> desensitizer;

    SensitiveStrategy(Function<String, String> desensitizer){
        this.desensitizer = desensitizer;
    }

    public Function<String, String> densitizer(){ return desensitizer;}
}

3. 定制json序列化实现

public class SensitiveJsonSerializer extends JsonSerializer<String> implements ContextualSerializer {
    private SensitiveStrategy 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 {

        Sensitive annotation = property.getAnnotation(Sensitive.class);
        if (Objects.nonNull(annotation)&&Objects.equals(String.class, property.getType().getRawClass())) {
            this.strategy = annotation.strategy();
            return this;
        }
        return prov.findValueSerializer(property.getType(), property);

    }
}

4. 定制json序列化实现

在透出的实体类中进行字段脱敏,使用@Sensitive注解

@Data
public class UserInfo {
    /**
     * 真实姓名
     */
    @Sensitive(strategy = SensitiveStrategy.USERNAME)
    private String realName;
    /**
     * 地址
     */
    @Sensitive(strategy = SensitiveStrategy.ADDRESS)
    private String address;
    /**
     * 电话号码
     */
    @Sensitive(strategy = SensitiveStrategy.PHONE)
    private String phoneNumber;
    /**
     * 身份证号码
     */
    @Sensitive(strategy = SensitiveStrategy.IDCARD)
    private String idCard;
}

5. 测试

@RestController
public class TestController {
    @GetMapping("/test")
    public Person test(){
        Person user = new Person();
        user.setRealName("王瑞");
        user.setPhoneNumber("19796328206");
        user.setAddress("四川省成都市武侯区倪家桥路地铁站3栋1204");
        user.setIdCard("4333333333334334333");
        return user;
    }
}

结果:

{
    "realName": "*瑞",
    "address": "四川省成都市************3栋1204",
    "phoneNumber": "197****8206",
    "idCard": "4333****34333"
}

 

posted @ 2023-04-22 23:59  十郎  阅读(371)  评论(0编辑  收藏  举报