Parcourir la source

操作日志记录支持排除敏感属性字段

RuoYi il y a 3 ans
Parent
commit
d6df1fe7b3

+ 14 - 2
ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java

@@ -21,6 +21,7 @@ import com.ruoyi.common.core.utils.StringUtils;
21 21
 import com.ruoyi.common.core.utils.ip.IpUtils;
22 22
 import com.ruoyi.common.log.annotation.Log;
23 23
 import com.ruoyi.common.log.enums.BusinessStatus;
24
+import com.ruoyi.common.log.filter.PropertyPreExcludeFilter;
24 25
 import com.ruoyi.common.log.service.AsyncLogService;
25 26
 import com.ruoyi.common.security.utils.SecurityUtils;
26 27
 import com.ruoyi.system.api.domain.SysOperLog;
@@ -35,7 +36,10 @@ import com.ruoyi.system.api.domain.SysOperLog;
35 36
 public class LogAspect
36 37
 {
37 38
     private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
38
-    
39
+
40
+    /** 排除敏感属性字段 */
41
+    public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" };
42
+
39 43
     @Autowired
40 44
     private AsyncLogService asyncLogService;
41 45
 
@@ -162,7 +166,7 @@ public class LogAspect
162 166
                 {
163 167
                     try
164 168
                     {
165
-                        Object jsonObj = JSON.toJSON(o);
169
+                        String jsonObj = JSON.toJSONString(o, excludePropertyPreFilter());
166 170
                         params += jsonObj.toString() + " ";
167 171
                     }
168 172
                     catch (Exception e)
@@ -175,6 +179,14 @@ public class LogAspect
175 179
     }
176 180
 
177 181
     /**
182
+     * 忽略敏感属性
183
+     */
184
+    public PropertyPreExcludeFilter excludePropertyPreFilter()
185
+    {
186
+        return new PropertyPreExcludeFilter().addExcludes(EXCLUDE_PROPERTIES);
187
+    }
188
+
189
+    /**
178 190
      * 判断是否需要过滤的对象。
179 191
      * 
180 192
      * @param o 对象信息。

+ 24 - 0
ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/filter/PropertyPreExcludeFilter.java

@@ -0,0 +1,24 @@
1
+package com.ruoyi.common.log.filter;
2
+
3
+import com.alibaba.fastjson2.filter.SimplePropertyPreFilter;
4
+
5
+/**
6
+ * 排除JSON敏感属性
7
+ * 
8
+ * @author ruoyi
9
+ */
10
+public class PropertyPreExcludeFilter extends SimplePropertyPreFilter
11
+{
12
+    public PropertyPreExcludeFilter()
13
+    {
14
+    }
15
+
16
+    public PropertyPreExcludeFilter addExcludes(String... filters)
17
+    {
18
+        for (int i = 0; i < filters.length; i++)
19
+        {
20
+            this.getExcludes().add(filters[i]);
21
+        }
22
+        return this;
23
+    }
24
+}