什么是脱敏
数据脱敏是指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护。简单来说就是你有些数据并不想让别人看见,需要进行处理再显示在页面上。
项目中如何脱敏数据
一般我们得到的数据基本上是从数据库中获得的,例如存在数据库中的手机号,不能泄露信息,就需要进行脱敏,那我们查出来的时候就要调用脱敏数据的方法,从而达到我们的目的。
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
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是可以一样的,所以就能保证各个数据进行脱敏互不干扰。效果如下图:
效果图