1. 程式人生 > >對樹狀結果進行脫敏

對樹狀結果進行脫敏

package com.paic.icore.agr.common.utils;

import com.paic.icore.agr.common.exception.ServiceException;
import com.paic.icore.agr.common.exception.ServiceRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 物件脫敏以及取消脫敏
 * @author wangxiaoshan
 */
public class VagueUtils {
  private static Logger logger = LoggerFactory.getLogger(VagueUtils.class);

  private static final String REPLACEMSG="****";
  /**
   * 對T中的list包含的屬性進行脫敏處理
   * @param t 模糊處理的物件
   * @param treeDto 模糊處理的屬性
   */
    public static void VagueData(Object t, TreeDto treeDto)  {
      try{
      for (TreeDto treeDtos : treeDto.nextNotes()){
        Field field = ReflectionUtils.findField(t.getClass(), treeDtos.getPath());
        Class type = field.getType();
        ReflectionUtils.makeAccessible(field);
        Object obj = field.get(t);
        if(treeDtos.hasNext()){
          if(type.equals(List.class)){
            List list= (List)obj;
            for (Object object:list) {
              VagueData(object,treeDtos);
            }
          }else{
            VagueData(obj,treeDtos);
          }
        }else{
          if(type.equals(String.class)){
            String fildValue = (String) obj;
            if(!StringUtils.isEmpty(fildValue)){
              int num = 0;
              if(fildValue.length()>6){
                num = 6;
              }else {
                num = (int) Math.floor(fildValue.length()/2);
              }
              fildValue = fildValue.substring(0,fildValue.length() - num)+REPLACEMSG;
            }
            field.set(t,fildValue);
          }else{
            logger.info("暫未處理資料型別"+type+",請處理!");
          }
        }

      }
      }catch (Exception e){
        //敏感資訊出錯不做異常上拋(不影響出單),只記錄錯誤資訊
        logger.info(e.getMessage());
      }
    }

  /**
   * 判斷T2中的list包含的屬性如果進行了脫敏處理就還原為t1中對應的屬性值
   * @param t1 原值
   * @param t2 新值
   * @param treeDto 處理後的結果
   * @return
   */
    public static void UnVagueData(Object t1,Object t2,TreeDto treeDto) throws IllegalAccessException, ServiceException{
      for (TreeDto treeDtos : treeDto.nextNotes()){
        Field field1 = ReflectionUtils.findField(t1.getClass(), treeDtos.getPath());
        Field field2 = ReflectionUtils.findField(t1.getClass(), treeDtos.getPath());
        Class type = field1.getType();
        ReflectionUtils.makeAccessible(field1);
        ReflectionUtils.makeAccessible(field2);
        Object obj1 = field1.get(t1);
        Object obj2 = field1.get(t2);
        if(treeDtos.hasNext()){
          if(type.equals(List.class)){
            List list1= (List)obj1;
            List list2= (List)obj2;
            for (int i = 0;i<list1.size();i++) {
              UnVagueData(list1.get(i),list2.get(i),treeDtos);
            }
          }else{
            UnVagueData(obj1,obj2,treeDtos);
          }
        }else{
          if(type.equals(String.class)){
            //如果新值中包含****就將其還原來的值
            String fildValue2 = (String) obj2;
            if(fildValue2.indexOf(REPLACEMSG)>0){
              field2.set(t1,obj1);
            }
          }else{
            throw new ServiceException("暫未處理資料型別"+type+",請處理!");
          }
        }

      }
    };
}