7ce4540e71bb0878ab44a675f0e9613ec554285e.svn-base 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package cn.com.goldenwater.dcproj.utils;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. public class UserMsgSectrityUtils {
  6. private Logger logger = LoggerFactory.getLogger(getClass());
  7. /**
  8. * 手机号脱敏筛选正则
  9. */
  10. public static final String PHONE_BLUR_REGEX = "(\\d{3})\\d{4}(\\d{4})";
  11. /**
  12. * 手机号脱敏替换正则
  13. */
  14. public static final String PHONE_BLUR_REPLACE_REGEX = "$1****$2";
  15. /**
  16. * 手机号格式校验
  17. *
  18. * @param phone
  19. * @return
  20. */
  21. public static final boolean checkPhone(String phone) {
  22. System.out.println("phone--->" + phone);
  23. if (StringUtils.isEmpty(phone)) {
  24. return false;
  25. }
  26. return isMobileNO(phone);
  27. }
  28. public static final String blurPhone(String phone) {
  29. System.out.println("phone--->" + phone);
  30. boolean checkFlag = isMobileNO(phone);
  31. if (!checkFlag) {
  32. throw new IllegalArgumentException("手机号格式不正确!");
  33. }
  34. return phone.replaceAll(PHONE_BLUR_REGEX, PHONE_BLUR_REPLACE_REGEX);
  35. }
  36. public static final String blurPhoneNotExc(String phone) {
  37. System.out.println("phone--->" + phone);
  38. boolean checkFlag = isMobileNO(phone);
  39. if (!checkFlag) {
  40. return phone;
  41. }
  42. return phone.replaceAll(PHONE_BLUR_REGEX, PHONE_BLUR_REPLACE_REGEX);
  43. }
  44. public static String desensitizedName(String fullName) {
  45. if (StringUtils.isNotBlank(fullName)) {
  46. String name = StringUtils.left(fullName, 1);
  47. return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
  48. }
  49. return fullName;
  50. }
  51. /**
  52. * 判断是否为手机号
  53. */
  54. public static boolean isMobileNO(String mobile) {
  55. /* if (mobile.length() != 11)
  56. {
  57. return false;
  58. }else{
  59. *//**
  60. * 移动号段正则表达式
  61. *//*
  62. String pat1 = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(198)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
  63. *//**
  64. * 联通号段正则表达式
  65. *//*
  66. String pat2 = "^((13[0-2])|(145)|(15[5-6])|(166)|(176)|(175)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
  67. *//**
  68. * 电信号段正则表达式
  69. *//*
  70. String pat3 = "^((133)|(153)|(177)|(173)|(171)|(174)|(162)|(199)|(191)|(18[0,1,9])|(149))\\d{8}$";
  71. *//**
  72. * 虚拟运营商正则表达式
  73. *//*
  74. String pat4 = "^((170))\\d{8}|(1718)|(1719)\\d{7}$";
  75. Pattern pattern1 = Pattern.compile(pat1);
  76. Matcher match1 = pattern1.matcher(mobile);
  77. boolean isMatch1 = match1.matches();
  78. if(isMatch1){
  79. return true;
  80. }
  81. Pattern pattern2 = Pattern.compile(pat2);
  82. Matcher match2 = pattern2.matcher(mobile);
  83. boolean isMatch2 = match2.matches();
  84. if(isMatch2){
  85. return true;
  86. }
  87. Pattern pattern3 = Pattern.compile(pat3);
  88. Matcher match3 = pattern3.matcher(mobile);
  89. boolean isMatch3 = match3.matches();
  90. if(isMatch3){
  91. return true;
  92. }
  93. Pattern pattern4 = Pattern.compile(pat4);
  94. Matcher match4 = pattern4.matcher(mobile);
  95. boolean isMatch4 = match4.matches();
  96. if(isMatch4){
  97. return true;
  98. }
  99. return false;*/
  100. /* }*/
  101. return PhoneFormatCheckUtils.isPhoneLegal(mobile);
  102. }
  103. public static void main(String[] args) {
  104. System.out.println(blurPhone("15810057885"));
  105. }
  106. }