feat:
This commit is contained in:
parent
8beb75cbc8
commit
4ac2f7624d
12
pom.xml
12
pom.xml
|
@ -148,6 +148,18 @@
|
||||||
<version>2.0.53</version>
|
<version>2.0.53</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring AOP -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- AspectJ -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -8,9 +8,11 @@ import com.guwan.backend.security.CustomUserDetails;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Around;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
import org.aspectj.lang.reflect.MethodSignature;
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
@ -31,7 +33,17 @@ public class OperationLogAspect {
|
||||||
private final SysLogMapper sysLogMapper;
|
private final SysLogMapper sysLogMapper;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
@Around("@annotation(operationLog)")
|
/**
|
||||||
|
* 定义切点
|
||||||
|
*/
|
||||||
|
@Pointcut("@annotation(com.guwan.backend.annotation.OperationLog)")
|
||||||
|
public void logPointcut() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 环绕通知
|
||||||
|
*/
|
||||||
|
@Around("logPointcut() && @annotation(operationLog)")
|
||||||
public Object around(ProceedingJoinPoint point, OperationLog operationLog) throws Throwable {
|
public Object around(ProceedingJoinPoint point, OperationLog operationLog) throws Throwable {
|
||||||
long beginTime = System.currentTimeMillis();
|
long beginTime = System.currentTimeMillis();
|
||||||
SysLog sysLog = new SysLog();
|
SysLog sysLog = new SysLog();
|
||||||
|
@ -48,13 +60,19 @@ public class OperationLogAspect {
|
||||||
sysLog.setErrorMsg(e.getMessage());
|
sysLog.setErrorMsg(e.getMessage());
|
||||||
throw e;
|
throw e;
|
||||||
} finally {
|
} finally {
|
||||||
|
try {
|
||||||
// 记录日志
|
// 记录日志
|
||||||
saveLog(point, operationLog, beginTime, sysLog);
|
saveLog(point, operationLog, beginTime, sysLog);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("记录操作日志失败", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存日志
|
||||||
|
*/
|
||||||
private void saveLog(ProceedingJoinPoint joinPoint, OperationLog operationLog, long beginTime, SysLog sysLog) {
|
private void saveLog(ProceedingJoinPoint joinPoint, OperationLog operationLog, long beginTime, SysLog sysLog) {
|
||||||
try {
|
|
||||||
// 获取当前请求
|
// 获取当前请求
|
||||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
if (attributes != null) {
|
if (attributes != null) {
|
||||||
|
@ -74,11 +92,18 @@ public class OperationLogAspect {
|
||||||
// 获取方法信息
|
// 获取方法信息
|
||||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||||
Method method = signature.getMethod();
|
Method method = signature.getMethod();
|
||||||
sysLog.setMethod(method.getDeclaringClass().getName() + "." + method.getName());
|
String className = method.getDeclaringClass().getName();
|
||||||
|
String methodName = method.getName();
|
||||||
|
sysLog.setMethod(className + "." + methodName);
|
||||||
|
|
||||||
// 获取请求参数
|
// 获取请求参数
|
||||||
String params = Arrays.toString(joinPoint.getArgs());
|
try {
|
||||||
|
String params = objectMapper.writeValueAsString(joinPoint.getArgs());
|
||||||
sysLog.setParams(params.length() > 2000 ? params.substring(0, 2000) : params);
|
sysLog.setParams(params.length() > 2000 ? params.substring(0, 2000) : params);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("序列化请求参数失败", e);
|
||||||
|
sysLog.setParams(Arrays.toString(joinPoint.getArgs()));
|
||||||
|
}
|
||||||
|
|
||||||
// 设置操作信息
|
// 设置操作信息
|
||||||
sysLog.setOperation(operationLog.description());
|
sysLog.setOperation(operationLog.description());
|
||||||
|
@ -87,11 +112,11 @@ public class OperationLogAspect {
|
||||||
|
|
||||||
// 保存日志
|
// 保存日志
|
||||||
sysLogMapper.insert(sysLog);
|
sysLogMapper.insert(sysLog);
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("记录操作日志失败", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取IP地址
|
||||||
|
*/
|
||||||
private String getIpAddress(HttpServletRequest request) {
|
private String getIpAddress(HttpServletRequest request) {
|
||||||
String ip = request.getHeader("X-Forwarded-For");
|
String ip = request.getHeader("X-Forwarded-For");
|
||||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||||
|
@ -103,6 +128,10 @@ public class OperationLogAspect {
|
||||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||||
ip = request.getRemoteAddr();
|
ip = request.getRemoteAddr();
|
||||||
}
|
}
|
||||||
|
// 多个代理的情况,第一个IP为客户端真实IP
|
||||||
|
if (ip != null && ip.contains(",")) {
|
||||||
|
ip = ip.split(",")[0].trim();
|
||||||
|
}
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue