日志掩码工具
作者:互联网
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Maps;
/**
* 掩码工具类
* @author Helen
*
*/
public class MaskUtil {
private static final Logger logger = LoggerFactory.getLogger(MaskUtil.class);
public static final String ENCRYPT_DEF = "***"; //默认全掩码
/**
* 敏感信息掩码
*
* @param jsonString JsonString
* @param maskKeys 自定义需要进行掩码的Key
* @return
*/
public static Object maskJsonString(String jsonString, String... maskKeys) {
try {
boolean isJsonObject = isJsonObject(jsonString);
boolean isJsonArray = isJsonArray(jsonString);
if (!isJsonObject && !isJsonArray) {
return StringUtils.EMPTY;
}
if (isJsonObject) {
return jsonObjectHelper(jsonString, maskKeys);
}
return jsonArrayHelper(jsonString, maskKeys);
} catch (Exception e) {
return StringUtils.EMPTY;
}
}
/**
* String类型的JSONObject
* @param jsonString
* @param maskKeys
* @return
*/
private static Object jsonObjectHelper(String jsonString, String... maskKeys) {
JSONObject originalJsonObject = JSONObject.parseObject(jsonString);
if (null != originalJsonObject) {
Set
for (String key : keySets) {
for (String maskKey : maskKeys) {
if (key.equalsIgnoreCase(maskKey)) {
originalJsonObject.put(key, maskByKey(key, originalJsonObject.get(key)));
}
}
try {
JSONArray jsonArray = originalJsonObject.getJSONArray(key);
originalJsonObject.put(key, maskJsonString(JSONArray.toJSONString(jsonArray), maskKeys));
} catch (Exception e) {
try {
JSONObject jsonObject = originalJsonObject.getJSONObject(key);
originalJsonObject.put(key, maskJsonString(JSONObject.toJSONString(jsonObject), maskKeys));
} catch (Exception e1) {
for (String maskKey : maskKeys) {
if (key.equalsIgnoreCase(maskKey)) {
originalJsonObject.put(key, maskByKey(key, originalJsonObject.get(key)));
}
}
}
}
}
}
return originalJsonObject;
}
/**
* String类型的JSONArray
* @param jsonString
* @param maskKeys
* @return
*/
private static String jsonArrayHelper(String jsonString, String... maskKeys) {
StringBuilder stringBuilder = new StringBuilder();
JSONArray originalJsonArray = JSONArray.parseArray(jsonString);
if (null != originalJsonArray && originalJsonArray.size() > 0) {
for (Object object : originalJsonArray) {
stringBuilder.append(maskJsonString(JSONArray.toJSONString(object), maskKeys));
}
return stringBuilder.toString();
} else {
return StringUtils.EMPTY;
}
}
/**
* 判断是否是JSONObject
* @param jsonString
* @return
*/
private static boolean isJsonObject(String jsonString) {
try {
JSONObject.parseObject(jsonString);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 判断是否是JSONArray
* @param jsonString
* @return
*/
private static boolean isJsonArray(String jsonString) {
try {
JSONArray.parseArray(jsonString);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 字段掩码方法
* @param key :需要掩码的key
* @param value :未掩码的值
* @return :掩码后的值
*/
private static Object maskByKey(String key, Object value) {
if(value == null) {
return ENCRYPT_DEF;
}
String strValue = value.toString();
if(StringUtils.equalsIgnoreCase("email", key)) {//邮箱
return maskEmail(strValue);
}else if(StringUtils.equalsIgnoreCase("mobile", key)) {//手机号
return maskMobile(strValue);
}else {
return ENCRYPT_DEF;
}
}
/**
* 掩码手机号
* @param mobile
* @return
*/
public static String maskMobile(String mobile) {
if (StringUtils.isNotBlank(mobile)) {
mobile = mobile.replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2");
}
return mobile;
}
/**
* 掩码邮箱
* @param email
* @return
*/
public static String maskEmail(String email) {
if (StringUtils.isNotBlank(email)) {
return ENCRYPT_DEF;
}
return email;
}
/**
* List
* @param rows:列表结果集
* @param description:日志错误描述
* @param T:类型
* @param key:自定义需要进行掩码的Key
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static
try {
Object jsonStr = null;
if(rows != null) {
//String json = JSONObject.toJSONString(rows);
String json = JSONObject.toJSONStringWithDateFormat(rows, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat);
if(StringUtils.isNotBlank(json)) {
jsonStr = maskJsonString(json,key);
if(jsonStr != null) {
String arrayStr = null;
if(!"[".equals(jsonStr.toString().substring(1))) {
arrayStr = new StringBuilder().append("[").append(jsonStr).append("]").toString();
}else {
arrayStr = jsonStr.toString();
}
rows = JSONObject.parseArray(arrayStr, T);
}
}
}
return rows;
}catch (Exception e) {
logger.warn("{}数据掩码出错, rows:{}; error:{}", description, rows, e);
return rows;
}
}
/**
* JSONArray掩码
* @param jsonArray: array结果集
* @param description:描述
* @param key:掩码字段
* @return
*/
public static JSONArray encryptJsonArray(JSONArray jsonArray, String description, String...key) {
try {
Object jsonStr = null;
if(jsonArray != null && jsonArray.size() > 0) {
//String json = JSONObject.toJSONString(jsonArray);
String json = JSONObject.toJSONStringWithDateFormat(jsonArray, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat);
if(StringUtils.isNotBlank(json)) {
jsonStr = maskJsonString(json,key);
if(jsonStr != null) {
String arrayStr = null;
if(!"[".equals(jsonStr.toString().substring(1))) {
arrayStr = new StringBuilder().append("[").append(jsonStr).append("]").toString();
}else {
arrayStr = jsonStr.toString();
}
jsonArray = JSONArray.parseArray(arrayStr);
}
}
}
return jsonArray;
}catch (Exception e) {
logger.warn("{}数据掩码出错, rows:{}; error:{}",description, jsonArray, e);
return jsonArray;
}
}
public static void main(String[] args) {
JSONObject json = new JSONObject();
json.put("email", "123@qq.com");
json.put("mobile", "13917180713");
BaseInfo baseInfo = new BaseInfo();
baseInfo.setAddress("上海市浦东新区");
baseInfo.setFirstName("哈哈");
json.put("baseInfo", baseInfo);
//============
JSONObject json2 = new JSONObject();
json2.put("email", "2123@qq.com");
json2.put("mobile", "15817184828");
BaseInfo baseInfo2 = new BaseInfo();
baseInfo2.setAddress("2上海市浦东新区");
baseInfo2.setFirstName("2哈哈");
json2.put("baseInfo", baseInfo2);
JSONArray array = new JSONArray();
array.add(json);
array.add(json2);
System.out.println("掩码前的array:"+array);
System.out.println("掩码后的array:"+encryptJsonArray(array,"测试数据","email", "firstName","mobile"));//掩码字段:email,firstName,mobile
List<Map
Map
map.put("email", "123@qq.com");
map.put("mobile", "15817184828");
map.put("number", "12345678945612345678");
Map
mapTo.put("email", "haha@qq.com");
mapTo.put("mobile", "13917180713");
mapTo.put("number", "789456123741852963");
list.add(map);
list.add(mapTo);
System.out.println("掩码前的list:"+list);
System.out.println("掩码后的list:"+encryptEntity(list, "测试数据-jsonObject", Map.class, "mobile"));//掩码字段:mobile
}
}
class BaseInfo{
private String address;
private String firstName;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
标签:jsonString,return,String,param,key,掩码,日志,工具 来源: https://blog.51cto.com/u_7218743/2785555