SpringBoot AOP打印请求参数与响应参数

  • Post author:
  • Post category:其他


package ai.yiye.agent.landingpage.filter;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * SpringBoot AOP打印请求参数与响应参数
 */
@Aspect
@Order(5)
@Component
public class ControllerLogAspect {

    private static final Logger logger = LoggerFactory.getLogger(ControllerLogAspect.class);

    @Pointcut("execution(public * ai.yiye.agent.landingpage.controller..*.*(..))")
    public void controllerLog() {
    }


    @Before("controllerLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {

        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 只记录post方法
        if("POST".equals(request.getMethod())){
            // 记录下请求内容
            logger.info("********************************************");
            logger.info("请求URL : " + request.getRequestURL());
            logger.info("客户端ip ["+ request.getRemoteAddr() +"]");
            logger.info("请求映射 : 【" + joinPoint.getSignature().getDeclaringTypeName() + "】类的【" + joinPoint.getSignature().getName() + "】方法");

            // 获取参数, 只取自定义的参数, 自带的HttpServletRequest, HttpServletResponse不管
            if (joinPoint.getArgs().length > 0) {
                for (Object o : joinPoint.getArgs()) {
                    if (Objects.isNull(o) || o instanceof MultipartFile || o instanceof MultipartFile[] || o instanceof HttpServletRequest || o instanceof HttpServletResponse) {
                        continue;
                    }
                    logger.info("请求参数 : " + JSONObject.toJSONString(o));
                }
            }
        }
        // 只记录post方法
        if("GET".equals(request.getMethod())){
            // 记录下请求内容
            logger.info("请求URL : " + request.getRequestURL());
            logger.info("请求IP : " + request.getRemoteAddr());
            logger.info("请求映射 : 【" + joinPoint.getSignature().getDeclaringTypeName() + "】类的【" + joinPoint.getSignature().getName() + "】方法");

            // 获取参数, 只取自定义的参数, 自带的HttpServletRequest, HttpServletResponse不管
            if (joinPoint.getArgs().length > 0) {
                for (Object o : joinPoint.getArgs()) {
                    if (Objects.isNull(o) || o instanceof MultipartFile || o instanceof MultipartFile[] || o instanceof HttpServletRequest || o instanceof HttpServletResponse) {
                        continue;
                    }
                    logger.info("请求参数 : " + JSONObject.toJSONString(o));
                }
            }
        }
    }


    @AfterReturning(returning = "ret", pointcut = "controllerLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        logger.info("返回 : " + JSONObject.toJSONString(ret));
        logger.info("请求结束 : " + "********************************************");
    }

}

过滤器方式:

https://blog.csdn.net/jiahao1186/article/details/91870776

该方式通过获取request中的流来进行打印,有个缺点是流只能读取一次,在打印日志时使用了request.getReader之后,还需要构造新的request交给controller使用,


https://blog.csdn.net/mazhen1991/article/details/80695473

原文出自:

https://blog.csdn.net/noseew/article/details/79179892



版权声明:本文为seesun2012原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。