This commit is contained in:
ls
2024-12-02 13:49:01 +08:00
parent 51a27be4a1
commit aff2f9008e
5 changed files with 66 additions and 72 deletions

View File

@@ -9,20 +9,15 @@ import jakarta.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
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.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.jeecg.common.api.dto.LogDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.enums.ModuleType;
import org.jeecg.common.constant.enums.OperateTypeEnum;
import org.jeecg.common.constant.enums.*;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.IpUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.common.util.*;
import org.jeecg.modules.base.service.BaseCommonService;
import org.springframework.core.StandardReflectionParameterNameDiscoverer;
import org.springframework.stereotype.Component;
@@ -32,7 +27,6 @@ import org.springframework.web.multipart.MultipartFile;
import java.lang.reflect.Method;
import java.util.Date;
/**
* 系统日志,切面处理类
*
@@ -88,7 +82,6 @@ public class AutoLogAspect {
String methodName = signature.getName();
dto.setMethod(className + "." + methodName + "()");
//设置操作类型
if (CommonConstant.LOG_TYPE_2 == dto.getLogType()) {
dto.setOperateType(getOperateType(methodName, syslog.operateType()));
@@ -110,11 +103,20 @@ public class AutoLogAspect {
//耗时
dto.setCostTime(time);
dto.setCreateTime(new Date());
try {
//设置客户端
if (BrowserUtils.isDesktop(request)) {
dto.setClientType(ClientTerminalTypeEnum.PC.getKey());
} else {
dto.setClientType(ClientTerminalTypeEnum.APP.getKey());
}
} catch (Exception e) {
//e.printStackTrace();
}
//保存系统日志
baseCommonService.addLog(dto);
}
/**
* 获取操作类型
*/
@@ -138,15 +140,20 @@ public class AutoLogAspect {
private String getReqestParams(HttpServletRequest request, JoinPoint joinPoint) {
String httpMethod = request.getMethod();
String params = "";
if (CommonConstant.HTTP_POST.equals(httpMethod) || CommonConstant.HTTP_PUT.equals(httpMethod) || CommonConstant.HTTP_PATCH.equals(httpMethod)) {
if (CommonConstant.HTTP_POST.equals(httpMethod) || CommonConstant.HTTP_PUT.equals(httpMethod) || CommonConstant.HTTP_PATCH.equals(
httpMethod)) {
Object[] paramsArray = joinPoint.getArgs();
// java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
// java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e
// . isAsyncStarted() returns false)
// https://my.oschina.net/mengzhang6/blog/2395893
Object[] arguments = new Object[paramsArray.length];
for (int i = 0; i < paramsArray.length; i++) {
if (paramsArray[i] instanceof BindingResult || paramsArray[i] instanceof ServletRequest || paramsArray[i] instanceof ServletResponse || paramsArray[i] instanceof MultipartFile) {
//ServletRequest不能序列化从入参里排除否则报异常java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
//ServletResponse不能序列化 从入参里排除否则报异常java.lang.IllegalStateException: getOutputStream() has already been called for this response
if (paramsArray[i] instanceof BindingResult || paramsArray[i] instanceof ServletRequest
|| paramsArray[i] instanceof ServletResponse || paramsArray[i] instanceof MultipartFile) {
//ServletRequest不能序列化从入参里排除否则报异常java.lang.IllegalStateException: It is illegal to call this method if the current
// request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
//ServletResponse不能序列化 从入参里排除否则报异常java.lang.IllegalStateException: getOutputStream() has already been called for
// this response
continue;
}
arguments[i] = paramsArray[i];

View File

@@ -3,6 +3,8 @@ package org.jeecg.modules.base.service;
import org.jeecg.common.api.dto.LogDTO;
import org.jeecg.common.system.vo.LoginUser;
import java.util.Date;
/**
* common接口
*
@@ -25,7 +27,7 @@ public interface BaseCommonService {
* @param operateType
* @param user
*/
void addLog(String logContent, Integer logType, Integer operateType, LoginUser user);
void addLog(String logContent, Integer logType, Integer operateType, LoginUser user, Date beginTime);
/**
* 保存日志

View File

@@ -8,10 +8,7 @@ import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.LogDTO;
import org.jeecg.common.constant.enums.ClientTerminalTypeEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.BrowserUtils;
import org.jeecg.common.util.IpUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.common.util.*;
import org.jeecg.modules.base.mapper.BaseCommonMapper;
import org.jeecg.modules.base.service.BaseCommonService;
import org.springframework.stereotype.Service;
@@ -45,7 +42,7 @@ public class BaseCommonServiceImpl implements BaseCommonService {
}
@Override
public void addLog(String logContent, Integer logType, Integer operatetype, LoginUser user) {
public void addLog(String logContent, Integer logType, Integer operatetype, LoginUser user, Date beginTime) {
LogDTO sysLog = new LogDTO();
sysLog.setId(String.valueOf(IdWorker.getId()));
//注解上的描述,操作日志内容
@@ -84,6 +81,7 @@ public class BaseCommonServiceImpl implements BaseCommonService {
sysLog.setUsername(user.getRealname());
}
sysLog.setCreateTime(new Date());
sysLog.setCostTime(System.currentTimeMillis() - beginTime.getTime());
//保存日志异常捕获处理防止数据太大存储失败导致业务失败JT-238
try {
baseCommonMapper.saveLog(sysLog);
@@ -95,8 +93,7 @@ public class BaseCommonServiceImpl implements BaseCommonService {
@Override
public void addLog(String logContent, Integer logType, Integer operateType) {
addLog(logContent, logType, operateType, null);
addLog(logContent, logType, operateType, null, new Date());
}
}