|
@@ -1,21 +1,20 @@
|
|
|
package cn.tr.plugin.operatelog.config;
|
|
package cn.tr.plugin.operatelog.config;
|
|
|
|
|
|
|
|
-import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
|
|
|
|
+import cn.hutool.core.date.DateUnit;
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.tr.core.exception.TRExcCode;
|
|
|
|
|
import cn.tr.core.pojo.CommonResult;
|
|
import cn.tr.core.pojo.CommonResult;
|
|
|
import cn.tr.core.strategy.ExceptionStrategy;
|
|
import cn.tr.core.strategy.ExceptionStrategy;
|
|
|
import cn.tr.core.strategy.LoginUserStrategy;
|
|
import cn.tr.core.strategy.LoginUserStrategy;
|
|
|
|
|
+import cn.tr.core.utils.IpUtil;
|
|
|
import cn.tr.core.utils.JsonUtils;
|
|
import cn.tr.core.utils.JsonUtils;
|
|
|
import cn.tr.core.utils.ServletUtils;
|
|
import cn.tr.core.utils.ServletUtils;
|
|
|
import cn.tr.plugin.operatelog.annotation.OperateLog;
|
|
import cn.tr.plugin.operatelog.annotation.OperateLog;
|
|
|
import cn.tr.plugin.operatelog.bo.OperateLogBO;
|
|
import cn.tr.plugin.operatelog.bo.OperateLogBO;
|
|
|
-import cn.tr.plugin.operatelog.enums.OperateTypeEnum;
|
|
|
|
|
import cn.tr.plugin.operatelog.strategy.OperateStrategy;
|
|
import cn.tr.plugin.operatelog.strategy.OperateStrategy;
|
|
|
import cn.tr.plugin.web.context.RequestContextHolder;
|
|
import cn.tr.plugin.web.context.RequestContextHolder;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
-import io.swagger.annotations.Tag;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Around;
|
|
@@ -28,9 +27,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.lang.annotation.Annotation;
|
|
|
|
|
import java.lang.reflect.Array;
|
|
import java.lang.reflect.Array;
|
|
|
-import java.time.LocalDateTime;
|
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.function.Predicate;
|
|
import java.util.function.Predicate;
|
|
|
import java.util.stream.IntStream;
|
|
import java.util.stream.IntStream;
|
|
@@ -44,15 +41,6 @@ import java.util.stream.IntStream;
|
|
|
@Aspect
|
|
@Aspect
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class OperateLogAspect {
|
|
public class OperateLogAspect {
|
|
|
-// @Around("@annotation(operation)")
|
|
|
|
|
-// public Object around(ProceedingJoinPoint joinPoint, ApiOperation operation) throws Throwable {
|
|
|
|
|
-// // 可能也添加了 @ApiOperation 注解
|
|
|
|
|
-// OperateLog operateLog = getMethodAnnotation(joinPoint,
|
|
|
|
|
-// OperateLog.class);
|
|
|
|
|
-// return around0(joinPoint, operateLog, operation);
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
-// @Around("!@annotation(io.swagger.annotations.ApiOperation) && @annotation(operateLog)")
|
|
|
|
|
@Around("@annotation(operateLog)")
|
|
@Around("@annotation(operateLog)")
|
|
|
// 兼容处理,只添加 @OperateLog 注解的情况
|
|
// 兼容处理,只添加 @OperateLog 注解的情况
|
|
|
public Object around(ProceedingJoinPoint joinPoint,
|
|
public Object around(ProceedingJoinPoint joinPoint,
|
|
@@ -64,7 +52,7 @@ public class OperateLogAspect {
|
|
|
OperateLog operateLog,
|
|
OperateLog operateLog,
|
|
|
ApiOperation apiOperation) throws Throwable {
|
|
ApiOperation apiOperation) throws Throwable {
|
|
|
// 记录开始时间
|
|
// 记录开始时间
|
|
|
- LocalDateTime startTime = LocalDateTime.now();
|
|
|
|
|
|
|
+ Date startTime = new Date();
|
|
|
try {
|
|
try {
|
|
|
// 执行原有方法
|
|
// 执行原有方法
|
|
|
Object result = joinPoint.proceed();
|
|
Object result = joinPoint.proceed();
|
|
@@ -81,7 +69,7 @@ public class OperateLogAspect {
|
|
|
private void log(ProceedingJoinPoint joinPoint,
|
|
private void log(ProceedingJoinPoint joinPoint,
|
|
|
OperateLog operateLog,
|
|
OperateLog operateLog,
|
|
|
ApiOperation apiOperation,
|
|
ApiOperation apiOperation,
|
|
|
- LocalDateTime startTime, Object result, Throwable exception) {
|
|
|
|
|
|
|
+ Date startTime, Object result, Throwable exception) {
|
|
|
try {
|
|
try {
|
|
|
// 判断不记录的情况
|
|
// 判断不记录的情况
|
|
|
if (!isLogEnable(joinPoint, operateLog)) {
|
|
if (!isLogEnable(joinPoint, operateLog)) {
|
|
@@ -96,10 +84,10 @@ public class OperateLogAspect {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void log0(ProceedingJoinPoint joinPoint, OperateLog operateLog, ApiOperation apiOperation, LocalDateTime startTime, Object result, Throwable exception) {
|
|
|
|
|
|
|
+ private void log0(ProceedingJoinPoint joinPoint, OperateLog operateLog, ApiOperation apiOperation, Date startTime, Object result, Throwable exception) {
|
|
|
OperateLogBO log = new OperateLogBO();
|
|
OperateLogBO log = new OperateLogBO();
|
|
|
// 补全通用字段
|
|
// 补全通用字段
|
|
|
- log.setStartTime(startTime);
|
|
|
|
|
|
|
+ log.setOpTime(startTime);
|
|
|
// 补充用户信息
|
|
// 补充用户信息
|
|
|
fillUserFields(log);
|
|
fillUserFields(log);
|
|
|
// 补全模块信息
|
|
// 补全模块信息
|
|
@@ -117,29 +105,27 @@ public class OperateLogAspect {
|
|
|
private static void fillMethodFields(OperateLogBO operateLogObj,
|
|
private static void fillMethodFields(OperateLogBO operateLogObj,
|
|
|
ProceedingJoinPoint joinPoint,
|
|
ProceedingJoinPoint joinPoint,
|
|
|
OperateLog operateLog,
|
|
OperateLog operateLog,
|
|
|
- LocalDateTime startTime, Object result, Throwable exception) {
|
|
|
|
|
|
|
+ Date startTime, Object result, Throwable exception) {
|
|
|
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
|
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
|
|
|
|
+
|
|
|
|
|
+ operateLogObj.setJavaClass(methodSignature.getMethod().getDeclaringClass().getCanonicalName());
|
|
|
operateLogObj.setJavaMethod(methodSignature.toString());
|
|
operateLogObj.setJavaMethod(methodSignature.toString());
|
|
|
- if (operateLog == null || operateLog.logArgs()) {
|
|
|
|
|
- operateLogObj.setJavaMethodArgs(obtainMethodArgs(joinPoint));
|
|
|
|
|
- }
|
|
|
|
|
- if (operateLog == null || operateLog.logResultData()) {
|
|
|
|
|
- operateLogObj.setResultData(obtainResultData(result));
|
|
|
|
|
- }
|
|
|
|
|
- operateLogObj.setDuration((int) (LocalDateTimeUtil.between(startTime, LocalDateTime.now()).toMillis()));
|
|
|
|
|
- // (正常)处理 resultCode 和 resultMsg 字段
|
|
|
|
|
- if (result instanceof CommonResult) {
|
|
|
|
|
- CommonResult<?> commonResult = (CommonResult<?>) result;
|
|
|
|
|
- operateLogObj.setResultCode(commonResult.getCode());
|
|
|
|
|
- operateLogObj.setResultMsg(commonResult.getErrorMsg());
|
|
|
|
|
- } else {
|
|
|
|
|
- operateLogObj.setResultCode(TRExcCode.SUCCESS.getErrCode());
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ operateLogObj.setDuration((int) (DateUtil.between(startTime,DateUtil.date(),DateUnit.MS)));
|
|
|
|
|
+ // 处理响应结果
|
|
|
|
|
+ if (result != null) {
|
|
|
|
|
+ operateLogObj.setResponseType(true);
|
|
|
|
|
+ if (operateLog == null || operateLog.logArgs()) {
|
|
|
|
|
+ operateLogObj.setResultJson(obtainMethodArgs(joinPoint));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
// (异常)处理 resultCode 和 resultMsg 字段
|
|
// (异常)处理 resultCode 和 resultMsg 字段
|
|
|
if (exception != null) {
|
|
if (exception != null) {
|
|
|
- CommonResult<?> commonResult = ExceptionStrategy.tr.exceptionHandle(ServletUtils.getRequest(),exception);
|
|
|
|
|
- operateLogObj.setResultCode(commonResult.getCode());
|
|
|
|
|
- operateLogObj.setResultMsg(commonResult.getErrorMsg());
|
|
|
|
|
|
|
+ operateLogObj.setResponseType(false);
|
|
|
|
|
+ if (operateLog == null || operateLog.logArgs()) {
|
|
|
|
|
+ operateLogObj.setResultJson(obtainResultData( ExceptionStrategy.tr.exceptionHandle(ServletUtils.getRequest(),exception)));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -200,93 +186,30 @@ public class OperateLogAspect {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
// 补全请求信息
|
|
// 补全请求信息
|
|
|
- operateLogObj.setRequestMethod(request.getMethod());
|
|
|
|
|
- operateLogObj.setRequestUrl(request.getRequestURI());
|
|
|
|
|
- operateLogObj.setUserIp(ServletUtils.getClientIP(request));
|
|
|
|
|
|
|
+ String clientIP = ServletUtils.getClientIP(request);
|
|
|
|
|
+ operateLogObj.setUserIp(clientIP);
|
|
|
|
|
+ operateLogObj.setLoginLocation(IpUtil.getCityInfo(clientIP));
|
|
|
operateLogObj.setUserAgent(ServletUtils.getUserAgent(request));
|
|
operateLogObj.setUserAgent(ServletUtils.getUserAgent(request));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void fillUserFields(OperateLogBO log) {
|
|
private void fillUserFields(OperateLogBO log) {
|
|
|
- log.setUserId(LoginUserStrategy.tr.getCurrentUserId());
|
|
|
|
|
|
|
+ log.setOpUser(LoginUserStrategy.tr.getCurrentUsername());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static void fillModuleFields(OperateLogBO operateLogObj,
|
|
private static void fillModuleFields(OperateLogBO operateLogObj,
|
|
|
ProceedingJoinPoint joinPoint,
|
|
ProceedingJoinPoint joinPoint,
|
|
|
OperateLog operateLog,
|
|
OperateLog operateLog,
|
|
|
ApiOperation apiOperation) {
|
|
ApiOperation apiOperation) {
|
|
|
- // module 属性
|
|
|
|
|
- if (operateLog != null) {
|
|
|
|
|
- operateLogObj.setModule(operateLog.module());
|
|
|
|
|
- }
|
|
|
|
|
- if (StrUtil.isEmpty(operateLogObj.getModule())) {
|
|
|
|
|
- Tag tag = getClassAnnotation(joinPoint, Tag.class);
|
|
|
|
|
- if (tag != null) {
|
|
|
|
|
- // 优先读取 @Tag 的 name 属性
|
|
|
|
|
- if (StrUtil.isNotEmpty(tag.name())) {
|
|
|
|
|
- operateLogObj.setModule(tag.name());
|
|
|
|
|
- }
|
|
|
|
|
- // 没有的话,读取 @API 的 description 属性
|
|
|
|
|
- if (StrUtil.isEmpty(operateLogObj.getModule()) && ArrayUtil.isNotEmpty(tag.description())) {
|
|
|
|
|
- operateLogObj.setModule(tag.description());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
// name 属性
|
|
// name 属性
|
|
|
if (operateLog != null) {
|
|
if (operateLog != null) {
|
|
|
- operateLogObj.setName(operateLog.name());
|
|
|
|
|
- }
|
|
|
|
|
- if (StrUtil.isEmpty(operateLogObj.getName()) && apiOperation != null) {
|
|
|
|
|
- operateLogObj.setName(apiOperation.value());
|
|
|
|
|
|
|
+ operateLogObj.setOpName(operateLog.name());
|
|
|
}
|
|
}
|
|
|
- // type 属性
|
|
|
|
|
- if (operateLog != null && ArrayUtil.isNotEmpty(operateLog.type())) {
|
|
|
|
|
- operateLogObj.setType(operateLog.type()[0].getType());
|
|
|
|
|
- }
|
|
|
|
|
- if (operateLogObj.getType() == null) {
|
|
|
|
|
- RequestMethod requestMethod = obtainFirstMatchRequestMethod(obtainRequestMethod(joinPoint));
|
|
|
|
|
- OperateTypeEnum operateLogType = convertOperateLogType(requestMethod);
|
|
|
|
|
- operateLogObj.setType(operateLogType != null ? operateLogType.getType() : null);
|
|
|
|
|
|
|
+ if (StrUtil.isEmpty(operateLogObj.getOpName()) && apiOperation != null) {
|
|
|
|
|
+ operateLogObj.setOpName(apiOperation.value());
|
|
|
}
|
|
}
|
|
|
// content
|
|
// content
|
|
|
- operateLogObj.setContent(RequestContextHolder.getRequestBody());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- private static RequestMethod obtainFirstMatchRequestMethod(RequestMethod[] requestMethods) {
|
|
|
|
|
- if (ArrayUtil.isEmpty(requestMethods)) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- // 优先,匹配最优的 POST、PUT、DELETE
|
|
|
|
|
- RequestMethod result = obtainFirstLogRequestMethod(requestMethods);
|
|
|
|
|
- if (result != null) {
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
- // 然后,匹配次优的 GET
|
|
|
|
|
- result = Arrays.stream(requestMethods).filter(requestMethod -> requestMethod == RequestMethod.GET)
|
|
|
|
|
- .findFirst().orElse(null);
|
|
|
|
|
- if (result != null) {
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
- // 兜底,获得第一个
|
|
|
|
|
- return requestMethods[0];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static OperateTypeEnum convertOperateLogType(RequestMethod requestMethod) {
|
|
|
|
|
- if (requestMethod == null) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- switch (requestMethod) {
|
|
|
|
|
- case GET:
|
|
|
|
|
- return OperateTypeEnum.GET;
|
|
|
|
|
- case POST:
|
|
|
|
|
- return OperateTypeEnum.CREATE;
|
|
|
|
|
- case PUT:
|
|
|
|
|
- return OperateTypeEnum.UPDATE;
|
|
|
|
|
- case DELETE:
|
|
|
|
|
- return OperateTypeEnum.DELETE;
|
|
|
|
|
- default:
|
|
|
|
|
- return OperateTypeEnum.OTHER;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ operateLogObj.setRequestArgs(RequestContextHolder.getRequestBody());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static boolean isLogEnable(ProceedingJoinPoint joinPoint,
|
|
private static boolean isLogEnable(ProceedingJoinPoint joinPoint,
|
|
@@ -318,16 +241,4 @@ public class OperateLogAspect {
|
|
|
((MethodSignature) joinPoint.getSignature()).getMethod(), RequestMapping.class);
|
|
((MethodSignature) joinPoint.getSignature()).getMethod(), RequestMapping.class);
|
|
|
return requestMapping != null ? requestMapping.method() : new RequestMethod[]{};
|
|
return requestMapping != null ? requestMapping.method() : new RequestMethod[]{};
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- private static <T extends Annotation> T getMethodAnnotation(ProceedingJoinPoint joinPoint, Class<T> annotationClass) {
|
|
|
|
|
- return ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(annotationClass);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @SuppressWarnings("SameParameterValue")
|
|
|
|
|
- private static <T extends Annotation> T getClassAnnotation(ProceedingJoinPoint joinPoint, Class<T> annotationClass) {
|
|
|
|
|
- return ((MethodSignature) joinPoint.getSignature()).getMethod().getDeclaringClass().getAnnotation(annotationClass);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|