Skip to content

Commit

Permalink
add privacy for email
Browse files Browse the repository at this point in the history
  • Loading branch information
liberhe committed Dec 16, 2023
1 parent 69deb61 commit c946aac
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.dl.officialsite.common.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;

@Target(ElementType.FIELD) // 作用在字段上
@Retention(RetentionPolicy.RUNTIME) // class文件中保留,运行时也保留,能通过反射读取到
@JacksonAnnotationsInside // 表示自定义自己的注解PrivacyEncrypt
@JsonSerialize(using = PrivacySerializer.class) // 该注解使用序列化的方式
public @interface PrivacyEncrypt {

/**
* 脱敏数据类型(没给默认值,所以使用时必须指定type)
*/
PrivacyTypeEnum type();

/**
* 前置不需要打码的长度
*/
int prefixNoMaskLen() default 1;

/**
* 后置不需要打码的长度
*/
int suffixNoMaskLen() default 1;

/**
* 用什么打码
*/
String symbol() default "*";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.dl.officialsite.common.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 lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Objects;

@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 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.dl.officialsite.common.privacy;


import lombok.Getter;

@Getter
public enum PrivacyTypeEnum {

/** 自定义(此项需设置脱敏的范围),也可以设置脱敏使用的字符*/
CUSTOMER,


/** 邮箱 */
EMAIL,
}
77 changes: 77 additions & 0 deletions src/main/java/com/dl/officialsite/common/privacy/PrivacyUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.dl.officialsite.common.privacy;

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, 3, 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();
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/dl/officialsite/member/Member.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.dl.officialsite.member;


import com.dl.officialsite.common.privacy.PrivacyEncrypt;
import com.dl.officialsite.common.privacy.PrivacyTypeEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;
Expand Down Expand Up @@ -36,6 +38,7 @@ public class Member implements Serializable
private String address;
@Column(unique=true,length = 40)
@NotNull
@PrivacyEncrypt(type = PrivacyTypeEnum.EMAIL)
private String email;
@Column(unique=true,length = 20)
@NotNull
Expand Down

0 comments on commit c946aac

Please sign in to comment.