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>
|
||||
</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>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -8,9 +8,11 @@ import com.guwan.backend.security.CustomUserDetails;
|
|||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.reflect.MethodSignature;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
@ -31,7 +33,17 @@ public class OperationLogAspect {
|
|||
private final SysLogMapper sysLogMapper;
|
||||
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 {
|
||||
long beginTime = System.currentTimeMillis();
|
||||
SysLog sysLog = new SysLog();
|
||||
|
@ -48,50 +60,63 @@ public class OperationLogAspect {
|
|||
sysLog.setErrorMsg(e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
// 记录日志
|
||||
saveLog(point, operationLog, beginTime, sysLog);
|
||||
try {
|
||||
// 记录日志
|
||||
saveLog(point, operationLog, beginTime, sysLog);
|
||||
} catch (Exception e) {
|
||||
log.error("记录操作日志失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*/
|
||||
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);
|
||||
// 获取当前请求
|
||||
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();
|
||||
String className = method.getDeclaringClass().getName();
|
||||
String methodName = method.getName();
|
||||
sysLog.setMethod(className + "." + methodName);
|
||||
|
||||
// 获取请求参数
|
||||
try {
|
||||
String params = objectMapper.writeValueAsString(joinPoint.getArgs());
|
||||
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.setCreateTime(LocalDateTime.now());
|
||||
sysLog.setTimeConsuming(System.currentTimeMillis() - beginTime);
|
||||
|
||||
// 保存日志
|
||||
sysLogMapper.insert(sysLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取IP地址
|
||||
*/
|
||||
private String getIpAddress(HttpServletRequest request) {
|
||||
String ip = request.getHeader("X-Forwarded-For");
|
||||
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)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
// 多个代理的情况,第一个IP为客户端真实IP
|
||||
if (ip != null && ip.contains(",")) {
|
||||
ip = ip.split(",")[0].trim();
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue