数据脱敏处理

什么是脱敏

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

项目中如何脱敏数据

一般我们得到的数据基本上是从数据库中获得的,例如存在数据库中的手机号,不能泄露信息,就需要进行脱敏,那我们查出来的时候就要调用脱敏数据的方法,从而达到我们的目的。

public class DataConcealUtil {
    private static final int SIZE = 6;
    private static final String SYMBOL = "*";

    public static String toConceal(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();
    }  

转载至:(https://blog.csdn.net/f1576813783/article/details/77253233)
在数据库中建表,意图是存放哪些需要脱敏的页面以及相关脱敏的字段

建表图

其中每个页面对应一个view_code(这个说法不准确,后面纠正),上面的name 和code就是对应查询封装的时候的字段名称(如果是Object对象,就保持和原数据库中表字段一致)
之后就是看sql查询得到需要脱敏的数据。

Map modelMap = new HashMap();
        List list =this.queryByPage(sql, new RowMapper(){
            @Override
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                Map row = new HashMap();  
                 ResultSetMetaData rowdata =rs.getMetaData();
                 for(int i = 1 ; i<= rowdata.getColumnCount() ; i++){
                     String columnName = rowdata.getColumnLabel(i); 
                     row.put(columnName, rs.getObject(i)==null?"":rs.getObject(i));
                 }
                 if(caltype==TimeLenUtil.TIME_LEN_WORK){
                     long timelen = TimeLenUtil.getWorkTimeLen( String.valueOf(row.get("ACCEPT_TIME")), String.valueOf(row.get("END_TIME")), Long.parseLong(String.valueOf(row.get("PROVINCE"))));//换算工单历时
                     row.put("TIMELONGS",timelen);//工单历时
                     long timelimits = Long.parseLong(String.valueOf(row.get("TIME_LIMIT")))*60;//工单时限秒值
                     row.put("CHAOSHIFLAG",timelen>timelimits?1:0);//是否超时
                     row.put("YUJINGFLAG",(timelentimelimits-7200)?1:0);//是否预警
                 }
                 //row.put("TIMELONGS",TimeLenUtil.getTimeLen( String.valueOf(row.get("ACCEPT_TIME"), String.valueOf(row.get("END_TIME"), Long.parseLong(String.valueOf(row.get("PROVINCE"))));//换算工单历时
                return row;
            }
        },pageObj,paralist.toArray());
        Listlist2=new ArrayList();
        list2=DataConcealService.getDataConcealForList(list, "ASSIGNXIU");
        modelMap.put("pageObj", pageObj);
        modelMap.put("list", list2);
        modelMap.put("queryCond", queryCond);
        return modelMap;

上面代码可以看出,查出的数据封装在list集合里 ,然后调用getDataConcealForList方法。由于我们每次查询得到的数据不一定是个list集合,也有可能是个类对象,那么,针对这样的可能性,在DataConcealService.java里面封装了两种方法:

public class DataConcealService implements InitializingBean{
@Autowired
    DataConcealDao dataConcealDao;
    public static String WFM_DATA_SWITCH = ConfigTypeUtil.getValue("WFM_DATA_SWITCH");//配置文件中读取到脱敏开关
    static Map> dataConcealMap=new HashMap>();//存放脱敏的字段
    /**
     * 初始化脱敏字段的配置
     */
    @Override
    public  void afterPropertiesSet() throws Exception {
        if("ON".equals(WFM_DATA_SWITCH)){
            List dataConcealist=dataConcealDao.getDataConceal();
            for(DataConceal dc:dataConcealist){
                List dcList=dataConcealMap.get(dc.getViewCode());
                if(dcList==null){
                    dcList=new ArrayList();
                }
                dcList.add(dc.getCode());
                dataConcealMap.put(dc.getViewCode(), dcList);
            }
        }
        
    }
/**
     * 根据数据库字段的配置,将横表的内容进行脱敏
     * @param obj 将实体类的内容进行脱敏
     * @param viewCode
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List getDataConcealForList(List  objList,String viewCode) {
        List  list =new ArrayList ();
        if("ON".equals(WFM_DATA_SWITCH)){
            try {
                List dataConcealist=dataConcealMap.get(viewCode);
                if(dataConcealist!=null && dataConcealist.size()>0){
                 for(int i = 0;i T getDataConceal(Object obj,String viewCode) {
        if("ON".equals(WFM_DATA_SWITCH)){
            try {
                List dataConcealist=dataConcealMap.get(viewCode);
                System.out.println(dataConcealMap.get(viewCode));
                if(dataConcealist!=null && dataConcealist.size()>0){
                    String json=JacksonUtil.obj2json(obj);
                    System.out.println(json);
                    JSONObject jsonObj=JSONObject.fromObject(json);
                    System.out.println(jsonObj);
                    for(String dataCode:dataConcealist){
                        System.out.println(jsonObj.getString(dataCode));
                        if(CommonUtil.hasValue(jsonObj.getString(dataCode)) && !jsonObj.getString(dataCode).equals("null")){
                            String val=DataConcealUtil.toConceal(jsonObj.getString(dataCode));
                            jsonObj.element(dataCode, val);
                        }
                    }
                //  System.out.println(JacksonUtil.json2pojo(jsonObj.toString(),obj.getClass()));
                    return (T) JacksonUtil.json2pojo(jsonObj.toString(),obj.getClass());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return (T) obj;
        }else{
            return (T) obj;
        }
    }
}

执行流程就是这样啦,前面提到一个不正确的理论是一个页面对应一个view_code,不准确的原因是因为一个页面可能执行多条查询sql 语句。这样的话,共用一个view_code就会导致其他的sql不能正确执行,因此我的理解是一条查询sql对应一个view_code,页面编码不一样,view_name是可以一样的,所以就能保证各个数据进行脱敏互不干扰。效果如下图:

效果图

你可能感兴趣的:(数据脱敏处理)