From d6df1fe7b30c373b864ea70bbf5edb9b5ffa2e99 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Wed, 10 Aug 2022 18:46:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=94=AF=E6=8C=81=E6=8E=92=E9=99=A4=E6=95=8F=E6=84=9F?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/log/aspect/LogAspect.java | 16 +++++++++++-- .../log/filter/PropertyPreExcludeFilter.java | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/filter/PropertyPreExcludeFilter.java diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java index 14178f8f..7de60b02 100644 --- a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java +++ b/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; import com.ruoyi.common.core.utils.ip.IpUtils; import com.ruoyi.common.log.annotation.Log; import com.ruoyi.common.log.enums.BusinessStatus; +import com.ruoyi.common.log.filter.PropertyPreExcludeFilter; import com.ruoyi.common.log.service.AsyncLogService; import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.system.api.domain.SysOperLog; @@ -35,7 +36,10 @@ import com.ruoyi.system.api.domain.SysOperLog; public class LogAspect { private static final Logger log = LoggerFactory.getLogger(LogAspect.class); - + + /** 排除敏感属性字段 */ + public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" }; + @Autowired private AsyncLogService asyncLogService; @@ -162,7 +166,7 @@ public class LogAspect { try { - Object jsonObj = JSON.toJSON(o); + String jsonObj = JSON.toJSONString(o, excludePropertyPreFilter()); params += jsonObj.toString() + " "; } catch (Exception e) @@ -174,6 +178,14 @@ public class LogAspect return params.trim(); } + /** + * 忽略敏感属性 + */ + public PropertyPreExcludeFilter excludePropertyPreFilter() + { + return new PropertyPreExcludeFilter().addExcludes(EXCLUDE_PROPERTIES); + } + /** * 判断是否需要过滤的对象。 * diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/filter/PropertyPreExcludeFilter.java b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/filter/PropertyPreExcludeFilter.java new file mode 100644 index 00000000..5391993e --- /dev/null +++ b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/filter/PropertyPreExcludeFilter.java @@ -0,0 +1,24 @@ +package com.ruoyi.common.log.filter; + +import com.alibaba.fastjson2.filter.SimplePropertyPreFilter; + +/** + * 排除JSON敏感属性 + * + * @author ruoyi + */ +public class PropertyPreExcludeFilter extends SimplePropertyPreFilter +{ + public PropertyPreExcludeFilter() + { + } + + public PropertyPreExcludeFilter addExcludes(String... filters) + { + for (int i = 0; i < filters.length; i++) + { + this.getExcludes().add(filters[i]); + } + return this; + } +}