天天看點

SpringBoot AOP列印請求參數與響應參數

package com.sszh.mall.operate.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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * SpringBoot AOP列印請求參數與響應參數
 */
@Aspect
@Order(5)
@Component
public class ControllerLogAspect {

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

    @Pointcut("execution(public * com.sszh.mall.operate.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 (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 (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