自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java用poi完成Excel導(dǎo)出數(shù)據(jù)脫敏

開源
脫敏的百度百科的定義:是指對(duì)某些敏感信息通過脫敏規(guī)則進(jìn)行數(shù)據(jù)的變形,實(shí)現(xiàn)敏感隱私數(shù)據(jù)的可靠保護(hù)。在涉及客戶安全數(shù)據(jù)或者一些商業(yè)性敏感數(shù)據(jù)的情況下,在不違反系統(tǒng)規(guī)則條件下,對(duì)真實(shí)數(shù)據(jù)進(jìn)行改造并提供測(cè)試使用,如身份證號(hào)、手機(jī)號(hào)、卡號(hào)、客戶號(hào)等個(gè)人信息都需要進(jìn)行數(shù)據(jù)脫敏。

 近日筆者更新了EasyPoi的4.3版本,主要功能是實(shí)現(xiàn)了數(shù)據(jù)脫敏,方便大家日常的脫敏需求。

脫敏的百度百科的定義:是指對(duì)某些敏感信息通過脫敏規(guī)則進(jìn)行數(shù)據(jù)的變形,實(shí)現(xiàn)敏感隱私數(shù)據(jù)的可靠保護(hù)。在涉及客戶安全數(shù)據(jù)或者一些商業(yè)性敏感數(shù)據(jù)的情況下,在不違反系統(tǒng)規(guī)則條件下,對(duì)真實(shí)數(shù)據(jù)進(jìn)行改造并提供測(cè)試使用,如身份證號(hào)、手機(jī)號(hào)、卡號(hào)、客戶號(hào)等個(gè)人信息都需要進(jìn)行數(shù)據(jù)脫敏。

這塊如果嚴(yán)格按照定義,實(shí)現(xiàn)身份證等數(shù)據(jù)的變形,筆者還沒實(shí)現(xiàn),因?yàn)檫@個(gè)設(shè)計(jì)脫敏規(guī)則對(duì)應(yīng),需要用戶指定規(guī)則,我們內(nèi)部的脫敏系統(tǒng)還是有些邏輯在的,這里就不細(xì)說了,這次主要實(shí)現(xiàn)的是加*號(hào)【后期再加自定義吧,畢竟常見的都是星號(hào)】,方便導(dǎo)出數(shù)據(jù)的時(shí)候隱藏非必須字段。


維護(hù)數(shù)據(jù)

主要功能

實(shí)現(xiàn)的功能主要分為三種:

  • 1. 隱去收尾,適合固定長(zhǎng)度,比如:手機(jī)號(hào),身份證
  • 2. 隱去部分,不固定長(zhǎng)度,比如:姓名,地址
  • 3. 隱去特定部分,特別表示保留,比如:郵箱

簡(jiǎn)單地實(shí)現(xiàn)的效果為

  • 13112345678 --> 131****1234
  • 張三 -->張*
  • 羅納爾迪尼奧 --> 羅納***奧
  • wuyun@163.com -> w****@16com.com

脫敏效果

實(shí)現(xiàn)方法

1.注解

注解還是通過@Excel 的屬性來實(shí)現(xiàn)的 ,在@Excel 這個(gè)地方加入了desensitizationRule屬性,可以在desensitizationRule屬性配置對(duì)應(yīng)的格式遍可以得到對(duì)應(yīng)的結(jié)果

  1. /** 
  2.   * 數(shù)據(jù)脫敏規(guī)則 
  3.   * 規(guī)則1: 采用保留頭和尾的方式,中間數(shù)據(jù)加星號(hào) 
  4.   * 如: 身份證  6_4 則保留 370101********1234 
  5.   *    手機(jī)號(hào)   3_4 則保留 131****1234 
  6.   * 規(guī)則2: 采用確定隱藏字段的進(jìn)行隱藏,優(yōu)先保留頭 
  7.   * 如: 姓名   1,3 表示最大隱藏3位,最小一位 
  8.   *          李 -->  * 
  9.   *          李三 --> 李* 
  10.   *          張全蛋  --> 張*蛋 
  11.   *          李張全蛋 --> 李**蛋 
  12.   *          尼古拉斯.李張全蛋 -> 尼古拉***張全蛋 
  13.   * 規(guī)則3: 特殊符號(hào)后保留 
  14.   * 如: 郵箱    1~@ 表示只保留第一位和@之后的字段 
  15.   *        afterturn@wupaas.com -> a********@wupaas.com 
  16.   * 復(fù)雜版本請(qǐng)使用接口 
  17.   * {@link cn.afterturn.easypoi.handler.inter.IExcelDataHandler} 
  18.   */ 
  19.  public String   desensitizationRule() default ""

 可以根據(jù)注釋看到,3個(gè)簡(jiǎn)單規(guī)則的使用方法,還是比較清晰的,主要是:

  • 用下劃線分隔的保留頭尾的第一種格式
  • 該格式首和尾都可以是0,表示不保留
  • 用逗號(hào)分隔的隱藏部分?jǐn)?shù)據(jù)的第二種格式
  • 保留規(guī)則以對(duì)稱為主
  • 用~號(hào)來區(qū)分的,保留特殊字符后的格式的
  • 如果存在多個(gè)特殊字符,只保留最后一位的數(shù)據(jù)

具體實(shí)現(xiàn)代碼如下:

  1. /** 
  2.      * 特定字符分隔,添加星號(hào) 
  3.      * @param start 
  4.      * @param mark 
  5.      * @param value 
  6.      * @return 
  7.      */ 
  8.     private static String markSpilt(int start, String mark, String value) { 
  9.         if (value == null) { 
  10.             return null
  11.         } 
  12.         int end = value.lastIndexOf(mark); 
  13.         if (end <= start) { 
  14.             return value; 
  15.         } 
  16.         return StringUtils.left(value, start).concat(StringUtils.leftPad(StringUtils.right(value, value.length() - end), value.length() - start, "*")); 
  17.     } 
  18.  
  19.     /** 
  20.      * 部分?jǐn)?shù)據(jù)截取,優(yōu)先對(duì)稱截取 
  21.      * @param start 
  22.      * @param end 
  23.      * @param value 
  24.      * @return 
  25.      */ 
  26.     private static String subMaxString(int start, int end, String value) { 
  27.         if (value == null) { 
  28.             return null
  29.         } 
  30.         if (start > end) { 
  31.             throw new IllegalArgumentException("start must less end"); 
  32.         } 
  33.         int len = value.length(); 
  34.         if (len <= start) { 
  35.             return StringUtils.leftPad("", len, "*"); 
  36.         } else if (len > start && len <= end) { 
  37.             if (len == 1) { 
  38.                 return value; 
  39.             } 
  40.             if (len == 2) { 
  41.                 return StringUtils.left(value, 1).concat("*"); 
  42.             } 
  43.             return StringUtils.left(value, 1).concat(StringUtils.leftPad(StringUtils.right(value, 1), StringUtils.length(value) - 1, "*")); 
  44.         } else { 
  45.             start = (int) Math.ceil((len - end + 0.0D) / 2); 
  46.             end = len - start - end
  47.             end = end == 0 ? 1 : end
  48.             return StringUtils.left(value, start).concat(StringUtils.leftPad(StringUtils.right(value, end), len - start, "*")); 
  49.         } 
  50.     } 
  51.  
  52.     /** 
  53.      * 收尾截取數(shù)據(jù) 
  54.      * @param start 
  55.      * @param end 
  56.      * @param value 
  57.      * @return 
  58.      */ 
  59.     private static String subStartEndString(int start, int end, String value) { 
  60.         if (value == null) { 
  61.             return null
  62.         } 
  63.         if (value.length() <= start + end) { 
  64.             return value; 
  65.         } 
  66.         return StringUtils.left(value, start).concat(StringUtils.leftPad(StringUtils.right(value, end), StringUtils.length(value) - start, "*")); 
  67.     } 

 注解demo

使用起來也比較簡(jiǎn)單,通過簡(jiǎn)單的注解配置,就可以實(shí)現(xiàn)

  1.    @Excel(name = "姓名", desensitizationRule = "1,6"
  2.     private String name
  3.     @Excel(name = "身份證", desensitizationRule = "6_4"
  4.     private String card; 
  5.     @Excel(name = "手機(jī)號(hào)", desensitizationRule = "3_4"
  6.     private String phone; 
  7.     @Excel(name = "郵箱", desensitizationRule = "3~@"
  8.     private String email; 
  9.  
  10. // ---------------下面是生成excel的代碼------- 
  11. // 都是測(cè)試數(shù)據(jù),就簡(jiǎn)單生成了部分 
  12.  List<DesensitizationEntity> list = new ArrayList<>(); 
  13.         for (int i = 0; i < 20; i++) { 
  14.             DesensitizationEntity entity = new DesensitizationEntity(); 
  15.             entity.setCard("37010119900101123" + i % 10); 
  16.             entity.setName("張三"); 
  17.             entity.setPhone("1311234567" + i % 10); 
  18.             entity.setEmail(i % 10 + "ttttt@afterturn.com"); 
  19.             list.add(entity); 
  20.         } 
  21.         Date         start    = new Date(); 
  22.         ExportParams params   = new ExportParams("脫敏測(cè)試""脫敏測(cè)試", ExcelType.XSSF); 
  23.         Workbook     workbook = ExcelExportUtil.exportExcel(params, DesensitizationEntity.class, list); 
  24.         System.out.println(new Date().getTime() - start.getTime()); 
  25.         File savefile = new File("D:/home/excel/"); 
  26.         if (!savefile.exists()) { 
  27.             savefile.mkdirs(); 
  28.         } 
  29.         FileOutputStream fos = new FileOutputStream("D:/home/excel/ExcelDesensitizationTest.xlsx"); 
  30.         workbook.write(fos); 
  31.         fos.close(); 

 生成的效果如下,可以看到幾個(gè)場(chǎng)景都有所覆蓋,基本上常用的字段也就

PS: 因?yàn)樽兎N注解和注解可以通用,所以大家在使用變種注解的時(shí)候也可以使用這種方式來脫敏數(shù)據(jù)。

這里就不舉例子了,只需要new ExcelExportEntity的之后調(diào)用setDesensitizationRule(rule),把對(duì)應(yīng)的規(guī)則設(shè)置就好,規(guī)則和上面保持一致。

2. 模板

模板脫敏

目前從群里反應(yīng)和問題數(shù)量看,模板已經(jīng)是最常用的方式了,雖然我一致推薦注解,但明顯模板比注解簡(jiǎn)單的,對(duì)代碼的侵入一些,我自己寫的特殊標(biāo)簽,也日益豐富了。

脫敏的標(biāo)簽取自:desensitizationRule 的兩個(gè)單詞的頭兩個(gè)字符 deru,用來表示這個(gè)數(shù)據(jù)需要脫敏。使用方法也比較簡(jiǎn)單deru:1_3,即deru:后面跟具體的規(guī)則,規(guī)則使用方式和上面介紹的 保持一致,這里就不重復(fù)贅述了,下面給大家看下例子:

  1. /** 
  2. *  這個(gè)包含兩個(gè)規(guī)則 
  3. * 1. deru: 代表這個(gè)數(shù)據(jù)需要脫敏 
  4. * 2. dict: 代表這個(gè)需要調(diào)用字典接口轉(zhuǎn)移 
  5. **/ 
  6. deru:1_1;dict:mtype;t.supMaterialList.mtype 

 脫敏在標(biāo)簽處理級(jí)別中屬于最低級(jí),所有標(biāo)簽處理后,才會(huì)處理這個(gè)標(biāo)簽,和他的書寫順序無關(guān),比如上面的例子他寫著第一個(gè),也是先處理了字典之后才處理標(biāo)簽。

核心代碼如下:

  1. /** 
  2.      * 根據(jù)模板解析函數(shù)獲取值 
  3.      * @param funStr 
  4.      * @param map 
  5.      * @return 
  6.      */ 
  7.     private Object getValByHandler(String funStr,Map<String, Object> map, Cell cell) throws Exception { 
  8.         // step 2. 判斷是否含有解析函數(shù) 
  9.         if (isHasSymbol(funStr, NUMBER_SYMBOL)) { 
  10.             funStr = funStr.replaceFirst(NUMBER_SYMBOL, ""); 
  11.         } 
  12.         boolean isStyleBySelf = false
  13.         if (isHasSymbol(funStr, STYLE_SELF)) { 
  14.             isStyleBySelf = true
  15.             funStr = funStr.replaceFirst(STYLE_SELF, ""); 
  16.         } 
  17.         boolean isDict = false
  18.         String  dict   = null
  19.         if (isHasSymbol(funStr, DICT_HANDLER)) { 
  20.             isDict = true
  21.             dict = funStr.substring(funStr.indexOf(DICT_HANDLER) + 5).split(";")[0]; 
  22.             funStr = funStr.replaceFirst(DICT_HANDLER, ""); 
  23.             funStr = funStr.replaceFirst(dict + ";"""); 
  24.         } 
  25.         boolean isI18n = false
  26.         if (isHasSymbol(funStr, I18N_HANDLER)) { 
  27.             isI18n = true
  28.             funStr = funStr.replaceFirst(I18N_HANDLER, ""); 
  29.         } 
  30.        //這里判斷是否包含注解規(guī)則,如果有注解規(guī)則就把注解規(guī)則挑出來,然后刪除掉規(guī)則 
  31.         boolean isDern = false
  32.         String  dern   = null
  33.         if (isHasSymbol(funStr, DESENSITIZATION_RULE)) { 
  34.             isDern = true
  35.             dern = funStr.substring(funStr.indexOf(DESENSITIZATION_RULE) + 5).split(";")[0]; 
  36.             funStr = funStr.replaceFirst(DESENSITIZATION_RULE, ""); 
  37.             funStr = funStr.replaceFirst(dern + ";"""); 
  38.         } 
  39.         if (isHasSymbol(funStr, MERGE)) { 
  40.             String mergeStr = PoiPublicUtil.getElStr(funStr,MERGE); 
  41.             funStr = funStr.replace(mergeStr, ""); 
  42.             mergeStr = mergeStr.replaceFirst(MERGE, ""); 
  43.             try { 
  44.                 int colSpan = (int)Double.parseDouble(PoiPublicUtil.getRealValue(mergeStr, map).toString()); 
  45.                 PoiMergeCellUtil.addMergedRegion(cell.getSheet(), cell.getRowIndex(), 
  46.                         cell.getRowIndex() , cell.getColumnIndex(), cell.getColumnIndex() + colSpan - 1); 
  47.             } catch (Exception e) { 
  48.                 LOGGER.error(e.getMessage(),e); 
  49.             } 
  50.         } 
  51.         Object obj = funStr.indexOf(START_STR) == -1 ? eval(funStr, map) : PoiPublicUtil.getRealValue(funStr, map); 
  52.         if (isDict) { 
  53.             obj = dictHandler.toName(dict, null, funStr, obj); 
  54.         } 
  55.         if (isI18n) { 
  56.             obj = i18nHandler.getLocaleName(obj.toString()); 
  57.         } 
  58.       // 這里就是調(diào)用脫敏規(guī)則,和注解是一個(gè)工具類,所以規(guī)則一致 
  59.         if (isDern) { 
  60.             obj = PoiDataDesensitizationUtil.desensitization(dern,obj); 
  61.         } 
  62.         return obj; 
  63.     } 

 給大家看下模板跑出來的效果:

最后結(jié)果

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2021-11-04 10:45:46

SpringBootExcelJava

2012-02-22 09:44:21

Java

2023-02-25 10:04:21

JavaExcel導(dǎo)出功能

2023-02-03 08:21:30

excelMySQL

2025-02-14 09:30:42

2022-12-28 08:17:36

數(shù)據(jù)庫數(shù)據(jù)導(dǎo)出

2010-06-22 14:58:50

JDOMJavaXML

2010-07-21 14:17:36

SQL Server數(shù)

2018-01-26 07:53:46

數(shù)據(jù)脫敏數(shù)據(jù)安全信息安全

2021-03-26 07:09:15

Java技術(shù)pdfExcel

2011-04-14 09:05:07

ExcelMySQL數(shù)據(jù)

2020-12-02 11:56:16

Java注解Excel

2017-02-05 17:27:43

2025-02-12 07:27:52

MySQL8分頁查詢excel

2025-03-10 00:13:00

數(shù)據(jù)庫脫敏日志脫敏出脫敏

2022-05-16 08:50:23

數(shù)據(jù)脫加密器

2021-10-22 06:53:45

脫敏處理數(shù)據(jù)

2022-12-29 08:49:40

SpringBootExcel

2010-05-17 16:25:05

MySQL數(shù)據(jù)

2022-02-09 18:28:46

多線程Excel代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)