update 优化 使用spring事件发布机制 重构登录日志与操作日志

2.X
疯狂的狮子li 2 years ago
parent fd83d6bc0a
commit 4092013da5

@ -45,6 +45,12 @@
<artifactId>ruoyi-common-security</artifactId>
</dependency>
<!-- RuoYi Common Log -->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-log</artifactId>
</dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<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.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);
}
/**

@ -20,7 +20,7 @@ import java.util.concurrent.*;
* @author Lion Li
*/
@Slf4j
@EnableAsync
@EnableAsync(proxyTargetClass = true)
@AutoConfiguration
public class AsyncConfig extends AsyncConfigurerSupport {

@ -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());

@ -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

Loading…
Cancel
Save