利用注解,实现 SpringBoot 项目中的隐私数据脱敏
在接口返回数据前,在序列化的时候对敏感字段值进行处理,并且选用 jackson 的序列化来实现。
1、创建隐私数据类型枚举:PrivacyTypeEnum
package com.digital.common.core.privacy;
import lombok.Getter;
/**
/** 自定义(此项需设置脱敏的范围)*/
CUSTOMER,
/** 姓名 */
NAME,
/** 身份证号 */
ID_CARD,
/** 手机号 */
PHONE,
/** 邮箱 */
EMAIL,
}
2、创建自定义隐私注解:PrivacyEncrypt
package com.digital.common.core.privacy;
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;
/**
/**
/**
/**
/**
3、创建自定义序列化器:PrivacySerializer
package com.digital.common.core.privacy;
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 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);
}
}
4、隐私数据隐藏工具类:PrivacyUtil
package com.digital.common.core.privacy;
public class PrivacyUtil {
/**
/**
/**
/**
// /**
// * 【身份证号】显示前4位, 后2位
// /
// public static String hideIdCard(String idCard) {
// return desValue(idCard, 4, 2, "");
// }
// /**
// * 【手机号码】前三位,后四位,其他隐藏。
// /
// public static String hidePhone(String phone) {
// return desValue(phone, 3, 4, "");
// }
/**
public static void main(String[] args) {
System.out.println(hideChineseName(“张三三”));
}
}
5、注解使用
public class SysUser extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 用户邮箱 */
@Excel(name = “用户邮箱”)
private String email;
/** 手机号码 */
@Excel(name = “手机号码”)
@PrivacyEncrypt(type = PrivacyTypeEnum.PHONE) // 隐藏手机号
private String phonenumber;
}