- 对包含身份证号码进行脱敏
public static String hideIdCard(String idStr) {
if (StringUtils.isNotEmpty(idStr)) {
Pattern IDCARD_PATTERN = Pattern.compile("((\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|[Xx])|\\d{15})");
Matcher matcher = IDCARD_PATTERN.matcher(idStr);
if (matcher.find()) {
StringBuilder idCardBuffer = new StringBuilder(matcher.group(0));
int index = idStr.indexOf(idCardBuffer.toString());
String header = idStr.substring(0, index);
String tail = idStr.substring(idCardBuffer.length() + header.length());
if (idCardBuffer.length() == 15) {
return header + idCardBuffer.replace(3, 13, "**********").toString() + tail;
} else {
return header + idCardBuffer.replace(3, 16, "*************").toString() + tail;
}
}
}
return idStr;
}
- 对手机号码前三后四脱敏
public static String encryptMobile(String mobile) {
if (StringUtils.isNotEmpty(mobile)) {
Pattern IDCARD_PATTERN = Pattern.compile("(\\d{3})\\d{4}(\\d{4})");
Matcher matcher = IDCARD_PATTERN.matcher(mobile);
if (matcher.find()) {
StringBuilder idCardBuffer = new StringBuilder(matcher.group(0));
int index = mobile.indexOf(idCardBuffer.toString());
String header = mobile.substring(0, index);
String tail = mobile.substring(idCardBuffer.length() + header.length());
if (idCardBuffer.length() == 11) {
return header + idCardBuffer.replace(3, 7, "****").toString() + tail;
}
}
}
return mobile;
}
- Map中交换key与value位置,相同value的key用集合进行存储
public static <T,S> Map<T, List<S>> exchangeKeyValue(Map<S,T> map){
Map<T, List<S>> stringListHashMap = new HashMap<>(16);
for (Map.Entry<S, T> entry : map.entrySet()) {
S key = entry.getKey();
T value = entry.getValue();
if (stringListHashMap.containsKey(value)) {
List<S> list3 = stringListHashMap.get(value);
list3.add(key);
stringListHashMap.put(value, list3);
} else {
List<S> list4 = new ArrayList<>();
list4.add(key);
stringListHashMap.put(value, list4);
}
}
return stringListHashMap;
}
- 固话进行脱敏
public static String phoneNum(String telnum) {
if (StringUtils.isNotEmpty(telnum)) {
Pattern pattern = Pattern.compile("(\\d{4})-(\\d{4})(\\d{4})");
Matcher matcher = pattern.matcher(telnum);
return matcher.replaceAll("$1-****$3");
}
return telnum;
}
- 名字脱敏
private static String nameDesensitization(String fullName) {
if (!Strings.isNullOrEmpty(fullName)) {
String name = StringUtils.left(fullName, 1);
return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
}
return fullName;
}
- 地址脱敏
private static String desensitizedAddress(String address){
if (!Strings.isNullOrEmpty(address)) {
return StringUtils.left(address, 3).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(address, address.length()-11), StringUtils.length(address), "*"), "***"));
}
return address;
}
- 身份证号码进行脱敏
public static String idEncrypt(String id) {
if (StringUtils.isEmpty(id) || (id.length() < 8)) {
return id;
}
return id.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
}