一、簡述
數據脫敏是指對某些敏感信息通過脫敏規則進行數據的變形,實現敏感隱私數據的可靠保護。簡單來說就是你有些數據並不想讓別人看見,需要進行處理再顯示在頁面上。
舉個最簡單的例子,比如我們在點外賣的時候,外賣單子上會有我們的電話號碼,平台為了保證我們的信息不被泄露,就使用信息脫敏來將部分信息進行隱藏來達到保護我們信息的目的。
二、如何使用
1、相關代碼
package com.zhouhong.utils; import sun.applet.Main; public class DesensitizationUtil { private static final int SIZE = 6; private static final String SYMBOL = "*"; public static void main(String[] args) { String address = commonDisplay("陝西省西安市雁塔區xx102號"); System.out.println(address); } /** * 通用脫敏方法 * @param value * @return */ public static String commonDisplay(String value) { if (null == value || "".equals(value)) { return value; } int len = value.length(); int pamaone = len / 2; int pamatwo = pamaone - 1; int pamathree = len % 2; StringBuilder stringBuilder = new StringBuilder(); if (len <= 2) { if (pamathree == 1) { return SYMBOL; } stringBuilder.append(SYMBOL); stringBuilder.append(value.charAt(len - 1)); } else { if (pamatwo <= 0) { stringBuilder.append(value.substring(0, 1)); stringBuilder.append(SYMBOL); stringBuilder.append(value.substring(len - 1, len)); } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) { int pamafive = (len - SIZE) / 2; stringBuilder.append(value.substring(0, pamafive)); for (int i = 0; i < SIZE; i++) { stringBuilder.append(SYMBOL); } if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) { stringBuilder.append(value.substring(len - pamafive, len)); } else { stringBuilder.append(value.substring(len - (pamafive + 1), len)); } } else { int pamafour = len - 2; stringBuilder.append(value.substring(0, 1)); for (int i = 0; i < pamafour; i++) { stringBuilder.append(SYMBOL); } stringBuilder.append(value.substring(len - 1, len)); } } return stringBuilder.toString(); } }
2、測試
3、項目中如何使用
在項目中只需要引入上面的類,然后在業務層需要脫敏的地方調用此方法即可。比如如下方法對評論的人姓名信息進行一定的脫敏處理
@Transactional(propagation = Propagation.SUPPORTS) @Override public PagedGridResult queryPagedComments(String itemId, Integer level, Integer page, Integer pageSize) { Map<String, Object> map = new HashMap<>(); map.put("itemId", itemId); map.put("level", level); //mybatis-pagehelper /** * page: 第幾頁 * pageSize: 每頁顯示條數 */ PageHelper.startPage(page, 10); List<ItemCommentVO> list = itemsMapperCustom.queryItemComments(map); for (ItemCommentVO vo : list ) { vo.setNickName(DesensitizationUtil.commonDisplay(vo.getNickName())); } return setterPagedGrid(list, page); } private PagedGridResult setterPagedGrid(List<?> list ,Integer page){ PageInfo<?> pageList = new PageInfo<>(list); PagedGridResult grid = new PagedGridResult(); grid.setPage(page); grid.setRows(list); grid.setTotal(pageList.getPages()); grid.setRecords(pageList.getTotal()); return grid; }
三、更新------------------------------其他脫敏實現
上面只是對於大部分通用的信息進行脫敏,但是針對於不同的需求如何實現呢,以下是從網上搜集的關於不同信息脫敏的實現
import org.apache.commons.lang3.StringUtils; public class MaskUtil { /** * 手機號顯示首3末4位,中間用*號隱藏代替,如:188****5593 * * @param mobile * @return */ public static String maskMobile(String mobile) { if(StringUtils.isBlank(mobile) || mobile.length() <= 8) { return mobile; } return wordMask(mobile, 3, 4, "*"); } /** * 電話號碼顯示區號及末4位,中間用*號隱藏代替,如:055****6666 * * @param telephone * @return */ public static String maskTelephone(String telephone) { if(StringUtils.isBlank(telephone)) { return telephone; } String result; if (telephone.length() > 8) { if (telephone.contains("-")) { String[] temp = telephone.split("-"); result = temp[0] + "****" + temp[1].substring(temp[1].length() - 4, temp[1].length()); } else { result = telephone.substring(0, 3) + "****" + telephone.substring(telephone.length() - 4, telephone.length()); } } else { result = "****" + telephone.substring(telephone.length() - 4, telephone.length()); } return result; } /** * 身份證號顯示首6末4位,中間用4個*號隱藏代替,如:340121****3754 * * @param idCard * @return */ public static String maskIDCard(String idCard) { if(StringUtils.isBlank(idCard)) { return idCard; } return wordMask(idCard, 3, 4, "*"); } /** * 銀行卡顯示首6末4位,中間用4個*號隱藏代替,如:622202****4123 * * @param cardNo * @return */ public static String maskBankCard(String cardNo) { if(StringUtils.isBlank(cardNo) || cardNo.length() < 10) { return cardNo; } return wordMask(cardNo, 6, 4, "*"); } /** * 郵箱像是前兩位及最后一位字符,及@后郵箱域名信息,如:ch****y@163.com * * @param email * @return */ public static String maskEmail(String email) { if(StringUtils.isBlank(email)) { return email; } String[] temp = email.split("@"); return wordMask(temp[0], 2, 1, "*") + "@" + temp[1]; } /** * 漢字掩碼 * 0-1字,如:用(用) * 2字,如:用於(*於) * 3-4字,如:用於掩(用*掩)、用於掩碼(用**碼) * 5-6字,如:用於掩碼測(用於*碼測)、用於掩碼測試(用於**測試) * 大於6字,如:用於掩碼測試的字符串(用於掩****字符串) * * @param name * @return */ public static String maskName(String name) { int lenth = StringUtils.length(name); switch (lenth) { case 0: case 1: return name; case 2: return "*" + name.substring(1, 2); case 3: case 4: return wordMask(name, 1, 1, "*"); case 5: case 6: return wordMask(name, 2, 2, "*"); default: return wordMask(name, 3, 3, "*"); } } /** * 全隱藏,如: *** * * @param str * @return */ public static String maskAll(String str) { if(StringUtils.isBlank(str)) { return str; } return "******"; } /** * 對字符串進行脫敏處理 -- * * @param word 被脫敏的字符 * @param startLength 被保留的開始長度 前余n位 * @param endLength 被保留的結束長度 后余n位 * @param pad 填充字符 * */ public static String wordMask(String word,int startLength ,int endLength,String pad) { if (startLength + endLength > word.length()) { return StringUtils.leftPad("", word.length() - 1, pad); } String startStr = word.substring(0, startLength); String endStr = word.substring(word.length() - endLength, word.length()); return startStr + StringUtils.leftPad("", word.length() - startLength - endLength, pad) + endStr; } }