使用注解 做数据脱敏

参考 https://blog.csdn.net/wcj_java/article/details/128583045?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-128583045-blog-119926641.235^v38^pc_relevant_anti_t3_base&spm=1001.2101.3001.4242.2&utm_relevant_index=4

 

1、新建注解,指定自定义的 json序列化。

@JacksonAnnotationsInside // 表示自定义自己的注解PrivacyEncrypt
@JsonSerialize(using = PrivacySerializer.class) // 该注解使用序列化的方式
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
/**
 * 自定义数据脱敏注解
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside // 表示自定义自己的注解PrivacyEncrypt
@JsonSerialize(using = PrivacySerializer.class) // 该注解使用序列化的方式
public @interface PrivacyEncrypt {
 
    /**
     * 脱敏数据类型
     */
    PrivacyTypeEnum type();
 
    /**
     * 前置不需要打码的长度
     */
    int prefixNoMaskLen() default 1;
 
    /**
     * 后置不需要打码的长度
     */
    int suffixNoMaskLen() default 1;
 
    /**
     * 用什么打码
     */
    String symbol() default "*";
}

 

2、给出序列化

import com.fasterxml.jackson.core.JsonGenerator;
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;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
 
@NoArgsConstructor
@AllArgsConstructor
public class PrivacySerializer extends JsonSerializer<String> implements ContextualSerializer {
 
  // 脱敏类型
  private PrivacyTypeEnum privacyTypeEnum;
  // 前几位不脱敏
  private Integer prefixNoMaskLen;
  // 最后几位不脱敏
  private Integer suffixNoMaskLen;
  // 用什么打码
  private String symbol;
 
  @Override
  public void serialize(final String origin, final JsonGenerator jsonGenerator,
      final SerializerProvider serializerProvider) throws IOException {
    if (StringUtils.isNotBlank(origin) && null != privacyTypeEnum) {
      switch (privacyTypeEnum) {
        case CUSTOMER:
          jsonGenerator.writeString(PrivacyUtil.desValue(origin, prefixNoMaskLen, suffixNoMaskLen, symbol));
          break;
        case NAME:
          jsonGenerator.writeString(PrivacyUtil.hideChineseName(origin));
          break;
        case ID_CARD:
          jsonGenerator.writeString(PrivacyUtil.hideIDCard(origin));
          break;
        case PHONE:
          jsonGenerator.writeString(PrivacyUtil.hidePhone(origin));
          break;
        case EMAIL:
          jsonGenerator.writeString(PrivacyUtil.hideEmail(origin));
          break;
        default:
          throw new IllegalArgumentException("unknown privacy type enum " + privacyTypeEnum);
      }
    }
  }
 
  @Override
  public JsonSerializer<?> createContextual(final SerializerProvider serializerProvider,
      final BeanProperty beanProperty) throws JsonMappingException {
    if (beanProperty != null) {
      if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
        PrivacyEncrypt privacyEncrypt = beanProperty.getAnnotation(PrivacyEncrypt.class);
        if (privacyEncrypt == null) {
          privacyEncrypt = beanProperty.getContextAnnotation(PrivacyEncrypt.class);
        }
        if (privacyEncrypt != null) {
          return new PrivacySerializer(privacyEncrypt.type(), privacyEncrypt.prefixNoMaskLen(),
              privacyEncrypt.suffixNoMaskLen(), privacyEncrypt.symbol());
        }
      }
      return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
    }
    return serializerProvider.findNullValueSerializer(null);
  }
}

 

3、给出工具类

public class PrivacyUtil {
 
    /**
     * 隐藏手机号中间四位
     */
//    public static String hidePhone(String phone) {
//        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
//    }
 
    /**
     * 隐藏邮箱
     */
    public static String hideEmail(String email) {
        return email.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4");
    }
 
    /**
     * 隐藏身份证
     */
    public static String hideIDCard(String idCard) {
        return idCard.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1*****$2");
    }
 
    /**
     * 【中文姓名】只显示第一个汉字
     */
    public static String hideChineseName(String chineseName) {
        if (chineseName == null) {
            return null;
        }
        return desValue(chineseName, 1, 0, "*");
    }
 
//    /**
//     * 【身份证号】显示前4位, 后2位
//     */
//    public static String hideIdCard(String idCard) {
//        return desValue(idCard, 4, 2, "*");
//    }
 
    /**
     * 【手机号码】前三位,后四位,其他隐藏。
     */
    public static String hidePhone(String phone) {
        return desValue(phone, 0, 4, "*");
    }
 
    /**
     * 对字符串进行脱敏操作
     * @param origin          原始字符串
     * @param prefixNoMaskLen 左侧需要保留几位明文字段
     * @param suffixNoMaskLen 右侧需要保留几位明文字段
     * @param maskStr         用于遮罩的字符串, 如'*'
     * @return 脱敏后结果
     */
    public static String desValue(String origin, int prefixNoMaskLen, int suffixNoMaskLen, String maskStr) {
        if (origin == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0, n = origin.length(); i < n; i++) {
            if (i < prefixNoMaskLen) {
                sb.append(origin.charAt(i));
                continue;
            }
            if (i > (n - suffixNoMaskLen - 1)) {
                sb.append(origin.charAt(i));
                continue;
            }
            sb.append(maskStr);
        }
        return sb.toString();
    }
 
    public static void main(String[] args) {
        System.out.println(hideChineseName("小明明"));
    }
}
import lombok.Getter;
 
/**
 * 隐私数据类型枚举
 */
@Getter
public enum PrivacyTypeEnum {
 
  /** 自定义(此项需设置脱敏的范围)*/
  CUSTOMER,
 
  /** 姓名 */
  NAME,
 
  /** 身份证号 */
  ID_CARD,
 
  /** 手机号 */
  PHONE,
 
  /** 邮箱 */
  EMAIL,
}

 

4、效果展示

import lombok.Data;

@Data
public class People {
 
    private Integer id;

    @PrivacyEncrypt(type = PrivacyTypeEnum.NAME)
    private String name;
 
    private Integer sex;
 
    private Integer age;
 
    @PrivacyEncrypt(type = PrivacyTypeEnum.PHONE) // 隐藏手机号
    private String phone;
 
    @PrivacyEncrypt(type = PrivacyTypeEnum.EMAIL) // 隐藏邮箱
    private String email;
 
    private String sign;
}

 

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/desensitization")
public class PeopleController {

    @GetMapping("test01")
    public People test01(){
        People people = new People();
        people.setName("霍元甲");
        people.setPhone("12345678912");
        people.setEmail("123@qq.com");
        people.setId(1);
        people.setAge(18);
        people.setSign("sign");
        return people;
    }

}

 

 

posted on 2023-11-22 16:53  wuyicode  阅读(49)  评论(0编辑  收藏  举报