f80f5a7d02b6dcda018a15ca9ee01d2c31bd4ff2.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if (cn.com.goldenwater.dcproj.utils.StringUtils.isBlank(phone)) {
  38. return phone;
  39. }
  40. System.out.println("phone--->" + phone);
  41. boolean checkFlag = isMobileNO(phone);
  42. if (!checkFlag) {
  43. return phone;
  44. }
  45. return phone.replaceAll(PHONE_BLUR_REGEX, PHONE_BLUR_REPLACE_REGEX);
  46. }
  47. public static String desensitizedName(String fullName) {
  48. if (StringUtils.isNotBlank(fullName)) {
  49. String name = StringUtils.left(fullName, 1);
  50. return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
  51. }
  52. return fullName;
  53. }
  54. /**
  55. * 判断是否为手机号
  56. */
  57. public static boolean isMobileNO(String mobile) {
  58. /* if (mobile.length() != 11)
  59. {
  60. return false;
  61. }else{
  62. *//**
  63. * 移动号段正则表达式
  64. *//*
  65. String pat1 = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(198)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
  66. *//**
  67. * 联通号段正则表达式
  68. *//*
  69. String pat2 = "^((13[0-2])|(145)|(15[5-6])|(166)|(176)|(175)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
  70. *//**
  71. * 电信号段正则表达式
  72. *//*
  73. String pat3 = "^((133)|(153)|(177)|(173)|(171)|(174)|(162)|(199)|(191)|(18[0,1,9])|(149))\\d{8}$";
  74. *//**
  75. * 虚拟运营商正则表达式
  76. *//*
  77. String pat4 = "^((170))\\d{8}|(1718)|(1719)\\d{7}$";
  78. Pattern pattern1 = Pattern.compile(pat1);
  79. Matcher match1 = pattern1.matcher(mobile);
  80. boolean isMatch1 = match1.matches();
  81. if(isMatch1){
  82. return true;
  83. }
  84. Pattern pattern2 = Pattern.compile(pat2);
  85. Matcher match2 = pattern2.matcher(mobile);
  86. boolean isMatch2 = match2.matches();
  87. if(isMatch2){
  88. return true;
  89. }
  90. Pattern pattern3 = Pattern.compile(pat3);
  91. Matcher match3 = pattern3.matcher(mobile);
  92. boolean isMatch3 = match3.matches();
  93. if(isMatch3){
  94. return true;
  95. }
  96. Pattern pattern4 = Pattern.compile(pat4);
  97. Matcher match4 = pattern4.matcher(mobile);
  98. boolean isMatch4 = match4.matches();
  99. if(isMatch4){
  100. return true;
  101. }
  102. return false;*/
  103. /* }*/
  104. return PhoneFormatCheckUtils.isPhoneLegal(mobile);
  105. }
  106. public static void main(String[] args) {
  107. System.out.println(blurPhone("15810057885"));
  108. }
  109. }