1. 增加接口操作日志功能

This commit is contained in:
黄昕 2024-03-07 15:00:17 +08:00
parent e000c80476
commit 566074ff21
2 changed files with 83 additions and 5 deletions

View File

@ -14,16 +14,15 @@
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>6.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>

View File

@ -0,0 +1,79 @@
package com.cf.cs.restful.aspect;
import com.cf.cs.base.annotation.OperationLogAnnotation;
import com.cf.cs.base.common.CommonEnumHandler;
import com.cf.cs.base.common.ErrorCode;
import com.cf.cs.database.service.OperationLogDataBaseService;
import com.cf.cs.protocol.pojo.po.BaseRespStatus;
import com.cf.cs.protocol.pojo.vo.ProtocolResp;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import java.lang.reflect.Method;
/**
* The type Restful log aspect.
*
* @author xajhuang @163.com
*/
@Aspect
@Component
@Order(1)
@Slf4j
public class RestfulLogAspect {
@Resource
private OperationLogDataBaseService optLogDbService;
/**
* Restful log.
*/
@Pointcut("execution(public * com.cf.cs.restful.controller.*.*(..))")
public void restfulLog() {
}
/**
* 记录操作日志
*
* @param joinPoint 方法的执行点
* @param result 方法返回值
*/
@AfterReturning(returning = "result", value = "restfulLog()")
public void saveOperationLog(JoinPoint joinPoint, Object result) {
// 获取RequestAttributes
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {
return;
}
// 从获取RequestAttributes中获取HttpServletRequest的信息
HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
if (request == null) {
return;
}
// 从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在的方法
Method method = signature.getMethod();
//获取操作
OperationLogAnnotation annotation = method.getAnnotation(OperationLogAnnotation.class);
if(result instanceof ProtocolResp<?> r && r.getMsgContent() instanceof BaseRespStatus m) {
ErrorCode err = CommonEnumHandler.codeOf(ErrorCode.class, m.getStatus());
optLogDbService.systemOperationLog(method.getName(), request, result, annotation, err);
} else {
optLogDbService.systemOperationLog(method.getName(), request, result, annotation, ErrorCode.ERR_SYSTEMEXCEPTION);
}
}
}