package com.oms.aspect; import com.oms.entity.SysOperationLog; import com.oms.mapper.SysOperationLogMapper; 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.annotation.Pointcut; 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.time.LocalDateTime; @Slf4j @Aspect @Component @RequiredArgsConstructor public class OperationLogAspect { private final SysOperationLogMapper sysOperationLogMapper; @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping) || " + "@annotation(org.springframework.web.bind.annotation.PutMapping) || " + "@annotation(org.springframework.web.bind.annotation.DeleteMapping)") public void controllerPointcut() {} @Around("controllerPointcut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object result = null; String resultStatus = "SUCCESS"; try { result = joinPoint.proceed(); return result; } catch (Exception e) { resultStatus = "FAILED"; throw e; } finally { long duration = System.currentTimeMillis() - startTime; try { saveOperationLog(joinPoint, resultStatus, duration); } catch (Exception e) { log.error("Failed to save operation log", e); } } } private void saveOperationLog(ProceedingJoinPoint joinPoint, String result, long duration) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (attributes == null) { return; } String ip = attributes.getRequest().getRemoteAddr(); String actor = "anonymous"; String actorRole = ""; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null && authentication.isAuthenticated()) { actor = authentication.getName(); actorRole = authentication.getAuthorities().stream() .findFirst() .map(a -> a.getAuthority().replace("ROLE_", "")) .orElse(""); } String className = joinPoint.getTarget().getClass().getSimpleName(); String methodName = joinPoint.getSignature().getName(); String module = className.replace("Controller", ""); String type = methodName.toUpperCase(); Long objectId = extractObjectId(joinPoint.getArgs()); SysOperationLog logEntry = new SysOperationLog(); logEntry.setModule(module); logEntry.setType(type); logEntry.setObjectId(objectId); logEntry.setActor(actor); logEntry.setActorRole(actorRole); logEntry.setAction(methodName + "()"); logEntry.setResult(result); logEntry.setSourceIp(ip); logEntry.setRemark("Duration: " + duration + "ms"); sysOperationLogMapper.insert(logEntry); } private Long extractObjectId(Object[] args) { if (args == null || args.length == 0) { return null; } for (Object arg : args) { if (arg instanceof Long) { return (Long) arg; } } return null; } }