108 lines
4.2 KiB
Java
108 lines
4.2 KiB
Java
package com.guwan.backend.aspect;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.guwan.backend.annotation.OperationLog;
|
|
import com.guwan.backend.entity.SysLog;
|
|
import com.guwan.backend.mapper.SysLogMapper;
|
|
import com.guwan.backend.security.CustomUserDetails;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.time.LocalDateTime;
|
|
import java.util.Arrays;
|
|
|
|
@Slf4j
|
|
@Aspect
|
|
@Component
|
|
@RequiredArgsConstructor
|
|
public class OperationLogAspect {
|
|
|
|
private final SysLogMapper sysLogMapper;
|
|
private final ObjectMapper objectMapper;
|
|
|
|
@Around("@annotation(operationLog)")
|
|
public Object around(ProceedingJoinPoint point, OperationLog operationLog) throws Throwable {
|
|
long beginTime = System.currentTimeMillis();
|
|
SysLog sysLog = new SysLog();
|
|
|
|
try {
|
|
// 执行方法
|
|
Object result = point.proceed();
|
|
// 设置状态为成功
|
|
sysLog.setStatus(1);
|
|
return result;
|
|
} catch (Exception e) {
|
|
// 记录异常信息
|
|
sysLog.setStatus(0);
|
|
sysLog.setErrorMsg(e.getMessage());
|
|
throw e;
|
|
} finally {
|
|
// 记录日志
|
|
saveLog(point, operationLog, beginTime, sysLog);
|
|
}
|
|
}
|
|
|
|
private void saveLog(ProceedingJoinPoint joinPoint, OperationLog operationLog, long beginTime, SysLog sysLog) {
|
|
try {
|
|
// 获取当前请求
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
if (attributes != null) {
|
|
HttpServletRequest request = attributes.getRequest();
|
|
sysLog.setIp(getIpAddress(request));
|
|
sysLog.setUserAgent(request.getHeader("User-Agent"));
|
|
}
|
|
|
|
// 获取当前用户信息
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails) {
|
|
CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
|
|
sysLog.setUserId(userDetails.getUserId());
|
|
sysLog.setUsername(userDetails.getUsername());
|
|
}
|
|
|
|
// 获取方法信息
|
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
Method method = signature.getMethod();
|
|
sysLog.setMethod(method.getDeclaringClass().getName() + "." + method.getName());
|
|
|
|
// 获取请求参数
|
|
String params = Arrays.toString(joinPoint.getArgs());
|
|
sysLog.setParams(params.length() > 2000 ? params.substring(0, 2000) : params);
|
|
|
|
// 设置操作信息
|
|
sysLog.setOperation(operationLog.description());
|
|
sysLog.setCreateTime(LocalDateTime.now());
|
|
sysLog.setTimeConsuming(System.currentTimeMillis() - beginTime);
|
|
|
|
// 保存日志
|
|
sysLogMapper.insert(sysLog);
|
|
} catch (Exception e) {
|
|
log.error("记录操作日志失败", e);
|
|
}
|
|
}
|
|
|
|
private String getIpAddress(HttpServletRequest request) {
|
|
String ip = request.getHeader("X-Forwarded-For");
|
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
ip = request.getHeader("Proxy-Client-IP");
|
|
}
|
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
|
}
|
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
ip = request.getRemoteAddr();
|
|
}
|
|
return ip;
|
|
}
|
|
} |