From c3670d7cbe6c70fe363d6b985a2be1705519a5ea Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期三, 31 一月 2024 08:35:08 +0800
Subject: [PATCH] #
---
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/entity/OperationRecord.java | 9 ++
zy-asrs-wcs/pom.xml | 5 +
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/aspect/OperationLogAspect.java | 184 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 197 insertions(+), 1 deletions(-)
diff --git a/zy-asrs-wcs/pom.xml b/zy-asrs-wcs/pom.xml
index 986a53d..d927918 100644
--- a/zy-asrs-wcs/pom.xml
+++ b/zy-asrs-wcs/pom.xml
@@ -56,6 +56,11 @@
<version>3.10.0</version>
</dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-aspects</artifactId>
+ <version>5.2.5.RELEASE</version>
+ </dependency>
</dependencies>
<build>
diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/aspect/OperationLogAspect.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/aspect/OperationLogAspect.java
new file mode 100644
index 0000000..8af863f
--- /dev/null
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/aspect/OperationLogAspect.java
@@ -0,0 +1,184 @@
+package com.zy.asrs.wcs.common.aspect;
+
+import com.alibaba.fastjson.JSON;
+import com.zy.asrs.common.utils.IpTools;
+import com.zy.asrs.framework.common.Cools;
+import com.zy.asrs.wcs.common.annotation.OperationLog;
+import com.zy.asrs.wcs.sys.entity.OperationRecord;
+import com.zy.asrs.wcs.sys.entity.User;
+import com.zy.asrs.wcs.sys.service.OperationRecordService;
+import com.zy.asrs.wcs.utils.Utils;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.*;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+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.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 鎿嶄綔鏃ュ織璁板綍
+ *
+ * @author vincent
+ * @since 2020-03-21 16:58:16:05
+ */
+@Aspect
+@Component
+public class OperationLogAspect {
+
+ @Resource
+ private OperationRecordService operationRecordService;
+ // 鍙傛暟銆佽繑鍥炵粨鏋溿�侀敊璇俊鎭瓑鏈�澶т繚瀛橀暱搴�
+ private static final int MAX_LENGTH = 1000;
+ // 鐢ㄤ簬璁板綍璇锋眰鑰楁椂
+ private final ThreadLocal<Long> startTime = new ThreadLocal<>();
+
+ @Pointcut("@annotation(com.zy.asrs.wcs.common.annotation.OperationLog)")
+ public void operationLog() {
+ }
+
+ @Before("operationLog()")
+ public void doBefore(JoinPoint joinPoint) throws Throwable {
+ startTime.set(System.currentTimeMillis());
+ }
+
+ @AfterReturning(pointcut = "operationLog()", returning = "result")
+ public void doAfterReturning(JoinPoint joinPoint, Object result) {
+ saveLog(joinPoint, result, null);
+ }
+
+ @AfterThrowing(value = "operationLog()", throwing = "e")
+ public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
+ saveLog(joinPoint, null, e);
+ }
+
+ /**
+ * 淇濆瓨鎿嶄綔璁板綍
+ */
+ private void saveLog(JoinPoint joinPoint, Object result, Exception e) {
+ OperationRecord record = new OperationRecord();
+ Long endTime = startTime.get();
+ // 璁板綍鎿嶄綔鑰楁椂
+ if (endTime != null) {
+ record.setSpendTime((int) (System.currentTimeMillis() - endTime));
+ }
+ record.setTimestamp(String.valueOf(endTime));
+ // 璁板綍褰撳墠鐧诲綍鐢ㄦ埛id銆佺鎴穒d
+ User user = getLoginUser();
+ if (user != null) {
+ record.setUserId(user.getId());
+ record.setHostId(user.getHostId());
+ }
+ // 璁板綍璇锋眰鍦板潃銆佽姹傛柟寮忋�乮p
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+ HttpServletRequest request = (attributes == null ? null : attributes.getRequest());
+ if (request != null) {
+ record.setUrl(request.getRequestURI());
+ record.setClientIp(IpTools.gainRealIp(request));
+ }
+ // 璁板綍寮傚父淇℃伅
+ if (e != null) {
+ record.setResult(0);
+ record.setErr(Utils.sub(e.toString(), MAX_LENGTH));
+ } else {
+ record.setResult(1);
+ }
+ // 璁板綍妯″潡鍚嶃�佹搷浣滃姛鑳姐�佽姹傛柟娉曘�佽姹傚弬鏁般�佽繑鍥炵粨鏋�
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
+ Method method = signature.getMethod();
+ if (method != null) {
+ OperationLog ol = method.getAnnotation(OperationLog.class);
+ if (ol != null) {
+ // 璁板綍鎿嶄綔鍔熻兘
+ record.setNamespace(getDescription(method, ol));
+ // 璁板綍澶囨敞
+ if (!Cools.isEmpty(ol.comments())) {
+ record.setMemo(ol.comments());
+ }
+ // 璁板綍璇锋眰鍙傛暟
+ if (ol.param() && request != null) {
+ record.setRequest(Utils.sub(getParams(joinPoint, request), MAX_LENGTH));
+ }
+ // 璁板綍璇锋眰缁撴灉
+ if (ol.result() && result != null) {
+ record.setResponse(Utils.sub(JSON.toJSONString(result), MAX_LENGTH));
+ }
+ }
+ }
+ operationRecordService.save(record);
+ }
+
+ /**
+ * 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛
+ */
+ private User getLoginUser() {
+ Authentication subject = SecurityContextHolder.getContext().getAuthentication();
+ if (subject != null) {
+ Object object = subject.getPrincipal();
+ if (object instanceof User) {
+ return (User) object;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * 鑾峰彇璇锋眰鍙傛暟
+ *
+ * @param joinPoint JoinPoint
+ * @param request HttpServletRequest
+ * @return String
+ */
+ private String getParams(JoinPoint joinPoint, HttpServletRequest request) {
+ String params;
+
+ Map<String, String> paramsMap = new HashMap<>();
+
+ Map<String, String[]> map = Collections.unmodifiableMap(request.getParameterMap());
+ for (Map.Entry<String, String[]> entry : map.entrySet()) {
+ paramsMap.put(entry.getKey(), Utils.join(entry.getValue(), ","));
+ }
+
+ if (paramsMap.keySet().size() > 0) {
+ params = JSON.toJSONString(paramsMap);
+ } else {
+ StringBuilder sb = new StringBuilder();
+ for (Object arg : joinPoint.getArgs()) {
+ if (null == arg
+ || arg instanceof MultipartFile
+ || arg instanceof HttpServletRequest
+ || arg instanceof HttpServletResponse) {
+ continue;
+ }
+ sb.append(JSON.toJSONString(arg)).append(" ");
+ }
+ params = sb.toString();
+ }
+ return params;
+ }
+
+ /**
+ * 鑾峰彇鎿嶄綔鍔熻兘
+ *
+ * @param method Method
+ * @param ol OperationLog
+ * @return String
+ */
+ private String getDescription(Method method, OperationLog ol) {
+ if (!Cools.isEmpty(ol.value())) {
+ return ol.value();
+ }
+ return null;
+ }
+
+}
diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/entity/OperationRecord.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/entity/OperationRecord.java
index 5fc8064..6edc8ce 100644
--- a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/entity/OperationRecord.java
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/entity/OperationRecord.java
@@ -71,6 +71,12 @@
private String response;
/**
+ * 娑堣�楁椂闂�
+ */
+ @ApiModelProperty(value= "娑堣�楁椂闂�")
+ private Integer spendTime;
+
+ /**
* 寮傚父鍐呭
*/
@ApiModelProperty(value= "寮傚父鍐呭")
@@ -109,7 +115,7 @@
public OperationRecord() {}
- public OperationRecord(String namespace,String url,String appkey,String timestamp,String clientIp,String request,String response,String err,Integer result,Long userId,Long hostId,Date createTime,String memo) {
+ public OperationRecord(String namespace, String url, String appkey, String timestamp, String clientIp, String request, String response, Integer spendTime, String err, Integer result, Long userId, Long hostId, Date createTime, String memo) {
this.namespace = namespace;
this.url = url;
this.appkey = appkey;
@@ -117,6 +123,7 @@
this.clientIp = clientIp;
this.request = request;
this.response = response;
+ this.spendTime = spendTime;
this.err = err;
this.result = result;
this.userId = userId;
--
Gitblit v1.9.1