数据的脱敏
- 脱敏的规则
public class DesensitizationStringUtil {
// 银行账户:显示前五后五,范例:62284******84568
public static String encryptBankAcct(String bankAcct) {
if (StringUtils.isBlank(bankAcct)) {
return bankAcct;
}
if (bankAcct.length() > 10) {
bankAcct = replaceBetween(bankAcct, 5, bankAcct.length() - 5, null);
}
return bankAcct;
}
// 身份证号码:显示前五后四,范例:11060*********2015
public static String encryptIdCard(String idCard) {
if (StringUtils.isBlank(idCard)) {
return idCard;
}
if (idCard.length() == 15 || idCard.length() == 18) {
idCard = replaceBetween(idCard, 5, idCard.length() - 4, null);
}
return idCard;
}
// 手机:显示前三后四,范例:189****3684
public static String encryptPhone(String phone) {
if (StringUtils.isBlank(phone)) {
return phone;
}
if (phone.length() == 11) {
phone = replaceBetween(phone, 3, phone.length() - 4, null);
}
return phone;
}
/**
* 名字脱敏 规则,张三丰,脱敏为:张*丰
*
* @param name
* @return
*/
public static String encryptUserName(String name) {
if (name == null || name.isEmpty()) {
return "";
}
String myName = null;
char[] chars = name.toCharArray();
if (chars.length == 1) {
myName = name;
}
if (chars.length == 2) {
myName = name.replaceFirst(name.substring(1), "*");
}
if (chars.length > 2) {
myName = name.replaceAll(name.substring(1, chars.length - 1), "*");
}
return myName;
}
/**
* 将字符串开始位置到结束位置之间的字符用指定字符替换
*
* @param sourceStr
* 待处理字符串
* @param begin
* 开始位置
* @param end
* 结束位置
* @param replacement
* 替换字符
* @return
*/
private static String replaceBetween(String sourceStr, int begin, int end, String replacement) {
if (sourceStr == null) {
return "";
}
if (replacement == null) {
replacement = "*";