Pre Merge pull request !435 from srma112233/master

pull/435/MERGE
srma112233 2023-02-20 04:54:48 +00:00 committed by Gitee
commit 9ad69c9850
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 87 additions and 20 deletions

View File

@ -0,0 +1,20 @@
package com.ruoyi.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
*
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface IgnoreTarget {
/**
* 使
*/
String[] target() default {};
}

View File

@ -0,0 +1,40 @@
package com.ruoyi.common.utils;
import com.ruoyi.common.annotation.IgnoreTarget;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
*
*
* @author ruoyi
*/
public class IgnoreFieldsUtils {
public static String[] getIgnoreFields(Object o, String target) {
Field[] fields = o.getClass().getDeclaredFields();
if (Objects.isNull(fields) || fields.length == 0) {
return new String[0];
}
List<String> list = new ArrayList<>();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (field.isAnnotationPresent(IgnoreTarget.class)) {
String[] t = field.getAnnotation(IgnoreTarget.class).target();
if (Objects.isNull(t) || t.length == 0) {
list.add(field.getName());
}
if (Arrays.asList(t).contains(target)) {
list.add(field.getName());
}
}
}
return list.toArray(new String[list.size()]);
}
}

View File

@ -2,8 +2,12 @@ package com.ruoyi.framework.aspectj;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.annotation.IgnoreTarget;
import com.ruoyi.common.utils.IgnoreFieldsUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
@ -194,27 +198,29 @@ public class LogAspect
/**
*
*/
private String argsArrayToString(Object[] paramsArray)
{
String params = "";
if (paramsArray != null && paramsArray.length > 0)
{
for (Object o : paramsArray)
{
if (StringUtils.isNotNull(o) && !isFilterObject(o))
{
try
{
Object jsonObj = JSONObject.toJSONString(o, excludePropertyPreFilter());
params += jsonObj.toString() + " ";
}
catch (Exception e)
{
}
}
private String argsArrayToString(Object[] paramsArray) {
StringBuffer sb = new StringBuffer();
if (Objects.isNull(paramsArray) || paramsArray.length <= 0) {
return StringUtils.EMPTY;
}
for (Object o : paramsArray) {
if (Objects.isNull(o) || isFilterObject(o)) {
continue;
}
try {
PropertyPreFilters.MySimplePropertyPreFilter filter = excludePropertyPreFilter();
// 过滤注解需要忽略的字段
filter.addExcludes(IgnoreFieldsUtils.getIgnoreFields(o, "LogAspect"));
sb.append(JSONObject.toJSONString(o, filter));
} catch (Exception e) {
//TODO something
}
}
return params.trim();
return sb.toString().trim();
}
/**
@ -248,7 +254,8 @@ public class LogAspect
return entry.getValue() instanceof MultipartFile;
}
}
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|| o instanceof BindingResult;
|| o instanceof BindingResult || clazz.isAnnotationPresent(IgnoreTarget.class);
}
}