Ver Fonte

身份证隐私算法调整

lideyin há 5 anos atrás
pai
commit
a2c633c882

+ 70 - 33
src/main/java/cn/com/qmth/examcloud/support/helper/IdentityNumberHelper.java

@@ -1,43 +1,80 @@
 package cn.com.qmth.examcloud.support.helper;
 
-import org.apache.commons.lang3.StringUtils;
-
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.support.Constants;
 import cn.com.qmth.examcloud.support.cache.CacheHelper;
 import cn.com.qmth.examcloud.support.cache.bean.OrgPropertyCacheBean;
+import org.apache.commons.lang3.StringUtils;
 
 public class IdentityNumberHelper {
 
-	public static String conceal(Long rootOrgId,String identityNumber) {
-		if(rootOrgId==null) {
-			throw new StatusException("1001", "rootOrgId不能为空");
-		}
-		if (!identityNumbeConceal(rootOrgId)) {
-			return identityNumber;
-		}
-		if (StringUtils.isBlank(identityNumber)) {
-			return identityNumber;
-		}
-		if (identityNumber.length() < 8) {
-			return identityNumber;
-		}
-		StringBuilder sb = new StringBuilder(identityNumber);
-		sb.replace(identityNumber.length() - 8, identityNumber.length() - 2, "******");
-		return sb.toString();
-	}
-
-	private static boolean identityNumbeConceal(Long rootOrgId) {
-		OrgPropertyCacheBean orgProperty = CacheHelper.getOrgProperty(rootOrgId, Constants.ID_NUMBER_PRIVATE_MODE_KEY);
-		
-		if(orgProperty==null) {
-			return false;
-		}
-
-		if (orgProperty.getHasValue()) {
-			return "true".equals(orgProperty.getValue());
-		}else {
-			return false;
-		}
-	}
+    /**
+     * 身份证从右向左第3位开始的6位字符为*,不足6位按实际位数来替换
+     *
+     * @param rootOrgId
+     * @param identityNumber
+     * @return
+     */
+    public static String conceal(Long rootOrgId, String identityNumber) {
+        if (rootOrgId == null) {
+            throw new StatusException("1001", "rootOrgId不能为空");
+        }
+        if (!identityNumbeConceal(rootOrgId)) {
+            return identityNumber;
+        }
+        if (StringUtils.isBlank(identityNumber)) {
+            return identityNumber;
+        }
+        if (identityNumber.length() <= 2) {
+            return identityNumber;
+        }
+
+        int startIndex = (identityNumber.length() - 8) < 0 ? 0 : (identityNumber.length() - 8);
+
+        return replaceStrToSymbol(identityNumber, startIndex, identityNumber.length() - 2, "*");
+    }
+
+    /**
+     * 将字符串内指定字符替换为特殊符号(包头不包尾)
+     *
+     * @param str   源字符串
+     * @param start 起始索引
+     * @param end   结束索引
+     * @return
+     */
+    private static String replaceStrToSymbol(String str, int start, int end, String symbol) {
+        StringBuilder sb = new StringBuilder();
+        String preStr = "";
+        String sufStr = "";
+        if (start > 0) {
+            preStr = str.substring(0, start);
+        }
+        sb.append(preStr);
+
+        for (int i = start; i < end; i++) {
+            sb.append(symbol);
+        }
+
+        if (str.length() > end + 1) {
+            sufStr = str.substring(end);
+        }
+
+        sb.append(sufStr);
+
+        return sb.toString();
+    }
+
+    private static boolean identityNumbeConceal(Long rootOrgId) {
+        OrgPropertyCacheBean orgProperty = CacheHelper.getOrgProperty(rootOrgId, Constants.ID_NUMBER_PRIVATE_MODE_KEY);
+
+        if (orgProperty == null) {
+            return false;
+        }
+
+        if (orgProperty.getHasValue()) {
+            return "true".equals(orgProperty.getValue());
+        } else {
+            return false;
+        }
+    }
 }