项目中 遇到的一些 身份证 电话号码 邮箱等有些时候会进行保密处理
/**
* <b>DesensitionType</b>
*
* @description: DesensitionType <br>
* @date: 2021/1/19 13:55 <br>
* @author: molin <br>
* @version: 1.0 <br>
*/
public enum DesensitionType {
//用户id
USER_ID,
//中文名
CHINESE_NAME,
//身份证号
ID_CARD,
//座机号
FIXED_PHONE,
//手机号
MOBILE_PHONE,
//地址
ADDRESS,
//电子邮件
EMAIL,
//密码
PASSWORD;
}
/**
* <b>Desensitization</b>
*
* @description: Desensitization <br>
* @date: 2021/1/19 13:54 <br>
* @author: molin <br>
* @version: 1.0 <br>
*/
@JacksonAnnotationsInside
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JsonSerialize(using = DesensitizationSerialize.class)
public @interface Desensitization {
/**
* 脱敏类型规则
* @return
*/
DesensitionType value();
}
/**
* <b>DesensitizationSerialize</b>
*
* @description: DesensitizationSerialize <br>
* @date: 2021/1/19 13:55 <br>
* @author: molin <br>
* @version: 1.0 <br>
*/
public class DesensitizationSerialize extends JsonSerializer implements ContextualSerializer {
private DesensitionType type;
public DesensitizationSerialize(){}
public DesensitizationSerialize(final DesensitionType type){
this.type = type;
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty beanProperty) throws JsonMappingException {
if(beanProperty != null){
//获取字段是否有脱敏注解,有则创建一个序列化对象,并调用serialize方法
Desensitization desensitization = beanProperty.getAnnotation(Desensitization.class);
if(desensitization == null){
desensitization = beanProperty.getContextAnnotation(Desensitization.class);
}
// 如果定义了脱敏注解,就将需要脱敏的类型传入DesensitizationSerialize构造函数
if(desensitization != null){
return new DesensitizationSerialize(desensitization.value());
}
return provider.findValueSerializer(beanProperty.getType() , beanProperty);
}
return provider.findNullValueSerializer(beanProperty);
}
@Override
public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException {
switch (this.type){
// case USER_ID:
// jsonGenerator.writeNumber(DesensitizedUtils.userId());
// break;
// case CHINESE_NAME:
// jsonGenerator.writeString(DesensitizedUtils.chineseName(String.valueOf(value)));
// break;
case ID_CARD:
jsonGenerator.writeString(DesensitizedUtils.idCardNum(String.valueOf(value),3,3));
break;
// case FIXED_PHONE:
// jsonGenerator.writeString(DesensitizedUtils.fixedPhone(String.valueOf(value)));
// break;
case MOBILE_PHONE:
jsonGenerator.writeString(DesensitizedUtils.mobilePhone(String.valueOf(value)));
break;
case ADDRESS:
jsonGenerator.writeString(DesensitizedUtils.address(String.valueOf(value), 8));
break;
case EMAIL:
jsonGenerator.writeString(DesensitizedUtils.email(String.valueOf(value)));
break;
// case PASSWORD:
// jsonGenerator.writeString(DesensitizedUtils.password(String.valueOf(value)));
// break;
default:
}
}
}
/**
* <b>DesensitizedUtils</b>
*
* @description: DesensitizedUtils <br>
* @date: 2021/1/19 13:56 <br>
* @author: molin <br>
* @version: 1.0 <br>
*/
public class DesensitizedUtils {
/**
* 【用户id】不对外提供userId
* @return
*/
public static Long userId(){
return Long.valueOf(0);
}
/**
* 【中文姓名】只显示第一个汉字,其他隐藏为2个星号,比如:李**
*
* @param fullName
* @return
*/
public static String chineseName(String fullName) {
if (StringUtils.isBlank(fullName)) {
return "";
}
String name = StringUtils.left(fullName, 1);
return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
}
/**
* 【身份证号】前三位 和后三位
*
* @param front
* @param end
* @return
*/
public static String idCardNum(String idCardNum, int front, int end) {
//身份证不能为空
if (StringUtils.isEmpty(idCardNum)) {
return "";
}
//需要截取的长度不能大于身份证号长度
if ((front + end) > idCardNum.length()) {
return "";
}
//需要截取的不能小于0
if (front < 0 || end < 0) {
return "";
}
//计算*的数量
int asteriskCount = idCardNum.length() - (front + end);
StringBuffer asteriskStr = new StringBuffer();
for (int i = 0; i < asteriskCount; i++) {
asteriskStr.append("*");
}
String regex = "(\\w{" + String.valueOf(front) + "})(\\w+)(\\w{" + String.valueOf(end) + "})";
return idCardNum.replaceAll(regex, "$1" + asteriskStr + "$3");
}
/**
* 【固定电话 前四位,后两位
*
* @param num
* @return
*/
public static String fixedPhone(String num) {
if (StringUtils.isBlank(num)) {
return "";
}
return StringUtils.left(num, 4).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(num, 2), StringUtils.length(num), "*"), "****"));
}
/**
* 【手机号码】前三位,后两位,其他隐藏,比如135******10
*
* @param num
* @return
*/
public static String mobilePhone(String num) {
if (StringUtils.isBlank(num)) {
return "";
}
return StringUtils.left(num, 3).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(num, 2), StringUtils.length(num), "*"), "***"));
}
/**
* 【地址】只显示到地区,不显示详细地址,比如:北京市海淀区****
*
* @param address
* @param sensitiveSize 敏感信息长度
* @return
*/
public static String address(String address, int sensitiveSize) {
if (StringUtils.isBlank(address)) {
return "";
}
int length = StringUtils.length(address);
return StringUtils.rightPad(StringUtils.left(address, length - sensitiveSize), length, "*");
}
/**
* 【电子邮箱 邮箱前缀仅显示第一个字母,前缀其他隐藏,用星号代替,@及后面的地址显示,比如:d**@126.com>
*
* @param email
* @return
*/
public static String email(String email) {
if (StringUtils.isBlank(email)) {
return "";
}
int index = StringUtils.indexOf(email, "@");
if (index <= 1) {
return email;
} else {
return StringUtils.rightPad(StringUtils.left(email, 1), index, "*").concat(StringUtils.mid(email, index, StringUtils.length(email)));
}
}
/**
* 【密码】密码的全部字符都用*代替,比如:******
*
* @param password
* @return
*/
public static String password(String password) {
if (StringUtils.isBlank(password)) {
return "";
}
String pwd = StringUtils.left(password, 0);
return StringUtils.rightPad(pwd, StringUtils.length(password), "*");
}
}
/**
* <b>DTO</b>
*
* @description: XXXXXDto <br>
* @date: 2020/10/1 00:00 <br>
* @author: kfw-mbg <br>
* @version: 1.0 <br>
*/
public class XXXXXDto implements Serializable {
@ApiModelProperty(value = "证件号")
@Desensitization(value = DesensitionType.ID_CARD)
private String certNo;
}