DataMaskingUtil.java
1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cn.com.glsx.entmicro.util;
import io.netty.util.internal.StringUtil;
import org.springframework.util.StringUtils;
/**
* @author wangxw
* @version 1.0.0
* @ClassName DataMaskingUtil.java
* @description: 脱敏工具类
* @createTime 2021年11月13日 10:48:00
*/
public class DataMaskingUtil {
private static final String MASK_PHONE_REGEX = "(\\d{3})\\d*(\\d{4})";
private static final String MASK_PHONE_REPLACEMENT = "$1****$2";
/**
* 手机号脱敏
* @param phoneSource
* @return
*/
public static String maskPhone(String phoneSource) {
if (StringUtils.isEmpty(phoneSource)) {
return StringUtil.EMPTY_STRING;
}
String phoneMask = phoneSource.replaceAll(MASK_PHONE_REGEX, MASK_PHONE_REPLACEMENT);
return phoneMask;
}
/**
* 手机号后四位加密
*/
public static String encryptionPhone(String phone){
if (StringUtils.isEmpty(phone) || phone.length() < 4) {
return StringUtil.EMPTY_STRING;
}
String substring = phone.substring(0, phone.length() - 4);
String targetPhone = substring + "****";
return targetPhone;
}
}