Преглед изворни кода

日志注解支持排除指定的请求参数

RuoYi пре 3 година
родитељ
комит
1126e2234f

+ 5 - 0
ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/annotation/Log.java

@@ -43,4 +43,9 @@ public @interface Log
43 43
      * 是否保存响应的参数
44 44
      */
45 45
     public boolean isSaveResponseData() default true;
46
+
47
+    /**
48
+     * 排除指定的请求参数
49
+     */
50
+    public String[] excludeParamNames() default {};
46 51
 }

+ 9 - 8
ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java

@@ -4,6 +4,7 @@ import java.util.Collection;
4 4
 import java.util.Map;
5 5
 import javax.servlet.http.HttpServletRequest;
6 6
 import javax.servlet.http.HttpServletResponse;
7
+import org.apache.commons.lang3.ArrayUtils;
7 8
 import org.aspectj.lang.JoinPoint;
8 9
 import org.aspectj.lang.annotation.AfterReturning;
9 10
 import org.aspectj.lang.annotation.AfterThrowing;
@@ -146,7 +147,7 @@ public class LogAspect
146 147
         if (log.isSaveRequestData())
147 148
         {
148 149
             // 获取参数的信息,传入到数据库中。
149
-            setRequestValue(joinPoint, operLog);
150
+            setRequestValue(joinPoint, operLog, log.excludeParamNames());
150 151
         }
151 152
         // 是否需要保存response,参数和值
152 153
         if (log.isSaveResponseData() && StringUtils.isNotNull(jsonResult))
@@ -161,25 +162,25 @@ public class LogAspect
161 162
      * @param operLog 操作日志
162 163
      * @throws Exception 异常
163 164
      */
164
-    private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog) throws Exception
165
+    private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog, String[] excludeParamNames) throws Exception
165 166
     {
166 167
         String requestMethod = operLog.getRequestMethod();
167 168
         if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod))
168 169
         {
169
-            String params = argsArrayToString(joinPoint.getArgs());
170
+            String params = argsArrayToString(joinPoint.getArgs(), excludeParamNames);
170 171
             operLog.setOperParam(StringUtils.substring(params, 0, 2000));
171 172
         }
172 173
         else
173 174
         {
174 175
             Map<?, ?> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest());
175
-            operLog.setOperParam(StringUtils.substring(JSON.toJSONString(paramsMap, excludePropertyPreFilter()), 0, 2000));
176
+            operLog.setOperParam(StringUtils.substring(JSON.toJSONString(paramsMap, excludePropertyPreFilter(excludeParamNames)), 0, 2000));
176 177
         }
177 178
     }
178 179
 
179 180
     /**
180 181
      * 参数拼装
181 182
      */
182
-    private String argsArrayToString(Object[] paramsArray)
183
+    private String argsArrayToString(Object[] paramsArray, String[] excludeParamNames)
183 184
     {
184 185
         String params = "";
185 186
         if (paramsArray != null && paramsArray.length > 0)
@@ -190,7 +191,7 @@ public class LogAspect
190 191
                 {
191 192
                     try
192 193
                     {
193
-                        String jsonObj = JSON.toJSONString(o, excludePropertyPreFilter());
194
+                        String jsonObj = JSON.toJSONString(o, excludePropertyPreFilter(excludeParamNames));
194 195
                         params += jsonObj.toString() + " ";
195 196
                     }
196 197
                     catch (Exception e)
@@ -205,9 +206,9 @@ public class LogAspect
205 206
     /**
206 207
      * 忽略敏感属性
207 208
      */
208
-    public PropertyPreExcludeFilter excludePropertyPreFilter()
209
+    public PropertyPreExcludeFilter excludePropertyPreFilter(String[] excludeParamNames)
209 210
     {
210
-        return new PropertyPreExcludeFilter().addExcludes(EXCLUDE_PROPERTIES);
211
+        return new PropertyPreExcludeFilter().addExcludes(ArrayUtils.addAll(EXCLUDE_PROPERTIES, excludeParamNames));
211 212
     }
212 213
 
213 214
     /**