refactor(操作日志): 修改记录日志方式,避免在高版本jdk中出现反射警告

This commit is contained in:
CaptainB 2022-04-01 18:29:20 +08:00 committed by 刘瑞斌
parent 2c781b7fa4
commit 508f26e625
2 changed files with 16 additions and 32 deletions

View File

@ -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 "";
/**
* 传入执行类
*

View File

@ -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<String> beforeValue = new ThreadLocal<>();
private ThreadLocal<String> 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<String, Object> memberValues = (Map<String, Object>) 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<String, Object> memberValues = (Map<String, Object>) 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<DetailColumn> columns = ReflexObjectUtil.compared(JSON.parseObject(msLog.beforeValue(), OperatingLogDetails.class), details, msLog.module());
List<DetailColumn> 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();
}
}
}