一、简述
数据脱敏是指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护。
简单来说就是你有些数据并不想让别人看见,需要进行处理再显示在页面上。如:(“筛选****地区”)
二、放代码(脱敏方法工具类:调用即可)
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 }