package cn.com.goldenwater.dcproj.utils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class UserMsgSectrityUtils { private Logger logger = LoggerFactory.getLogger(getClass()); /** * 手机号脱敏筛选正则 */ public static final String PHONE_BLUR_REGEX = "(\\d{3})\\d{4}(\\d{4})"; /** * 手机号脱敏替换正则 */ public static final String PHONE_BLUR_REPLACE_REGEX = "$1****$2"; /** * 手机号格式校验 * * @param phone * @return */ public static final boolean checkPhone(String phone) { System.out.println("phone--->" + phone); if (StringUtils.isEmpty(phone)) { return false; } return isMobileNO(phone); } public static final String blurPhone(String phone) { System.out.println("phone--->" + phone); boolean checkFlag = isMobileNO(phone); if (!checkFlag) { throw new IllegalArgumentException("手机号格式不正确!"); } return phone.replaceAll(PHONE_BLUR_REGEX, PHONE_BLUR_REPLACE_REGEX); } public static final String blurPhoneNotExc(String phone) { if (cn.com.goldenwater.dcproj.utils.StringUtils.isBlank(phone)) { return phone; } System.out.println("phone--->" + phone); boolean checkFlag = isMobileNO(phone); if (!checkFlag) { return phone; } return phone.replaceAll(PHONE_BLUR_REGEX, PHONE_BLUR_REPLACE_REGEX); } public static String desensitizedName(String fullName) { if (StringUtils.isNotBlank(fullName)) { String name = StringUtils.left(fullName, 1); return StringUtils.rightPad(name, StringUtils.length(fullName), "*"); } return fullName; } /** * 判断是否为手机号 */ public static boolean isMobileNO(String mobile) { /* if (mobile.length() != 11) { return false; }else{ *//** * 移动号段正则表达式 *//* String pat1 = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(198)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$"; *//** * 联通号段正则表达式 *//* String pat2 = "^((13[0-2])|(145)|(15[5-6])|(166)|(176)|(175)|(18[5,6]))\\d{8}|(1709)\\d{7}$"; *//** * 电信号段正则表达式 *//* String pat3 = "^((133)|(153)|(177)|(173)|(171)|(174)|(162)|(199)|(191)|(18[0,1,9])|(149))\\d{8}$"; *//** * 虚拟运营商正则表达式 *//* String pat4 = "^((170))\\d{8}|(1718)|(1719)\\d{7}$"; Pattern pattern1 = Pattern.compile(pat1); Matcher match1 = pattern1.matcher(mobile); boolean isMatch1 = match1.matches(); if(isMatch1){ return true; } Pattern pattern2 = Pattern.compile(pat2); Matcher match2 = pattern2.matcher(mobile); boolean isMatch2 = match2.matches(); if(isMatch2){ return true; } Pattern pattern3 = Pattern.compile(pat3); Matcher match3 = pattern3.matcher(mobile); boolean isMatch3 = match3.matches(); if(isMatch3){ return true; } Pattern pattern4 = Pattern.compile(pat4); Matcher match4 = pattern4.matcher(mobile); boolean isMatch4 = match4.matches(); if(isMatch4){ return true; } return false;*/ /* }*/ return PhoneFormatCheckUtils.isPhoneLegal(mobile); } public static void main(String[] args) { System.out.println(blurPhone("15810057885")); } }