content: package cn.org.alan.agile.common.aop;
  
  import org.aspectj.lang.JoinPoint;
  import org.aspectj.lang.ProceedingJoinPoint;
  import org.aspectj.lang.Signature;
  import org.aspectj.lang.annotation.Around;
  import org.aspectj.lang.annotation.Aspect;
  import org.aspectj.lang.annotation.Before;
  import org.aspectj.lang.annotation.Pointcut;
  import org.springframework.stereotype.Component;
  
  import com.alibaba.fastjson.JSONObject;
  import com.alibaba.fastjson.support.spring.PropertyPreFilters;
  
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.web.context.request.RequestContextHolder;
  import org.springframework.web.context.request.ServletRequestAttributes;
  import org.springframework.web.multipart.MultipartFile;
  
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletRequest;
  
  /**
   * @Author Alan
   * @Version
   * @Date 2024/5/13 2:33 PM
   */
  
  @Aspect
  @Component
  public class LogAsPect {
  
      private final static Logger LOG = LoggerFactory.getLogger(LogAsPect.class);
      // @Around("execution(* cn.org.alan.exam.controller..*.*(..))")
      @Pointcut("execution(public * cn.org.alan.agile.controller..*Controller.*(..))")
      public void controllerPointcut() {}
  
  
  
      @Before("controllerPointcut()")
      public void doBefore(JoinPoint joinPoint) throws Throwable {
  
          ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
          HttpServletRequest request = attributes.getRequest();
          Signature signature = joinPoint.getSignature();
          String name = signature.getName();
  
          LOG.info("------------- 开始 -------------");
          LOG.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
          LOG.info("类名方法: {}.{}", signature.getDeclaringTypeName(), name);
          LOG.info("远程地址: {}", request.getRemoteAddr());
  
          Object[] args = joinPoint.getArgs();
          Object[] arguments  = new Object[args.length];
          for (int i = 0; i < args.length; i++) {
              if (args[i] instanceof ServletRequest
                      || args[i] instanceof ServletResponse
                      || args[i] instanceof MultipartFile) {
                  continue;
              }
              arguments[i] = args[i];
          }
  
          String[] excludeProperties = {"password", "file"};
          PropertyPreFilters filters = new PropertyPreFilters();
          PropertyPreFilters.MySimplePropertyPreFilter excludeFilter = filters.addFilter();
          excludeFilter.addExcludes(excludeProperties);
          LOG.info("请求参数: {}", JSONObject.toJSONString(arguments, excludeFilter));
      }
  
      @Around("controllerPointcut()")
      public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
          long startTime = System.currentTimeMillis();
          Object result = proceedingJoinPoint.proceed();
  
          String[] excludeProperties = {"password", "file"};
          PropertyPreFilters filters = new PropertyPreFilters();
          PropertyPreFilters.MySimplePropertyPreFilter excludeFilter = filters.addFilter();
          excludeFilter.addExcludes(excludeProperties);
          LOG.info("返回结果: {}", JSONObject.toJSONString(result, excludeFilter));
          LOG.info("------------- 结束 耗时：{} ms -------------", System.currentTimeMillis() - startTime);
          return result;
      }
  
  }
  