| | |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.Objects; |
| | | import java.util.Map; |
| | | |
| | | /** 审计数据统一脱敏并将常用字段名转换为中文,供日志页面直接展示。 */ |
| | |
| | | return mask(JSON.toJSONString(localize(json, entityType))); |
| | | } |
| | | |
| | | /** 返回前后快照中实际发生改变的字段;未变化字段不会写入审计记录。 */ |
| | | public static ChangeSet changes(String before, String after) { |
| | | JSONObject beforeObject = object(before); |
| | | JSONObject afterObject = object(after); |
| | | JSONObject changedBefore = new JSONObject(true); |
| | | JSONObject changedAfter = new JSONObject(true); |
| | | Map<String, Object> all = new LinkedHashMap<String, Object>(); |
| | | all.putAll(beforeObject); |
| | | all.putAll(afterObject); |
| | | for (String field : all.keySet()) { |
| | | Object oldValue = beforeObject.get(field); |
| | | Object newValue = afterObject.get(field); |
| | | if (!Objects.equals(JSON.toJSONString(oldValue), JSON.toJSONString(newValue))) { |
| | | changedBefore.put(field, oldValue); |
| | | changedAfter.put(field, newValue); |
| | | } |
| | | } |
| | | return new ChangeSet(changedBefore.isEmpty() ? null : mask(changedBefore.toJSONString()), |
| | | changedAfter.isEmpty() ? null : mask(changedAfter.toJSONString())); |
| | | } |
| | | |
| | | private static JSONObject object(String value) { |
| | | if (value == null || value.trim().isEmpty()) return new JSONObject(true); |
| | | try { |
| | | JSONObject object = JSON.parseObject(value); |
| | | return object == null ? new JSONObject(true) : object; |
| | | } catch (Exception ignored) { |
| | | return new JSONObject(true); |
| | | } |
| | | } |
| | | |
| | | public static final class ChangeSet { |
| | | private final String before; |
| | | private final String after; |
| | | private ChangeSet(String before, String after) { this.before = before; this.after = after; } |
| | | public String getBefore() { return before; } |
| | | public String getAfter() { return after; } |
| | | } |
| | | |
| | | private static Object parse(String value) { |
| | | try { return JSON.parse(value); } catch (Exception ignored) { return value; } |
| | | } |