Skip to content
广告❤️成为赞助商

字段脱敏

注解@Desensitization实现数据脱敏,内置手机号邮箱银行卡号姓名等15种常用脱敏规则

@Desensitization

  • 数据脱敏注解
  • type(): 脱敏类型,参考:DesensitizationTypeEnum
  • startInclude(): 脱敏开始位置(包含),{@link #type()}为{@link DesensitizationTypeEnum#CUSTOM}并且{@link #desensitizationHandler()}为{@link Void}时,会使用这里指定的位置处理
  • endExclude(): 脱敏结束位置(不包含),{@link #type()}为{@link DesensitizationTypeEnum#CUSTOM}并且{@link #desensitizationHandler()}为{@link Void}时,会使用这里指定的位置处理
  • desensitizationHandler(): 脱敏处理器,{@link #type()}为{@link DesensitizationTypeEnum#CUSTOM}时,会走这里配置的处理器. 脱敏处理器需实现{@link com.anwen.mongo.handlers.DesensitizationHandler}接口

示例

实体类使用

实体类字段使用@Desensitization标识,并且指定为姓名类型脱敏

java
public class User{
    @Desensitization(type = DesensitizationTypeEnum.CHINESE_NAME)
    private String userName;
}

自定义脱敏处理器

java
public class CustomDesensitizationHandler implements DesensitizationHandler {
    @Override
    public String desensitized(Field field, Object obj, int startInclude, int endExclude, DesensitizationTypeEnum desensitizedType) {
        return DesensitizedUtil.desensitized(String.valueOf(obj),startInclude,endExclude, desensitizedType);
    }
}
//实体类使用
public class User{
    @Desensitization(desensitizationHandler = CustomDesensitizationHandler.class)
    private String userName;
}