作者 RuoYi

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

@@ -20,7 +20,7 @@ import com.ruoyi.common.enums.OperatorType; @@ -20,7 +20,7 @@ import com.ruoyi.common.enums.OperatorType;
20 public @interface Log 20 public @interface Log
21 { 21 {
22 /** 22 /**
23 - * 模块 23 + * 模块
24 */ 24 */
25 public String title() default ""; 25 public String title() default "";
26 26
@@ -43,4 +43,9 @@ public @interface Log @@ -43,4 +43,9 @@ public @interface Log
43 * 是否保存响应的参数 43 * 是否保存响应的参数
44 */ 44 */
45 public boolean isSaveResponseData() default true; 45 public boolean isSaveResponseData() default true;
  46 +
  47 + /**
  48 + * 排除指定的请求参数
  49 + */
  50 + public String[] excludeParamNames() default {};
46 } 51 }
@@ -4,6 +4,7 @@ import java.util.Collection; @@ -4,6 +4,7 @@ import java.util.Collection;
4 import java.util.Map; 4 import java.util.Map;
5 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse; 6 import javax.servlet.http.HttpServletResponse;
  7 +import org.apache.commons.lang3.ArrayUtils;
7 import org.aspectj.lang.JoinPoint; 8 import org.aspectj.lang.JoinPoint;
8 import org.aspectj.lang.annotation.AfterReturning; 9 import org.aspectj.lang.annotation.AfterReturning;
9 import org.aspectj.lang.annotation.AfterThrowing; 10 import org.aspectj.lang.annotation.AfterThrowing;
@@ -146,7 +147,7 @@ public class LogAspect @@ -146,7 +147,7 @@ public class LogAspect
146 if (log.isSaveRequestData()) 147 if (log.isSaveRequestData())
147 { 148 {
148 // 获取参数的信息,传入到数据库中。 149 // 获取参数的信息,传入到数据库中。
149 - setRequestValue(joinPoint, operLog); 150 + setRequestValue(joinPoint, operLog, log.excludeParamNames());
150 } 151 }
151 // 是否需要保存response,参数和值 152 // 是否需要保存response,参数和值
152 if (log.isSaveResponseData() && StringUtils.isNotNull(jsonResult)) 153 if (log.isSaveResponseData() && StringUtils.isNotNull(jsonResult))
@@ -161,25 +162,25 @@ public class LogAspect @@ -161,25 +162,25 @@ public class LogAspect
161 * @param operLog 操作日志 162 * @param operLog 操作日志
162 * @throws Exception 异常 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 String requestMethod = operLog.getRequestMethod(); 167 String requestMethod = operLog.getRequestMethod();
167 if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) 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 operLog.setOperParam(StringUtils.substring(params, 0, 2000)); 171 operLog.setOperParam(StringUtils.substring(params, 0, 2000));
171 } 172 }
172 else 173 else
173 { 174 {
174 Map<?, ?> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest()); 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 String params = ""; 185 String params = "";
185 if (paramsArray != null && paramsArray.length > 0) 186 if (paramsArray != null && paramsArray.length > 0)
@@ -190,7 +191,7 @@ public class LogAspect @@ -190,7 +191,7 @@ public class LogAspect
190 { 191 {
191 try 192 try
192 { 193 {
193 - String jsonObj = JSON.toJSONString(o, excludePropertyPreFilter()); 194 + String jsonObj = JSON.toJSONString(o, excludePropertyPreFilter(excludeParamNames));
194 params += jsonObj.toString() + " "; 195 params += jsonObj.toString() + " ";
195 } 196 }
196 catch (Exception e) 197 catch (Exception e)
@@ -205,9 +206,9 @@ public class LogAspect @@ -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 /**