-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/com/dl/officialsite/common/privacy/PrivacyEncrypt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "*"; | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/dl/officialsite/common/privacy/PrivacySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/dl/officialsite/common/privacy/PrivacyTypeEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
src/main/java/com/dl/officialsite/common/privacy/PrivacyUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters