From ad0539f90df37d449ae86d604ae9288af38651f2 Mon Sep 17 00:00:00 2001 From: Junjie <540245094@qq.com> Date: 星期三, 29 五月 2024 11:51:04 +0800 Subject: [PATCH] # --- src/main/java/com/zy/common/config/AspectConfig.java | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ pom.xml | 4 ++ 2 files changed, 112 insertions(+), 0 deletions(-) diff --git a/pom.xml b/pom.xml index 47a1583..c4eda78 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,10 @@ <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-aop</artifactId> + </dependency> + <dependency> <groupId>cn.cool</groupId> <artifactId>framework</artifactId> <version>${cool.version}</version> diff --git a/src/main/java/com/zy/common/config/AspectConfig.java b/src/main/java/com/zy/common/config/AspectConfig.java new file mode 100644 index 0000000..0e5b2c8 --- /dev/null +++ b/src/main/java/com/zy/common/config/AspectConfig.java @@ -0,0 +1,108 @@ +package com.zy.common.config; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.mapper.EntityWrapper; +import com.core.annotations.ManagerAuth; +import com.core.common.BaseRes; +import com.core.common.Cools; +import com.zy.common.utils.Http; +import com.zy.system.entity.OperateLog; +import com.zy.system.entity.User; +import com.zy.system.entity.UserLogin; +import com.zy.system.service.UserLoginService; +import com.zy.system.service.UserService; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@Component +@Aspect +@Slf4j +public class AspectConfig { + + @Autowired + private UserLoginService userLoginService; + @Autowired + private UserService userService; + + @Pointcut("execution(* com.zy.asrs.controller..*.*(..))") + private void webLog() { + } + + @Around("@within(org.springframework.web.bind.annotation.RestController)" + + "||@within(org.springframework.stereotype.Controller)") + public Object after(ProceedingJoinPoint joinPoint) throws Throwable{ + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = requestAttributes.getRequest(); + long start = System.currentTimeMillis(); + Object result = joinPoint.proceed(joinPoint.getArgs()); + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + if (method.isAnnotationPresent(ManagerAuth.class)){ + ManagerAuth annotation = method.getAnnotation(ManagerAuth.class); + if (annotation.value().equals(ManagerAuth.Auth.CHECK)){ + if (!Cools.isEmpty(annotation.memo())) { + saveLog(joinPoint, request, result); + } + } + } + + long end = System.currentTimeMillis(); + +// log.info("璇锋眰鏃ュ織鐨勬墦鍗�"); +// log.info("璇锋眰鍦板潃:{}", Optional.ofNullable(request.getRequestURI().toString()).orElse(null)); +// log.info("璇锋眰鏂瑰紡:{}",request.getMethod()); +// log.info("璇锋眰绫绘柟娉�:{}",joinPoint.getSignature()); +// log.info("璇锋眰绫绘柟娉曞弬鏁�:{}", JSONObject.toJSONString(filterArgs(joinPoint.getArgs()))); +// log.info("璇锋眰鍝嶅簲鍙傛暟{}", JSONObject.toJSONString(result)); +// log.info("鎵ц鑰楁椂:{}", end - start); + + return result; + } + + private List<Object> filterArgs(Object[] objects) { + return Arrays.stream(objects).filter(obj -> !(obj instanceof MultipartFile) + && !(obj instanceof HttpServletResponse) + && !(obj instanceof HttpServletRequest)).collect(Collectors.toList()); + } + + private void saveLog(ProceedingJoinPoint joinPoint, HttpServletRequest request, Object result) { + Long userId = 9527L; + String token = request.getHeader("token"); + UserLogin userLogin = userLoginService.selectOne(new EntityWrapper<UserLogin>().eq("token", token).eq("system", "WMS")); + if (userLogin != null) { + User user = userService.selectById(userLogin.getUserId()); + if (user != null) { + userId = user.getId(); + } + } + + // 璁板綍鎿嶄綔鏃ュ織 + OperateLog operateLog = new OperateLog(); + operateLog.setAction(request.getRequestURI()); + operateLog.setIp(request.getRemoteAddr()); + operateLog.setUserId(userId); + operateLog.setRequest(JSONObject.toJSONString(filterArgs(joinPoint.getArgs()))); + operateLog.setResponse(JSONObject.toJSONString(result)); + request.setAttribute("operateLog", operateLog); + } + +} \ No newline at end of file -- Gitblit v1.9.1