public class DesensitizationUtil { private static final String certDemo = "$1**************$2"; private static final String phoneNoDemo = "$1****$2"; private static final String custCertId = "(\\d{6})\\d{10}(\\w{2})"; private static final String phoneNo = "(\\d{3})\\d*(\\d{4})"; //姓名脱敏 public static String dealName(String name) { if (StringUtils.isEmpty(name)) { return ""; } if (name.length() == 2) { name = name.substring(0, 1) + "*"; } if (name.length() == 3) { String b = name.substring(2, 3); name = name.substring(0, 1) + "*" + b; } if (name.length() >= 4) { String b = name.substring(name.length() - 1, name.length()); String c = ""; for (int i = 0; i < name.length() - 2; i++) { c = c + "*"; } name = name.substring(0, 1) + c + b; } return name; } //手机号码脱敏:136****0762 public static String dealPhoneNo(String phone) { if (StringUtils.isNotEmpty(phone) && phone.length() == 11) { phone = phone.replaceAll(phoneNo, phoneNoDemo); } return phone; } //身份证脱敏:410327**********17 public static String dealCertNo(String certNo) { if (StringUtils.isNotEmpty(certNo) && certNo.length() == 18) { certNo = certNo.replaceAll(custCertId, certDemo); } return certNo; } //地址脱敏:直辖市保留前9位,其他省分保留前12位 public static String dealAddress(String address) { String a = ""; String c = ""; if (address.startsWith("北京市") || address.startsWith("上海市") || address.startsWith("天津市") || address.startsWith("重庆市")) { if (address.length() > 9) { a = address.substring(0, 9); c = address.substring(9); return a + c.replaceAll("[\\u4e00-\\u9fa5_a-zA-Z0-9]", "*"); } else { return address.replaceAll("[\\u4e00-\\u9fa5_a-zA-Z0-9]", "*"); } } else { if (address.length() > 12) { a = address.substring(0, 12); c = address.substring(12); return a + c.replaceAll("[\\u4e00-\\u9fa5_a-zA-Z0-9]", "*"); } else { return address.replaceAll("[\\u4e00-\\u9fa5_a-zA-Z0-9]", "*"); } } } }
一些常用的脱敏方法,姓名脱敏,手机号码脱敏,身份证脱敏,地址脱敏
最新推荐文章于 2025-01-16 23:44:38 发布
7665

被折叠的 条评论
为什么被折叠?



