首页>代码>Spring MVC+Spring+apache Shiro整合开发客户端日志埋点校验系统>/logreplay/src/main/java/com/sogou/map/logreplay/audit/AuditFastjsonFilter.java
package com.sogou.map.logreplay.audit;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.audit4j.core.annotation.DeIdentify;
import org.audit4j.core.annotation.IgnoreAudit;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.ValueFilter;
public class AuditFastjsonFilter implements PropertyFilter, ValueFilter {
private static final ConcurrentMap<String, Map<String, DeIdentify>> DEIDENTIFY_INDEX = new ConcurrentHashMap<String, Map<String, DeIdentify>>();
private static final ConcurrentMap<String, Map<String, Boolean>> IGNORE_AUDIT_INDEX = new ConcurrentHashMap<String, Map<String, Boolean>>();
private static final AuditFastjsonFilter INSTANCE = new AuditFastjsonFilter();
private AuditFastjsonFilter() {}
@Override
public Object process(Object object, String name, Object value) {
Class<?> clazz = object.getClass();
String indexKey = clazz.getName();
Map<String, DeIdentify> deIdentifyMap = DEIDENTIFY_INDEX.get(indexKey);
if (deIdentifyMap == null) {
DEIDENTIFY_INDEX.putIfAbsent(indexKey, createDeIdentifyMapping(clazz));
deIdentifyMap = DEIDENTIFY_INDEX.get(indexKey);
}
return ObjectToJsonSerializer.deidentifyValue(value, deIdentifyMap.get(name));
}
@Override
public boolean apply(Object object, String name, Object value) {
Class<?> clazz = object.getClass();
String indexKey = clazz.getName();
Map<String, Boolean> ignoreAuditMap = IGNORE_AUDIT_INDEX.get(indexKey);
if (ignoreAuditMap == null) {
IGNORE_AUDIT_INDEX.putIfAbsent(indexKey, createIgnoreAuditMapping(clazz));
ignoreAuditMap = IGNORE_AUDIT_INDEX.get(indexKey);
}
return !Boolean.TRUE.equals(ignoreAuditMap.get(name));
}
private static Map<String, DeIdentify> createDeIdentifyMapping(Class<?> clazz) {
Map<String, DeIdentify> mapping = new HashMap<String, DeIdentify>();
Field[] fields = clazz.getDeclaredFields();
for (Field field: fields) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
if (field.isAnnotationPresent(DeIdentify.class)) {
mapping.put(field.getName(), field.getAnnotation(DeIdentify.class));
}
}
return mapping;
}
public static Map<String, Boolean> createIgnoreAuditMapping(Class<?> clazz) {
Map<String, Boolean> mapping = new HashMap<String, Boolean>();
Field[] fields = clazz.getDeclaredFields();
for (Field field: fields) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
if (!field.isAnnotationPresent(IgnoreAudit.class)) {
continue;
}
mapping.put(field.getName(), Boolean.TRUE);
}
return mapping;
}
public static AuditFastjsonFilter instance() {
return INSTANCE;
}
}
最近下载更多
00044304 LV6
2022年8月23日
tutuhero LV8
2021年4月21日
zdm2157 LV3
2020年6月11日
zmw55555 LV1
2020年4月2日
Mason-想飞 LV2
2019年7月5日
z924931408 LV25
2019年6月10日
ybywx1030 LV7
2019年4月24日
最代码酒酒 LV20
2019年4月16日
denliv_hui LV14
2018年12月27日
gvin001 LV14
2018年12月12日
最近浏览更多
微信网友_6248713511227392 LV11
2022年12月5日
00044304 LV6
2022年8月22日
wanglinddad LV55
2022年4月15日
等你归来 LV2
2022年3月1日
囤华飞
2021年7月15日
暂无贡献等级
2840820049 LV9
2021年6月21日
tutuhero LV8
2021年4月21日
yanxiaoai LV6
2021年2月1日
啊最代码我要下 LV15
2020年11月16日
adminzk LV2
2020年7月12日

