敏感信息脱敏实现

一、简述

  数据脱敏是指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护。简单来说就是你有些数据并不想让别人看见,需要进行处理再显示在页面上。

  举个最简单的例子,比如我们在点外卖的时候,外卖单子上会有我们的电话号码,平台为了保证我们的信息不被泄露,就使用信息脱敏来将部分信息进行隐藏来达到保护我们信息的目的。

二、如何使用

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;
    }
}

 

 

 

  

 

posted @ 2020-12-21 01:26  Tom-shushu  阅读(2087)  评论(0编辑  收藏  举报