From e6e92a5503e15cba333b97bd33304c0173f544db Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Fri, 10 Jan 2025 09:13:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../njcn/gather/system/log/aop/LogAdvice.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/system/src/main/java/com/njcn/gather/system/log/aop/LogAdvice.java b/system/src/main/java/com/njcn/gather/system/log/aop/LogAdvice.java index b8029b90..cbc5e1cd 100644 --- a/system/src/main/java/com/njcn/gather/system/log/aop/LogAdvice.java +++ b/system/src/main/java/com/njcn/gather/system/log/aop/LogAdvice.java @@ -1,16 +1,21 @@ package com.njcn.gather.system.log.aop; +import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; +import com.alibaba.fastjson.JSONObject; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.njcn.common.bean.CustomCacheUtil; import com.njcn.common.pojo.annotation.OperateInfo; +import com.njcn.common.pojo.constant.SecurityConstants; import com.njcn.db.mybatisplus.constant.UserConstant; import com.njcn.gather.system.log.pojo.po.SysLogAudit; import com.njcn.gather.system.log.service.ISysLogAuditService; +import com.njcn.gather.user.user.pojo.po.SysUser; import com.njcn.web.utils.HttpServletUtil; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.logging.log4j.util.Strings; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @@ -45,8 +50,17 @@ public class LogAdvice implements ApplicationListener { @Around("logPointcut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { - CustomCacheUtil customCacheUtil = SpringUtil.getBean(CustomCacheUtil.CACHE_NAME); - //String username = customCacheUtil.get(UserConstant.USER_NAME, false); + String username = ""; + HttpServletRequest request = HttpServletUtil.getRequest(); + String authorization = request.getHeader(SecurityConstants.AUTHORIZATION_KEY); + if (StrUtil.isNotBlank(authorization)) { + String tokenStr = authorization.replace(SecurityConstants.AUTHORIZATION_PREFIX, Strings.EMPTY); + CustomCacheUtil customCacheUtil = SpringUtil.getBean(CustomCacheUtil.CACHE_NAME); + String userjson = customCacheUtil.get(tokenStr, false); + SysUser user = JSONObject.parseObject(userjson, SysUser.class); + username = user.getName(); + } + // String operationType = ""; // String operateResult = "成功"; // Integer level = 0; @@ -55,9 +69,9 @@ public class LogAdvice implements ApplicationListener { Object result = null; try { result = joinPoint.proceed(); - //addLogToQueue(joinPoint, username, "操作日志", "成功", 0, 0); + addLogToQueue(joinPoint, "".equals(username) ? UserConstant.UNKNOWN_USER_ID : username, "操作日志", "成功", 0, 0); } catch (Throwable e) { - //addLogToQueue(joinPoint, username, "告警日志", "失败", 1, 1); + addLogToQueue(joinPoint, "".equals(username) ? UserConstant.UNKNOWN_USER_ID : username, "告警日志", "失败", 1, 1); throw e; } return result;