Quellcode durchsuchen

对象字典,业务表加密脱敏处理

skcj vor 2 Jahren
Ursprung
Commit
2e2bc500a7

+ 77 - 0
ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/utils/SecurityUtils.java

@@ -14,6 +14,7 @@ import com.ruoyi.system.api.model.LoginUser;
 import com.ruoyi.system.sm4.SM4Utils;
 import com.ruoyi.system.utils.Encryption;
 import com.ruoyi.system.utils.SubCodeUtil;
+import com.sun.javaws.Main;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 
 import javax.servlet.http.HttpServletRequest;
@@ -98,6 +99,74 @@ public class SecurityUtils
         sm4.secretKey = key;
         return sm4.decryptData_ECB(data);
     }
+    public static String maskA(String str, int cd) {
+        int cdc = str.length() - 2 * cd;
+        if (cdc > 0) {
+            String reptext = "";
+            for (int k = 0; k < cdc; k++) {
+                reptext = reptext + "✱";
+            }
+            return str.substring(0, cd) + reptext + str.substring(str.length() - cd);
+        }
+        else
+            return str;
+    }
+    public static String masks(String str,int type){
+        int strcd = str != null ? str.length() : 0;
+        if (strcd > 0) {
+            if (type == 1)    //姓名
+            {
+                if (strcd > 1) {
+                    int cdc = strcd - 1;
+                    String reptext = "";
+                    for (int k = 0; k < cdc; k++) {
+                        reptext = reptext + "✱";
+                    }
+                    return reptext + str.substring(cdc);
+                }
+                else {
+                    return str;
+                }
+            }
+            else if (type == 2)//身份证号
+            {
+                if (strcd == 18) {
+                    return str.substring(0, 3) + "✱✱✱✱✱" + str.substring(8, 9) + "✱✱✱✱✱✱✱" + str.substring(16, 17) + "✱";
+                }
+                else {
+                    return maskA(str, 1);
+                }
+            }
+            else if (type == 3)//手机号
+            {
+                if (strcd == 11) {
+                    return str.substring(0, 2) + "✱✱✱✱✱✱✱✱" + str.substring(10, 11);
+                }
+                else {
+                    return maskA(str, 2);
+                }
+            }
+            else if (type == 4)//住址
+            {
+                return maskA(str, 2);
+            }
+            else if (type == 5)//邮件地址
+            {
+                return maskA(str, 2);
+            }
+            else if (type == 6)//邮件地址
+            {
+                return maskA(str, 1);
+            }
+            else  //其他
+            {
+                return str;
+            }
+        }
+        else {
+            return str;
+        }
+    }
     public static String sm4encrypt_CBC(String key,String iv,String data){
         SM4Utils sm4 = new SM4Utils();
         sm4.secretKey = key;
@@ -315,4 +384,12 @@ public class SecurityUtils
                 || (isPureChar && hasSpecChar) || isNumAndChar && hasSpecChar) || isNumAndChar;
         return isLegal;
     }
+    public static void mainTest(String[] args)
+    {
+       System.out.println(masks("我开双方都三始",1));
+       System.out.println(masks("220122197601247014",2));
+       System.out.println(masks("13630594359",3));
+        System.out.println(masks("吉林省农安县",4));
+        System.out.println(masks("吉林省农安县",99));
+    }
 }

+ 22 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictTypeController.java

@@ -11,6 +11,7 @@ import com.ruoyi.common.log.enums.BusinessType;
 import com.ruoyi.common.redis.service.RedisService;
 import com.ruoyi.common.security.annotation.RequiresPermissions;
 import com.ruoyi.common.security.utils.AmountUtils;
+import com.ruoyi.common.security.utils.EncryptionUtils;
 import com.ruoyi.common.security.utils.SecurityUtils;
 import com.ruoyi.system.api.domain.SysDictType;
 import com.ruoyi.system.api.domain.SysObjDictRet;
@@ -289,6 +290,27 @@ public class SysDictTypeController extends BaseController
             String paramDataType[] = new String[]{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
             DBExecResult ret = db1.exeProcedureWithRet("getstatisticaldata", param, paramType, paramDataType);
             if (ret.isSuccessed() && ret.dbResultsize > 0) {
+                String bt=ret.getDBResult(0).getString(0,0);
+                System.out.println(bt);
+                String[] masksl=new String [bt.split(",").length];
+                String[] filedl=new String [bt.split(",").length];
+                for (int i = 0; i <masksl.length ; i++) {
+                    masksl[i]="0";
+                    filedl[i]=bt.split(",")[i].split("[|]")[2];
+                    if (bt.split(",")[i].split("[|]").length==5)
+                    {
+                        masksl[i]=bt.split(",")[i].split("[|]")[4];
+                    }
+                }
+                for (int i = 0; i <ret.getDBResult(1).getRecordCount() ; i++) {
+                    for (int j = 0; j <filedl.length ; j++) {
+                        if (!masksl[j].equals("0"))
+                        {
+                           String value=SecurityUtils.masks(SecurityUtils.sm4decrypt_ECB(EncryptionUtils.key, ret.getDBResult(1).getString(i,filedl[j])),Integer.parseInt(masksl[j]));
+                           ret.getDBResult(1).setString(i,filedl[j],value);
+                        }
+                    }
+                }
                 AjaxResult rets = AjaxResult.success(ret.getDbResult());
                 if (para10 != null && para10.equals("1")) {
                     DBImpl dbs = DBFactory.getDBImpl("slave");

+ 5 - 1
ruoyi-ui/src/api/tool/sm4.js

@@ -494,10 +494,14 @@ function mask(str, type) {
     {
       return maskA(str, 2);
     }
-    else  //其他
+    else if (type == 6)//邮件地址
     {
       return maskA(str, 1);
     }
+   else  //其他
+    {
+      return str;
+    }
   }
   else {
     return str