OperationLogAspect.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.oms.aspect;
  2. import com.oms.entity.SysOperationLog;
  3. import com.oms.mapper.SysOperationLogMapper;
  4. import lombok.RequiredArgsConstructor;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.annotation.Around;
  8. import org.aspectj.lang.annotation.Aspect;
  9. import org.aspectj.lang.annotation.Pointcut;
  10. import org.springframework.security.core.Authentication;
  11. import org.springframework.security.core.context.SecurityContextHolder;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.web.context.request.RequestContextHolder;
  14. import org.springframework.web.context.request.ServletRequestAttributes;
  15. import java.time.LocalDateTime;
  16. @Slf4j
  17. @Aspect
  18. @Component
  19. @RequiredArgsConstructor
  20. public class OperationLogAspect {
  21. private final SysOperationLogMapper sysOperationLogMapper;
  22. @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
  23. "@annotation(org.springframework.web.bind.annotation.PutMapping) || " +
  24. "@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
  25. public void controllerPointcut() {}
  26. @Around("controllerPointcut()")
  27. public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
  28. long startTime = System.currentTimeMillis();
  29. Object result = null;
  30. String resultStatus = "SUCCESS";
  31. try {
  32. result = joinPoint.proceed();
  33. return result;
  34. } catch (Exception e) {
  35. resultStatus = "FAILED";
  36. throw e;
  37. } finally {
  38. long duration = System.currentTimeMillis() - startTime;
  39. try {
  40. saveOperationLog(joinPoint, resultStatus, duration);
  41. } catch (Exception e) {
  42. log.error("Failed to save operation log", e);
  43. }
  44. }
  45. }
  46. private void saveOperationLog(ProceedingJoinPoint joinPoint, String result, long duration) {
  47. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  48. if (attributes == null) {
  49. return;
  50. }
  51. String ip = attributes.getRequest().getRemoteAddr();
  52. String actor = "anonymous";
  53. String actorRole = "";
  54. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  55. if (authentication != null && authentication.isAuthenticated()) {
  56. actor = authentication.getName();
  57. actorRole = authentication.getAuthorities().stream()
  58. .findFirst()
  59. .map(a -> a.getAuthority().replace("ROLE_", ""))
  60. .orElse("");
  61. }
  62. String className = joinPoint.getTarget().getClass().getSimpleName();
  63. String methodName = joinPoint.getSignature().getName();
  64. String module = className.replace("Controller", "");
  65. String type = methodName.toUpperCase();
  66. Long objectId = extractObjectId(joinPoint.getArgs());
  67. SysOperationLog logEntry = new SysOperationLog();
  68. logEntry.setModule(module);
  69. logEntry.setType(type);
  70. logEntry.setObjectId(objectId);
  71. logEntry.setActor(actor);
  72. logEntry.setActorRole(actorRole);
  73. logEntry.setAction(methodName + "()");
  74. logEntry.setResult(result);
  75. logEntry.setSourceIp(ip);
  76. logEntry.setRemark("Duration: " + duration + "ms");
  77. sysOperationLogMapper.insert(logEntry);
  78. }
  79. private Long extractObjectId(Object[] args) {
  80. if (args == null || args.length == 0) {
  81. return null;
  82. }
  83. for (Object arg : args) {
  84. if (arg instanceof Long) {
  85. return (Long) arg;
  86. }
  87. }
  88. return null;
  89. }
  90. }