From 508f26e62572bad9ba041bbb89eb56d230b79dcb Mon Sep 17 00:00:00 2001 From: CaptainB Date: Fri, 1 Apr 2022 18:29:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97):?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E8=AE=B0=E5=BD=95=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=8C=E9=81=BF=E5=85=8D=E5=9C=A8=E9=AB=98?= =?UTF-8?q?=E7=89=88=E6=9C=ACjdk=E4=B8=AD=E5=87=BA=E7=8E=B0=E5=8F=8D?= =?UTF-8?q?=E5=B0=84=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../log/annotation/MsAuditLog.java | 13 ------- .../metersphere/log/aspect/MsLogAspect.java | 35 +++++++++---------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/backend/src/main/java/io/metersphere/log/annotation/MsAuditLog.java b/backend/src/main/java/io/metersphere/log/annotation/MsAuditLog.java index 143f46762f..289930664e 100644 --- a/backend/src/main/java/io/metersphere/log/annotation/MsAuditLog.java +++ b/backend/src/main/java/io/metersphere/log/annotation/MsAuditLog.java @@ -27,13 +27,6 @@ public @interface MsAuditLog { */ String project() default ""; - /** - * 操作人 - * - * @return - */ - String operUser() default ""; - /** * 操作类型 * @@ -66,12 +59,6 @@ public @interface MsAuditLog { */ String beforeEvent() default ""; - /** - * - * @return - */ - String beforeValue() default ""; - /** * 传入执行类 * diff --git a/backend/src/main/java/io/metersphere/log/aspect/MsLogAspect.java b/backend/src/main/java/io/metersphere/log/aspect/MsLogAspect.java index c28e9d93dc..402d99e856 100644 --- a/backend/src/main/java/io/metersphere/log/aspect/MsLogAspect.java +++ b/backend/src/main/java/io/metersphere/log/aspect/MsLogAspect.java @@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON; import io.metersphere.base.domain.OperatingLogWithBLOBs; import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.SessionUtils; -import io.metersphere.i18n.Translator; import io.metersphere.log.annotation.MsAuditLog; import io.metersphere.log.service.OperatingLogService; import io.metersphere.log.utils.ReflexObjectUtil; @@ -31,12 +30,8 @@ import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.lang.reflect.Proxy; import java.util.List; -import java.util.Map; import java.util.UUID; /** @@ -60,6 +55,9 @@ public class MsLogAspect { @Resource private OperatingLogService operatingLogService; + private ThreadLocal beforeValue = new ThreadLocal<>(); + private ThreadLocal operUser = new ThreadLocal<>(); + /** * 定义切点 @Pointcut 在注解的位置切入代码 */ @@ -85,14 +83,10 @@ public class MsLogAspect { for (int len = 0; len < params.length; len++) { context.setVariable(params[len], args[len]); } - InvocationHandler invocationHandler = Proxy.getInvocationHandler(msLog); - Field value = invocationHandler.getClass().getDeclaredField("memberValues"); - value.setAccessible(true); boolean isNext = false; for (Class clazz : msLog.msClass()) { if (clazz.getName().equals("io.metersphere.commons.utils.SessionUtils")) { - Map memberValues = (Map) value.get(invocationHandler); - memberValues.put("operUser", SessionUtils.getUserId()); + operUser.set(SessionUtils.getUserId()); continue; } context.setVariable("msClass", applicationContext.getBean(clazz)); @@ -101,8 +95,7 @@ public class MsLogAspect { if (isNext) { Expression expression = parser.parseExpression(msLog.beforeEvent()); String beforeContent = expression.getValue(context, String.class); - Map memberValues = (Map) value.get(invocationHandler); - memberValues.put("beforeValue", beforeContent); + beforeValue.set(beforeContent); } } } catch (Exception e) { @@ -183,6 +176,7 @@ public class MsLogAspect { } // 操作内容 + String beforeValue = this.beforeValue.get(); if (StringUtils.isNotEmpty(msLog.content())) { Expression expression = parser.parseExpression(msLog.content()); String content = expression.getValue(context, String.class); @@ -198,9 +192,9 @@ public class MsLogAspect { msOperLog.setSourceId(details.getSourceId()); msOperLog.setCreateUser(details.getCreateUser()); } - if (StringUtils.isNotEmpty(content) && StringUtils.isNotEmpty(msLog.beforeValue())) { + if (StringUtils.isNotEmpty(content) && StringUtils.isNotEmpty(beforeValue)) { OperatingLogDetails details = JSON.parseObject(content, OperatingLogDetails.class); - List columns = ReflexObjectUtil.compared(JSON.parseObject(msLog.beforeValue(), OperatingLogDetails.class), details, msLog.module()); + List columns = ReflexObjectUtil.compared(JSON.parseObject(beforeValue, OperatingLogDetails.class), details, msLog.module()); details.setColumns(columns); msOperLog.setOperContent(JSON.toJSONString(details)); msOperLog.setSourceId(details.getSourceId()); @@ -213,9 +207,9 @@ public class MsLogAspect { } } // 只有前置操作的处理/如 删除操作 - if (StringUtils.isNotEmpty(msLog.beforeEvent()) && StringUtils.isNotEmpty(msLog.beforeValue()) && StringUtils.isEmpty(msLog.content())) { - msOperLog.setOperContent(msLog.beforeValue()); - OperatingLogDetails details = JSON.parseObject(msLog.beforeValue(), OperatingLogDetails.class); + if (StringUtils.isNotEmpty(msLog.beforeEvent()) && StringUtils.isNotEmpty(beforeValue) && StringUtils.isEmpty(msLog.content())) { + msOperLog.setOperContent(beforeValue); + OperatingLogDetails details = JSON.parseObject(beforeValue, OperatingLogDetails.class); if (StringUtils.isEmpty(msLog.title())) { msOperLog.setOperTitle(details.getTitle()); } @@ -234,8 +228,8 @@ public class MsLogAspect { msOperLog.setOperTime(System.currentTimeMillis()); //获取用户名 - if (StringUtils.isNotEmpty(msLog.operUser())) { - msOperLog.setOperUser(msLog.operUser()); + if (StringUtils.isNotEmpty(operUser.get())) { + msOperLog.setOperUser(operUser.get()); } else { msOperLog.setOperUser(SessionUtils.getUserId()); } @@ -263,6 +257,9 @@ public class MsLogAspect { } } catch (Exception e) { LogUtil.error("操作日志写入异常:" + joinPoint.getSignature()); + } finally { + operUser.remove(); + beforeValue.remove(); } } }