소스 검색

优化手机号脱敏逻辑

woody 9 달 전
부모
커밋
e2a37491c2

+ 39 - 0
framework-common/src/main/java/com/chelvc/framework/common/util/ChineseUtils.java

@@ -175,6 +175,23 @@ public final class ChineseUtils {
         return datetime == null ? null : CHINESE_DATETIME_MINUTE_FORMATTER.format(datetime);
     }
 
+    /**
+     * 判断是否是中文数字
+     *
+     * @param text 数字文本
+     * @return true/false
+     */
+    public static boolean isChineseNumber(String text) {
+        if (StringUtils.notEmpty(text)) {
+            for (int i = 0, size = text.length(); i < size; i++) {
+                if (text.charAt(i) > 9) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     /**
      * 将中文数字转换成阿拉伯数字
      *
@@ -378,6 +395,28 @@ public final class ChineseUtils {
             this.aliases = Collections.unmodifiableSet(Sets.newHashSet(aliases));
         }
 
+        /**
+         * 阿拉伯数字格式化
+         *
+         * @param text 数字文本
+         * @return 阿拉伯数字字符串
+         */
+        public static String format(String text) {
+            if (StringUtils.isEmpty(text)) {
+                return text;
+            }
+            boolean changed = false;
+            char[] chars = text.toCharArray();
+            for (int i = 0; i < chars.length; i++) {
+                Number number = valueOf(chars[i]);
+                if (number != null) {
+                    chars[i] = Character.forDigit(number.value, 10);
+                    changed = true;
+                }
+            }
+            return changed ? new String(chars) : text;
+        }
+
         /**
          * 根据别名获取中文数字枚举
          *

+ 36 - 8
framework-common/src/main/java/com/chelvc/framework/common/util/MobileUtils.java

@@ -24,6 +24,14 @@ public final class MobileUtils {
      */
     public static final String DEFAULT_DESENSITIZE_EXPRESSION = "---****-?";
 
+    /**
+     * 脱敏手机号默认匹配模式
+     */
+    private static final Pattern DEFAULT_DESENSITIZE_PATTERN = Pattern.compile(
+            "[1一壹][^0-9\\u4e00-\\u9fa5]*[3-9零〇一壹二贰弍三叁弎仨四肆五伍六陆七柒八捌九玖]" +
+                    "([^0-9\\u4e00-\\u9fa5]*[0-9零〇一壹二贰弍三叁弎仨四肆五伍六陆七柒八捌九玖]){9}"
+    );
+
     /**
      * 手机号处理工具实例
      */
@@ -34,12 +42,6 @@ public final class MobileUtils {
      */
     private static final PhoneNumberOfflineGeocoder GEOCODER = PhoneNumberOfflineGeocoder.getInstance();
 
-    /**
-     * 手机号脱敏匹配模式
-     */
-    private static final Pattern DESENSITIZE_MOBILE_PATTERN
-            = Pattern.compile("1[\\.\\-—~~_ ]*[3-9]([\\.\\-—~~_ ]*[0-9]){9}");
-
     private MobileUtils() {
     }
 
@@ -97,7 +99,7 @@ public final class MobileUtils {
      * @return true/false
      */
     public static boolean isMobile(String mobile, @NonNull Locale locale) {
-        return StringUtils.notEmpty(mobile) && mobile.length() == 11 && isMobile(parse(mobile, locale));
+        return isMobile(parse(mobile, locale));
     }
 
     /**
@@ -152,6 +154,17 @@ public final class MobileUtils {
         return desensitize(text, DEFAULT_DESENSITIZE_EXPRESSION);
     }
 
+    /**
+     * 脱敏手机号
+     *
+     * @param text    文本内容
+     * @param pattern 手机号匹配模式
+     * @return 文本内容
+     */
+    public static String desensitize(String text, @NonNull Pattern pattern) {
+        return desensitize(text, pattern, DEFAULT_DESENSITIZE_EXPRESSION);
+    }
+
     /**
      * 脱敏手机号
      *
@@ -160,6 +173,18 @@ public final class MobileUtils {
      * @return 文本内容
      */
     public static String desensitize(String text, @NonNull String expression) {
+        return desensitize(text, DEFAULT_DESENSITIZE_PATTERN, expression);
+    }
+
+    /**
+     * 脱敏手机号
+     *
+     * @param text       文本内容
+     * @param pattern    手机号匹配模式
+     * @param expression 手机号脱敏模式表达式
+     * @return 文本内容
+     */
+    public static String desensitize(String text, @NonNull Pattern pattern, @NonNull String expression) {
         if (StringUtils.isEmpty(text)) {
             return text;
         }
@@ -167,7 +192,7 @@ public final class MobileUtils {
         int offset = 0;
         StringBuilder buffer = null;
         DesensitizeUtils.Mode mode = null;
-        Matcher matcher = DESENSITIZE_MOBILE_PATTERN.matcher(text);
+        Matcher matcher = pattern.matcher(text);
         while (matcher.find()) {
             if (buffer == null) {
                 buffer = new StringBuilder();
@@ -180,6 +205,9 @@ public final class MobileUtils {
             if (mobile.length() > 11) {
                 mobile = StringUtils.clear(mobile);
             }
+            if (ChineseUtils.isChineseNumber(mobile)) {
+                mobile = ChineseUtils.Number.format(mobile);
+            }
             buffer.append(mode.desensitize(mobile));
             offset = matcher.end();
         }

+ 2 - 2
framework-feign/src/main/java/com/chelvc/framework/feign/interceptor/FeignFailureInterceptor.java

@@ -4,9 +4,9 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 
+import com.chelvc.framework.base.context.JacksonContextHolder;
 import com.chelvc.framework.base.context.Result;
 import com.chelvc.framework.common.exception.FrameworkException;
-import com.chelvc.framework.common.util.JacksonUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import feign.Response;
 import feign.codec.ErrorDecoder;
@@ -25,7 +25,7 @@ public class FeignFailureInterceptor implements ErrorDecoder {
      * @return 请求结果
      */
     protected Result<?> deserialize(String body) {
-        return JacksonUtils.deserialize(body, Result.class);
+        return JacksonContextHolder.deserialize(body, Result.class);
     }
 
     @Override