From 4092013da555e0f2f3588cf717a721621b2dfddb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?=
<15040126243@163.com>
Date: Thu, 8 Dec 2022 09:49:39 +0800
Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E4=BD=BF?=
=?UTF-8?q?=E7=94=A8spring=E4=BA=8B=E4=BB=B6=E5=8F=91=E5=B8=83=E6=9C=BA?=
=?UTF-8?q?=E5=88=B6=20=E9=87=8D=E6=9E=84=E7=99=BB=E5=BD=95=E6=97=A5?=
=?UTF-8?q?=E5=BF=97=E4=B8=8E=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ruoyi-auth/pom.xml | 6 +
.../ruoyi/auth/service/SysLoginService.java | 7 +-
.../ruoyi/common/core/config/AsyncConfig.java | 2 +-
.../ruoyi/common/log/aspect/LogAspect.java | 18 ++-
.../common/log/event/LogEventListener.java | 40 +++++++
.../common/log/event/LogininforEvent.java | 38 +++++++
.../ruoyi/common/log/event/OperLogEvent.java | 104 ++++++++++++++++++
.../common/log/service/AsyncLogService.java | 27 -----
...ot.autoconfigure.AutoConfiguration.imports | 2 +-
9 files changed, 201 insertions(+), 43 deletions(-)
create mode 100644 ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/LogEventListener.java
create mode 100644 ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/LogininforEvent.java
create mode 100644 ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/OperLogEvent.java
delete mode 100644 ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/service/AsyncLogService.java
diff --git a/ruoyi-auth/pom.xml b/ruoyi-auth/pom.xml
index 19d1e3d8..5d8ae913 100644
--- a/ruoyi-auth/pom.xml
+++ b/ruoyi-auth/pom.xml
@@ -45,6 +45,12 @@
ruoyi-common-security
+
+
+ com.ruoyi
+ ruoyi-common-log
+
+
com.ruoyi
ruoyi-common-doc
diff --git a/ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java b/ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java
index 46e7390a..1d1a7d85 100644
--- a/ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java
+++ b/ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java
@@ -15,12 +15,13 @@ import com.ruoyi.common.core.exception.user.CaptchaExpireException;
import com.ruoyi.common.core.exception.user.UserException;
import com.ruoyi.common.core.utils.MessageUtils;
import com.ruoyi.common.core.utils.ServletUtils;
+import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
+import com.ruoyi.common.log.event.LogininforEvent;
import com.ruoyi.common.redis.utils.RedisUtils;
import com.ruoyi.common.satoken.utils.LoginHelper;
import com.ruoyi.system.api.RemoteLogService;
import com.ruoyi.system.api.RemoteUserService;
-import com.ruoyi.system.api.domain.SysLogininfor;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.api.model.XcxLoginUser;
@@ -128,7 +129,7 @@ public class SysLoginService {
* @return
*/
public void recordLogininfor(String username, String status, String message) {
- SysLogininfor logininfor = new SysLogininfor();
+ LogininforEvent logininfor = new LogininforEvent();
logininfor.setUserName(username);
logininfor.setIpaddr(ServletUtils.getClientIP());
logininfor.setMsg(message);
@@ -138,7 +139,7 @@ public class SysLoginService {
} else if (Constants.LOGIN_FAIL.equals(status)) {
logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
}
- remoteLogService.saveLogininfor(logininfor);
+ SpringUtils.context().publishEvent(logininfor);
}
/**
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/AsyncConfig.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/AsyncConfig.java
index 75a3cb05..f6c2e82e 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/AsyncConfig.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/AsyncConfig.java
@@ -20,7 +20,7 @@ import java.util.concurrent.*;
* @author Lion Li
*/
@Slf4j
-@EnableAsync
+@EnableAsync(proxyTargetClass = true)
@AutoConfiguration
public class AsyncConfig extends AsyncConfigurerSupport {
diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java
index 8661e5fd..8cf7a657 100644
--- a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java
+++ b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java
@@ -5,18 +5,17 @@ import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.utils.JsonUtils;
import com.ruoyi.common.core.utils.ServletUtils;
+import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessStatus;
-import com.ruoyi.common.log.service.AsyncLogService;
+import com.ruoyi.common.log.event.OperLogEvent;
import com.ruoyi.common.satoken.utils.LoginHelper;
-import com.ruoyi.system.api.domain.SysOperLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.http.HttpMethod;
import org.springframework.validation.BindingResult;
@@ -42,9 +41,6 @@ public class LogAspect {
*/
public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" };
- @Autowired
- private AsyncLogService asyncLogService;
-
/**
* 处理完请求后执行
*
@@ -69,7 +65,7 @@ public class LogAspect {
protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) {
try {
// *========数据库日志=========*//
- SysOperLog operLog = new SysOperLog();
+ OperLogEvent operLog = new OperLogEvent();
operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
// 请求的地址
operLog.setOperIp(ServletUtils.getClientIP());
@@ -91,8 +87,8 @@ public class LogAspect {
operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
// 处理设置注解上的参数
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
- // 保存数据库
- asyncLogService.saveSysLog(operLog);
+ // 发布事件保存数据库
+ SpringUtils.context().publishEvent(operLog);
} catch (Exception exp) {
// 记录本地异常日志
log.error("异常信息:{}", exp.getMessage());
@@ -107,7 +103,7 @@ public class LogAspect {
* @param operLog 操作日志
* @throws Exception
*/
- public void getControllerMethodDescription(JoinPoint joinPoint, Log log, SysOperLog operLog, Object jsonResult) throws Exception {
+ public void getControllerMethodDescription(JoinPoint joinPoint, Log log, OperLogEvent operLog, Object jsonResult) throws Exception {
// 设置action动作
operLog.setBusinessType(log.businessType().ordinal());
// 设置标题
@@ -131,7 +127,7 @@ public class LogAspect {
* @param operLog 操作日志
* @throws Exception 异常
*/
- private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog) throws Exception {
+ private void setRequestValue(JoinPoint joinPoint, OperLogEvent operLog) throws Exception {
String requestMethod = operLog.getRequestMethod();
if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
String params = argsArrayToString(joinPoint.getArgs());
diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/LogEventListener.java b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/LogEventListener.java
new file mode 100644
index 00000000..324cecd4
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/LogEventListener.java
@@ -0,0 +1,40 @@
+package com.ruoyi.common.log.event;
+
+import com.ruoyi.common.core.utils.BeanCopyUtils;
+import com.ruoyi.system.api.RemoteLogService;
+import com.ruoyi.system.api.domain.SysLogininfor;
+import com.ruoyi.system.api.domain.SysOperLog;
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Component;
+
+/**
+ * 异步调用日志服务
+ *
+ * @author ruoyi
+ */
+@Component
+public class LogEventListener {
+
+ @DubboReference
+ private RemoteLogService remoteLogService;
+
+ /**
+ * 保存系统日志记录
+ */
+ @Async
+ @EventListener
+ public void saveLog(OperLogEvent operLogEvent) {
+ SysOperLog sysOperLog = BeanCopyUtils.copy(operLogEvent, SysOperLog.class);
+ remoteLogService.saveLog(sysOperLog);
+ }
+
+ @Async
+ @EventListener
+ public void saveLogininfor(LogininforEvent logininforEvent) {
+ SysLogininfor sysLogininfor = BeanCopyUtils.copy(logininforEvent, SysLogininfor.class);
+ remoteLogService.saveLogininfor(sysLogininfor);
+ }
+
+}
diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/LogininforEvent.java b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/LogininforEvent.java
new file mode 100644
index 00000000..8f96e10d
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/LogininforEvent.java
@@ -0,0 +1,38 @@
+package com.ruoyi.common.log.event;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * 登录事件
+ *
+ * @author Lion Li
+ */
+
+@Data
+public class LogininforEvent implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * 用户账号
+ */
+ private String userName;
+
+ /**
+ * 登录状态 0成功 1失败
+ */
+ private String status;
+
+ /**
+ * ip地址
+ */
+ private String ipaddr;
+
+ /**
+ * 提示消息
+ */
+ private String msg;
+
+}
diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/OperLogEvent.java b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/OperLogEvent.java
new file mode 100644
index 00000000..2eceebea
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/event/OperLogEvent.java
@@ -0,0 +1,104 @@
+package com.ruoyi.common.log.event;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * 操作日志事件
+ *
+ * @author Lion Li
+ */
+
+@Data
+public class OperLogEvent implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * 日志主键
+ */
+ private Long operId;
+
+ /**
+ * 操作模块
+ */
+ private String title;
+
+ /**
+ * 业务类型(0其它 1新增 2修改 3删除)
+ */
+ private Integer businessType;
+
+ /**
+ * 业务类型数组
+ */
+ private Integer[] businessTypes;
+
+ /**
+ * 请求方法
+ */
+ private String method;
+
+ /**
+ * 请求方式
+ */
+ private String requestMethod;
+
+ /**
+ * 操作类别(0其它 1后台用户 2手机端用户)
+ */
+ private Integer operatorType;
+
+ /**
+ * 操作人员
+ */
+ private String operName;
+
+ /**
+ * 部门名称
+ */
+ private String deptName;
+
+ /**
+ * 请求url
+ */
+ private String operUrl;
+
+ /**
+ * 操作地址
+ */
+ private String operIp;
+
+ /**
+ * 操作地点
+ */
+ private String operLocation;
+
+ /**
+ * 请求参数
+ */
+ private String operParam;
+
+ /**
+ * 返回参数
+ */
+ private String jsonResult;
+
+ /**
+ * 操作状态(0正常 1异常)
+ */
+ private Integer status;
+
+ /**
+ * 错误消息
+ */
+ private String errorMsg;
+
+ /**
+ * 操作时间
+ */
+ private Date operTime;
+
+}
diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/service/AsyncLogService.java b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/service/AsyncLogService.java
deleted file mode 100644
index 972398b5..00000000
--- a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/service/AsyncLogService.java
+++ /dev/null
@@ -1,27 +0,0 @@
-package com.ruoyi.common.log.service;
-
-import com.ruoyi.system.api.RemoteLogService;
-import com.ruoyi.system.api.domain.SysOperLog;
-import org.apache.dubbo.config.annotation.DubboReference;
-import org.springframework.scheduling.annotation.Async;
-import org.springframework.stereotype.Service;
-
-/**
- * 异步调用日志服务
- *
- * @author ruoyi
- */
-@Service
-public class AsyncLogService {
-
- @DubboReference
- private RemoteLogService remoteLogService;
-
- /**
- * 保存系统日志记录
- */
- @Async
- public void saveSysLog(SysOperLog sysOperLog) {
- remoteLogService.saveLog(sysOperLog);
- }
-}
diff --git a/ruoyi-common/ruoyi-common-log/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/ruoyi-common/ruoyi-common-log/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
index ee96a679..8f602369 100644
--- a/ruoyi-common/ruoyi-common-log/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
+++ b/ruoyi-common/ruoyi-common-log/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -1,2 +1,2 @@
-com.ruoyi.common.log.service.AsyncLogService
+com.ruoyi.common.log.event.LogEventListener
com.ruoyi.common.log.aspect.LogAspect