| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- 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;
- }
- }
|