mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-09-27 06:01:46 +00:00
update 优化 使用spring事件发布机制 重构登录日志与操作日志
This commit is contained in:
@@ -45,6 +45,12 @@
|
|||||||
<artifactId>ruoyi-common-security</artifactId>
|
<artifactId>ruoyi-common-security</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- RuoYi Common Log -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common-log</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
<artifactId>ruoyi-common-doc</artifactId>
|
<artifactId>ruoyi-common-doc</artifactId>
|
||||||
|
@@ -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.exception.user.UserException;
|
||||||
import com.ruoyi.common.core.utils.MessageUtils;
|
import com.ruoyi.common.core.utils.MessageUtils;
|
||||||
import com.ruoyi.common.core.utils.ServletUtils;
|
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.core.utils.StringUtils;
|
||||||
|
import com.ruoyi.common.log.event.LogininforEvent;
|
||||||
import com.ruoyi.common.redis.utils.RedisUtils;
|
import com.ruoyi.common.redis.utils.RedisUtils;
|
||||||
import com.ruoyi.common.satoken.utils.LoginHelper;
|
import com.ruoyi.common.satoken.utils.LoginHelper;
|
||||||
import com.ruoyi.system.api.RemoteLogService;
|
import com.ruoyi.system.api.RemoteLogService;
|
||||||
import com.ruoyi.system.api.RemoteUserService;
|
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.domain.SysUser;
|
||||||
import com.ruoyi.system.api.model.LoginUser;
|
import com.ruoyi.system.api.model.LoginUser;
|
||||||
import com.ruoyi.system.api.model.XcxLoginUser;
|
import com.ruoyi.system.api.model.XcxLoginUser;
|
||||||
@@ -128,7 +129,7 @@ public class SysLoginService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public void recordLogininfor(String username, String status, String message) {
|
public void recordLogininfor(String username, String status, String message) {
|
||||||
SysLogininfor logininfor = new SysLogininfor();
|
LogininforEvent logininfor = new LogininforEvent();
|
||||||
logininfor.setUserName(username);
|
logininfor.setUserName(username);
|
||||||
logininfor.setIpaddr(ServletUtils.getClientIP());
|
logininfor.setIpaddr(ServletUtils.getClientIP());
|
||||||
logininfor.setMsg(message);
|
logininfor.setMsg(message);
|
||||||
@@ -138,7 +139,7 @@ public class SysLoginService {
|
|||||||
} else if (Constants.LOGIN_FAIL.equals(status)) {
|
} else if (Constants.LOGIN_FAIL.equals(status)) {
|
||||||
logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
|
logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
|
||||||
}
|
}
|
||||||
remoteLogService.saveLogininfor(logininfor);
|
SpringUtils.context().publishEvent(logininfor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -20,7 +20,7 @@ import java.util.concurrent.*;
|
|||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@EnableAsync
|
@EnableAsync(proxyTargetClass = true)
|
||||||
@AutoConfiguration
|
@AutoConfiguration
|
||||||
public class AsyncConfig extends AsyncConfigurerSupport {
|
public class AsyncConfig extends AsyncConfigurerSupport {
|
||||||
|
|
||||||
|
@@ -5,18 +5,17 @@ import cn.hutool.core.map.MapUtil;
|
|||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.ruoyi.common.core.utils.JsonUtils;
|
import com.ruoyi.common.core.utils.JsonUtils;
|
||||||
import com.ruoyi.common.core.utils.ServletUtils;
|
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.core.utils.StringUtils;
|
||||||
import com.ruoyi.common.log.annotation.Log;
|
import com.ruoyi.common.log.annotation.Log;
|
||||||
import com.ruoyi.common.log.enums.BusinessStatus;
|
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.common.satoken.utils.LoginHelper;
|
||||||
import com.ruoyi.system.api.domain.SysOperLog;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.AfterReturning;
|
import org.aspectj.lang.annotation.AfterReturning;
|
||||||
import org.aspectj.lang.annotation.AfterThrowing;
|
import org.aspectj.lang.annotation.AfterThrowing;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
@@ -42,9 +41,6 @@ public class LogAspect {
|
|||||||
*/
|
*/
|
||||||
public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" };
|
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) {
|
protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) {
|
||||||
try {
|
try {
|
||||||
// *========数据库日志=========*//
|
// *========数据库日志=========*//
|
||||||
SysOperLog operLog = new SysOperLog();
|
OperLogEvent operLog = new OperLogEvent();
|
||||||
operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
|
operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
|
||||||
// 请求的地址
|
// 请求的地址
|
||||||
operLog.setOperIp(ServletUtils.getClientIP());
|
operLog.setOperIp(ServletUtils.getClientIP());
|
||||||
@@ -91,8 +87,8 @@ public class LogAspect {
|
|||||||
operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
|
operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
|
||||||
// 处理设置注解上的参数
|
// 处理设置注解上的参数
|
||||||
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
|
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
|
||||||
// 保存数据库
|
// 发布事件保存数据库
|
||||||
asyncLogService.saveSysLog(operLog);
|
SpringUtils.context().publishEvent(operLog);
|
||||||
} catch (Exception exp) {
|
} catch (Exception exp) {
|
||||||
// 记录本地异常日志
|
// 记录本地异常日志
|
||||||
log.error("异常信息:{}", exp.getMessage());
|
log.error("异常信息:{}", exp.getMessage());
|
||||||
@@ -107,7 +103,7 @@ public class LogAspect {
|
|||||||
* @param operLog 操作日志
|
* @param operLog 操作日志
|
||||||
* @throws Exception
|
* @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动作
|
// 设置action动作
|
||||||
operLog.setBusinessType(log.businessType().ordinal());
|
operLog.setBusinessType(log.businessType().ordinal());
|
||||||
// 设置标题
|
// 设置标题
|
||||||
@@ -131,7 +127,7 @@ public class LogAspect {
|
|||||||
* @param operLog 操作日志
|
* @param operLog 操作日志
|
||||||
* @throws Exception 异常
|
* @throws Exception 异常
|
||||||
*/
|
*/
|
||||||
private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog) throws Exception {
|
private void setRequestValue(JoinPoint joinPoint, OperLogEvent operLog) throws Exception {
|
||||||
String requestMethod = operLog.getRequestMethod();
|
String requestMethod = operLog.getRequestMethod();
|
||||||
if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
|
if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
|
||||||
String params = argsArrayToString(joinPoint.getArgs());
|
String params = argsArrayToString(joinPoint.getArgs());
|
||||||
|
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -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;
|
||||||
|
|
||||||
|
}
|
@@ -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;
|
||||||
|
|
||||||
|
}
|
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,2 +1,2 @@
|
|||||||
com.ruoyi.common.log.service.AsyncLogService
|
com.ruoyi.common.log.event.LogEventListener
|
||||||
com.ruoyi.common.log.aspect.LogAspect
|
com.ruoyi.common.log.aspect.LogAspect
|
||||||
|
Reference in New Issue
Block a user