一、簡述
數據脫敏是指對某些敏感信息通過脫敏規則進行數據的變形,實現敏感隱私數據的可靠保護。
簡單來說就是你有些數據並不想讓別人看見,需要進行處理再顯示在頁面上。如:(“篩選****地區”)
二、放代碼(脫敏方法工具類:調用即可)
1 public class RptUtils { 2 3 private static final int SIZE = 4; 4 private static final String SYMBOL = "*"; 5 6 /** 7 * 通用脫敏方法 8 * @param value 9 * @return 10 */ 11 public static String commonDisplay(String value) { 12 if (null == value || "".equals(value)) { 13 return value; 14 } 15 int len = value.length(); 16 int pamaone = len / 2; 17 int pamatwo = pamaone - 1; 18 int pamathree = len % 2; 19 StringBuilder stringBuilder = new StringBuilder(); 20 if (len <= 2) { 21 if (pamathree == 1) { 22 return SYMBOL; 23 } 24 stringBuilder.append(SYMBOL); 25 stringBuilder.append(value.charAt(len - 1)); 26 } else { 27 if (pamatwo <= 0) { 28 stringBuilder.append(value.substring(0, 1)); 29 stringBuilder.append(SYMBOL); 30 stringBuilder.append(value.substring(len - 1, len)); 31 } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) { 32 int pamafive = (len - SIZE) / 2; 33 stringBuilder.append(value.substring(0, pamafive)); 34 for (int i = 0; i < SIZE; i++) { 35 stringBuilder.append(SYMBOL); 36 } 37 if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) { 38 stringBuilder.append(value.substring(len - pamafive, len)); 39 } else { 40 stringBuilder.append(value.substring(len - (pamafive + 1), len)); 41 } 42 } else { 43 int pamafour = len - 2; 44 stringBuilder.append(value.substring(0, 1)); 45 for (int i = 0; i < pamafour; i++) { 46 stringBuilder.append(SYMBOL); 47 } 48 stringBuilder.append(value.substring(len - 1, len)); 49 } 50 } 51 return stringBuilder.toString(); 52 } 53 }
三、在你需要的業務邏輯上調用以上工具類方法
@Override 2 public List<List<Object>> selectSysCrmDeptCollectionCount(String startTime, String endTime) { 3 List<Map<String, Object>> maps = countMapper.selectSysCrmDeptCollectionCount(startTime, endTime); 4 //for 循環取值 5 for (int i = 0; i < maps.size(); i++){ 6 Map<String, Object> objects = maps.get(i); 7 //因業務需求,我存放的是Map類型。直接拿到put里的key:“money”,在調用commonDisplay脫敏工具類 8 String str = RptUtils.commonDisplay(objects.get("money").toString()); 9 //這里金額有小數,業務需求不要小數,直接用的截取從下標0開始截取到 ‘.’ 結束。 10 String money = str.substring(0, str.indexOf(".")); //最后賦值到指定位置 11 objects.put("money", money); 12 } 13 return RptUtils.convert(maps); 14 }
注:如果你要前端分頁展示某個字段脫敏可以:
1 //渲染到前端的分頁數據集 2 List<ItemCommentVO> list = itemsMapperCustom.queryItemComments(map); 3 for (ItemCommentVO vo : list ) { 4 //看那個字段需脫敏,直接set,get 即可 5 vo.setNickName(DesensitizationUtil.commonDisplay(vo.getNickName())); 6 }
四、更新——————————其他脫敏實現
上面只是對於大部分通用的信息進行脫敏,但是針對於不同的需求如何實現呢,以下是從網上搜集的關於不同信息脫敏的實現
1 import org.apache.commons.lang3.StringUtils; 2 public class MaskUtil { 3 /** 4 * 手機號顯示首3末4位,中間用*號隱藏代替,如:188****5593 5 * 6 * @param mobile 7 * @return 8 */ 9 public static String maskMobile(String mobile) { 10 if(StringUtils.isBlank(mobile) || mobile.length() <= 8) { 11 return mobile; 12 } 13 return wordMask(mobile, 3, 4, "*"); 14 } 15 16 /** 17 * 電話號碼顯示區號及末4位,中間用*號隱藏代替,如:055****6666 18 * 19 * @param telephone 20 * @return 21 */ 22 public static String maskTelephone(String telephone) { 23 if(StringUtils.isBlank(telephone)) { 24 return telephone; 25 } 26 String result; 27 if (telephone.length() > 8) { 28 if (telephone.contains("-")) { 29 String[] temp = telephone.split("-"); 30 result = temp[0] + "****" + temp[1].substring(temp[1].length() - 4, temp[1].length()); 31 } else { 32 result = telephone.substring(0, 3) + "****" + telephone.substring(telephone.length() - 4, telephone.length()); 33 } 34 } else { 35 result = "****" + telephone.substring(telephone.length() - 4, telephone.length()); 36 } 37 return result; 38 } 39 /** 40 * 身份證號顯示首6末4位,中間用4個*號隱藏代替,如:340121****3754 41 * 42 * @param idCard 43 * @return 44 */ 45 public static String maskIDCard(String idCard) { 46 if(StringUtils.isBlank(idCard)) { 47 return idCard; 48 } 49 50 return wordMask(idCard, 3, 4, "*"); 51 } 52 /** 53 * 銀行卡顯示首6末4位,中間用4個*號隱藏代替,如:622202****4123 54 * 55 * @param cardNo 56 * @return 57 */ 58 public static String maskBankCard(String cardNo) { 59 if(StringUtils.isBlank(cardNo) || cardNo.length() < 10) { 60 return cardNo; 61 } 62 return wordMask(cardNo, 6, 4, "*"); 63 } 64 /** 65 * 郵箱像是前兩位及最后一位字符,及@后郵箱域名信息,如:ch****y@163.com 66 * 67 * @param email 68 * @return 69 */ 70 public static String maskEmail(String email) { 71 if(StringUtils.isBlank(email)) { 72 return email; 73 } 74 String[] temp = email.split("@"); 75 76 return wordMask(temp[0], 2, 1, "*") + "@" + temp[1]; 77 } 78 /** 79 * 漢字掩碼 80 * 0-1字,如:用(用) 81 * 2字,如:用於(*於) 82 * 3-4字,如:用於掩(用*掩)、用於掩碼(用**碼) 83 * 5-6字,如:用於掩碼測(用於*碼測)、用於掩碼測試(用於**測試) 84 * 大於6字,如:用於掩碼測試的字符串(用於掩****字符串) 85 * 86 * @param name 87 * @return 88 */ 89 public static String maskName(String name) { 90 int lenth = StringUtils.length(name); 91 switch (lenth) { 92 case 0: 93 case 1: 94 return name; 95 case 2: 96 return "*" + name.substring(1, 2); 97 case 3: 98 case 4: 99 return wordMask(name, 1, 1, "*"); 100 case 5: 101 case 6: 102 return wordMask(name, 2, 2, "*"); 103 default: 104 return wordMask(name, 3, 3, "*"); 105 } 106 } 107 /** 108 * 全隱藏,如: *** 109 * 110 * @param str 111 * @return 112 */ 113 public static String maskAll(String str) { 114 if(StringUtils.isBlank(str)) { 115 return str; 116 } 117 return "******"; 118 } 119 /** 120 * 對字符串進行脫敏處理 -- 121 * 122 * @param word 被脫敏的字符 123 * @param startLength 被保留的開始長度 前余n位 124 * @param endLength 被保留的結束長度 后余n位 125 * @param pad 填充字符 126 * */ 127 public static String wordMask(String word,int startLength ,int endLength,String pad) { 128 if (startLength + endLength > word.length()) { 129 return StringUtils.leftPad("", word.length() - 1, pad); 130 } 131 String startStr = word.substring(0, startLength); 132 String endStr = word.substring(word.length() - endLength, word.length()); 133 return startStr + StringUtils.leftPad("", word.length() - startLength - endLength, pad) + endStr; 134 } 135 }