From 86caf24ec6e0b82e0ccdec374cd13fc3f79df2f8 Mon Sep 17 00:00:00 2001 From: wsm <2746563060@qq.com> Date: Wed, 5 Aug 2026 15:10:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(syslog):=20=E6=93=8D=E4=BD=9C=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=94=AF=E6=8C=81=20@AutoLogIgnore=20=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增字段级注解 @AutoLogIgnore,标注在实体字段上; 写操作日志序列化请求参数时,按运行时类型自动识别并 将标注字段的值替换为 ***,避免敏感数据写入日志。 --- .../jeecg/common/aspect/AutoLogAspect.java | 35 ++++++++++++++++++- .../aspect/annotation/AutoLogIgnore.java | 16 +++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/AutoLogIgnore.java diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/AutoLogAspect.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/AutoLogAspect.java index be41114..5ae2552 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/AutoLogAspect.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/AutoLogAspect.java @@ -2,6 +2,7 @@ package org.jeecg.common.aspect; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.PropertyFilter; +import com.alibaba.fastjson.serializer.ValueFilter; import lombok.extern.slf4j.Slf4j; import org.apache.shiro.SecurityUtils; import org.aspectj.lang.JoinPoint; @@ -13,6 +14,7 @@ import org.aspectj.lang.reflect.MethodSignature; import org.jeecg.common.api.dto.LogDTO; import org.jeecg.common.api.vo.Result; import org.jeecg.common.aspect.annotation.AutoLog; +import org.jeecg.common.aspect.annotation.AutoLogIgnore; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.constant.enums.ModuleType; import org.jeecg.common.constant.enums.OperateTypeEnum; @@ -29,8 +31,11 @@ import javax.annotation.Resource; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Date; +import java.util.HashSet; +import java.util.Set; /** @@ -48,6 +53,23 @@ public class AutoLogAspect { @Resource private BaseCommonService baseCommonService; + //update-begin-author:wsm date:20260805 for:按运行时类型缓存标注了@AutoLogIgnore的字段名集合 + private final ClassValue> autoLogIgnoreCache = new ClassValue>() { + @Override + protected Set computeValue(Class type) { + Set fieldNames = new HashSet<>(); + for (Class clazz = type; clazz != null && clazz != Object.class; clazz = clazz.getSuperclass()) { + for (Field field : clazz.getDeclaredFields()) { + if (field.getAnnotation(AutoLogIgnore.class) != null) { + fieldNames.add(field.getName()); + } + } + } + return fieldNames; + } + }; + //update-end-author:wsm date:20260805 for:按运行时类型缓存标注了@AutoLogIgnore的字段名集合 + @Pointcut("@annotation(org.jeecg.common.aspect.annotation.AutoLog)") public void logPointCut() { @@ -170,8 +192,19 @@ public class AutoLogAspect { return true; } }; - params = JSONObject.toJSONString(arguments, profilter); //update-end-author:taoyan date:20200724 for:日志数据太长的直接过滤掉 + //update-begin-author:wsm date:20260805 for:序列化请求参数时对标注了@AutoLogIgnore的字段值脱敏 + ValueFilter valueFilter = (source, name, value) -> { + if (source != null) { + Set ignoreFields = autoLogIgnoreCache.get(source.getClass()); + if (ignoreFields.contains(name)) { + return "***"; + } + } + return value; + }; + params = JSONObject.toJSONString(arguments, profilter, valueFilter); + //update-end-author:wsm date:20260805 for:序列化请求参数时对标注了@AutoLogIgnore的字段值脱敏 } else { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/AutoLogIgnore.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/AutoLogIgnore.java new file mode 100644 index 0000000..fcfc691 --- /dev/null +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/AutoLogIgnore.java @@ -0,0 +1,16 @@ +package org.jeecg.common.aspect.annotation; + +import java.lang.annotation.*; + +/** + * 标注在实体字段上 + * 写操作日志序列化请求参数时,该字段的值会被自动替换为 ***,避免敏感数据写入日志 + * + * @Author wsm + * @Date 2026年8月5日 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface AutoLogIgnore { +}