use of org.sagacity.sqltoy.config.model.SecureMask in project sagacity-sqltoy by chenrenfei.
the class ResultUtils method secureMask.
/**
* 对字段进行安全脱敏
*
* @param result
* @param sqlToyConfig
* @param labelIndexMap
*/
private static void secureMask(QueryResult result, SqlToyConfig sqlToyConfig, HashMap<String, Integer> labelIndexMap) {
List<List> rows = result.getRows();
SecureMask[] masks = sqlToyConfig.getSecureMasks();
Integer index;
Object value;
for (SecureMask mask : masks) {
index = labelIndexMap.get(mask.getColumn().toLowerCase());
if (index != null) {
int column = index.intValue();
for (List row : rows) {
value = row.get(column);
if (value != null)
row.set(column, maskStr(mask, value));
}
}
}
}
use of org.sagacity.sqltoy.config.model.SecureMask in project sagacity-sqltoy by chenrenfei.
the class QueryExecutor method secureMask.
/**
* @TODO 对结果字段进行安全脱敏
* @param maskType
* @param params
* @return
*/
public QueryExecutor secureMask(MaskType maskType, String... columns) {
if (maskType != null && columns != null && columns.length > 0) {
for (String column : columns) {
SecureMask mask = new SecureMask();
mask.setColumn(column);
mask.setType(maskType.getValue());
innerModel.secureMask.put(column, mask);
}
}
return this;
}
use of org.sagacity.sqltoy.config.model.SecureMask in project sagacity-sqltoy by chenrenfei.
the class SqlXMLConfigParse method parseSecureMask.
/**
* @todo 解析安全脱敏配置
* @param sqlToyConfig
* @param maskElts
*/
public static void parseSecureMask(SqlToyConfig sqlToyConfig, NodeList maskElts) {
if (maskElts == null || maskElts.getLength() == 0) {
return;
}
// <secure-mask columns="" type="name" head-size="" tail-size=""
// mask-code="*****" mask-rate="50%"/>
List<SecureMask> secureMasks = new ArrayList<SecureMask>();
String tmp;
Element elt;
for (int i = 0; i < maskElts.getLength(); i++) {
elt = (Element) maskElts.item(i);
tmp = getAttrValue(elt, "columns");
// 兼容老版本
if (tmp == null) {
tmp = getAttrValue(elt, "column");
}
String[] columns = trimParams(tmp.toLowerCase().split("\\,"));
String type = getAttrValue(elt, "type").toLowerCase();
String maskCode = getAttrValue(elt, "mask-code");
String headSize = getAttrValue(elt, "head-size");
String tailSize = getAttrValue(elt, "tail-size");
String maskRate = getAttrValue(elt, "mask-rate");
if (maskRate == null) {
maskRate = getAttrValue(elt, "mask-percent");
}
// 剔除百分号
if (maskRate != null) {
maskRate = maskRate.replace("%", "").trim();
}
for (String col : columns) {
SecureMask secureMask = new SecureMask();
secureMask.setColumn(col);
secureMask.setType(type);
secureMask.setMaskCode(maskCode);
if (secureMask.getMaskCode() == null) {
if (secureMask.getType().equals("id-card") || secureMask.getType().equals("bank-card") || secureMask.getType().equals("email") || secureMask.getType().equals("address") || secureMask.getType().equals("address")) {
secureMask.setMaskCode("******");
} else if (secureMask.getType().equals("name")) {
secureMask.setMaskCode("**");
} else {
secureMask.setMaskCode("****");
}
}
if (StringUtil.isNotBlank(headSize)) {
secureMask.setHeadSize(Integer.parseInt(headSize));
}
if (StringUtil.isNotBlank(tailSize)) {
secureMask.setTailSize(Integer.parseInt(tailSize));
}
if (StringUtil.isNotBlank(maskRate)) {
// 小数
if (Double.parseDouble(maskRate) < 1) {
secureMask.setMaskRate(Double.valueOf(Double.parseDouble(maskRate) * 100).intValue());
} else {
secureMask.setMaskRate(Double.valueOf(maskRate).intValue());
}
}
secureMasks.add(secureMask);
}
}
sqlToyConfig.setSecureMasks(secureMasks);
}
use of org.sagacity.sqltoy.config.model.SecureMask in project sagacity-sqltoy by chenrenfei.
the class SqlXMLConfigParse method parseSecureMask.
/**
* @todo 解析安全脱敏配置
* @param maskElts
* @return
*/
public static SecureMask[] parseSecureMask(List<Element> maskElts) {
if (maskElts != null && !maskElts.isEmpty()) {
// <secure-mask column="" type="name" head-size="" tail-size=""
// mask-code="*****" mask-rate="50%"/>
SecureMask[] secureMasks = new SecureMask[maskElts.size()];
int index = 0;
for (Element elt : maskElts) {
SecureMask secureMask = new SecureMask();
secureMask.setColumn(getAttrValue(elt, "column"));
secureMask.setType(getAttrValue(elt, "type").toLowerCase());
secureMask.setMaskCode(getAttrValue(elt, "mask-code"));
if (secureMask.getMaskCode() == null) {
if (secureMask.getType().equals("id-card") || secureMask.getType().equals("bank-card") || secureMask.getType().equals("email") || secureMask.getType().equals("address") || secureMask.getType().equals("address")) {
secureMask.setMaskCode("******");
} else if (secureMask.getType().equals("name")) {
secureMask.setMaskCode("**");
} else
secureMask.setMaskCode("****");
}
String headSize = getAttrValue(elt, "head-size");
String tailSize = getAttrValue(elt, "tail-size");
String maskRate = getAttrValue(elt, "mask-rate");
if (maskRate == null)
maskRate = getAttrValue(elt, "mask-percent");
if (StringUtil.isNotBlank(headSize))
secureMask.setHeadSize(Integer.parseInt(headSize));
if (StringUtil.isNotBlank(tailSize))
secureMask.setTailSize(Integer.parseInt(tailSize));
if (StringUtil.isNotBlank(maskRate))
secureMask.setMaskRate(Integer.parseInt(maskRate.replace("%", "")));
secureMasks[index] = secureMask;
index++;
}
return secureMasks;
}
return null;
}
use of org.sagacity.sqltoy.config.model.SecureMask in project sagacity-sqltoy by chenrenfei.
the class ResultUtils method calculate.
/**
* @todo 对查询结果进行计算处理:字段脱敏、格式化、数据旋转、同步环比、分组汇总等
* @param desensitizeProvider
* @param sqlToyConfig
* @param dataSetResult
* @param pivotCategorySet
* @param extend
* @return
*/
public static boolean calculate(DesensitizeProvider desensitizeProvider, SqlToyConfig sqlToyConfig, DataSetResult dataSetResult, List pivotCategorySet, QueryExecutorExtend extend) {
List items = dataSetResult.getRows();
// 数据为空直接跳出处理
if (items == null || items.isEmpty()) {
return false;
}
boolean changedCols = false;
List<SecureMask> secureMasks = sqlToyConfig.getSecureMasks();
List<FormatModel> formatModels = sqlToyConfig.getFormatModels();
List resultProcessors = new ArrayList();
if (!sqlToyConfig.getResultProcessor().isEmpty()) {
resultProcessors.addAll(sqlToyConfig.getResultProcessor());
}
if (extend != null && !extend.calculators.isEmpty()) {
resultProcessors.addAll(extend.calculators);
}
// 整理列名称跟index的对照map
LabelIndexModel labelIndexMap = null;
if (!secureMasks.isEmpty() || !formatModels.isEmpty() || (extend != null && (!extend.secureMask.isEmpty() || !extend.colsFormat.isEmpty())) || !resultProcessors.isEmpty()) {
labelIndexMap = wrapLabelIndexMap(dataSetResult.getLabelNames());
}
// 字段脱敏
if (!secureMasks.isEmpty()) {
secureMask(desensitizeProvider, items, secureMasks.iterator(), labelIndexMap);
}
// 自动格式化
if (!formatModels.isEmpty()) {
formatColumn(items, formatModels.iterator(), labelIndexMap);
}
// 扩展脱敏和格式化处理
if (extend != null) {
if (!extend.secureMask.isEmpty()) {
secureMask(desensitizeProvider, items, extend.secureMask.values().iterator(), labelIndexMap);
}
if (!extend.colsFormat.isEmpty()) {
formatColumn(items, extend.colsFormat.values().iterator(), labelIndexMap);
}
}
// 计算
if (!resultProcessors.isEmpty()) {
Object processor;
for (int i = 0; i < resultProcessors.size(); i++) {
processor = resultProcessors.get(i);
// 数据旋转(行转列)
if (processor instanceof PivotModel) {
items = pivotResult((PivotModel) processor, labelIndexMap, items, pivotCategorySet);
changedCols = true;
} else // 列转行
if (processor instanceof UnpivotModel) {
items = UnpivotList.process((UnpivotModel) processor, dataSetResult, labelIndexMap, items);
} else if (processor instanceof SummaryModel) {
// 数据汇总合计
GroupSummary.process((SummaryModel) processor, labelIndexMap, items);
} else if (processor instanceof ColsChainRelativeModel) {
// 列数据环比
ColsChainRelative.process((ColsChainRelativeModel) processor, labelIndexMap, items);
changedCols = true;
} else if (processor instanceof RowsChainRelativeModel) {
// 行数据环比
RowsChainRelative.process((RowsChainRelativeModel) processor, labelIndexMap, items);
} else if (processor instanceof ReverseModel) {
// 数据反序
ReverseList.process((ReverseModel) processor, labelIndexMap, items);
}
}
dataSetResult.setRows(items);
}
return changedCols;
}
Aggregations