spring boot aop 中獲取requestbody參數(shù)

看到許多不優(yōu)雅的方法,唯有這個(gè)看著最好看

@Before("log()")
    public void beforeLog(JoinPoint point) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
        log.info("【請(qǐng)求 URL】:{}", request.getRequestURL());
        log.info("【請(qǐng)求 IP】:{}", request.getRemoteAddr());
        log.info("【請(qǐng)求類名】:{},【請(qǐng)求方法名】:{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName());
        log.info("【body】:{},", JSONUtil.toJsonStr(point.getArgs()));
        Map<String, String[]> parameterMap = request.getParameterMap();
        log.info("【請(qǐng)求參數(shù)】:{},", JSONUtil.toJsonStr(parameterMap));
        Long start = System.currentTimeMillis();
        request.setAttribute(START_TIME, start);
    }

就是這一句point.getArgs()其實(shí)aspectj已經(jīng)替我們作好這一切了

package com.xkcoding.log.aop.aspectj;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.hutool.json.JSONUtil;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;

/**
 * @author www.gaozz.club
 * @功能描述 aop 中獲取requestbody參數(shù)
 * @date 2018-08-26
 */
@Aspect
@Component
@Slf4j
public class AopLog {
    private static final String START_TIME = "request-start";

    /**
     * 切入點(diǎn)
     */
    @Pointcut("execution(public * com.xkcoding.log.aop.controller.*Controller.*(..))")
    public void log() {

    }

    /**
     * 前置操作
     *
     * @param point 切入點(diǎn)
     */
    @Before("log()")
    public void beforeLog(JoinPoint point) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
        log.info("【請(qǐng)求 URL】:{}", request.getRequestURL());
        log.info("【請(qǐng)求 IP】:{}", request.getRemoteAddr());
        log.info("【請(qǐng)求類名】:{},【請(qǐng)求方法名】:{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName());
        log.info("【body】:{},", JSONUtil.toJsonStr(point.getArgs()));
        Map<String, String[]> parameterMap = request.getParameterMap();
        log.info("【請(qǐng)求參數(shù)】:{},", JSONUtil.toJsonStr(parameterMap));
        Long start = System.currentTimeMillis();
        request.setAttribute(START_TIME, start);
    }

    /**
     * 環(huán)繞操作
     *
     * @param point 切入點(diǎn)
     * @return 原方法返回值
     * @throws Throwable 異常信息
     */
    @Around("log()")
    public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
        Object result = point.proceed();
        log.info("【返回值】:{}", JSONUtil.toJsonStr(result));
        return result;
    }

    /**
     * 后置操作
     */
    @AfterReturning("log()")
    public void afterReturning() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

        Long start = (Long) request.getAttribute(START_TIME);
        Long end = System.currentTimeMillis();
        log.info("【請(qǐng)求耗時(shí)】:{}毫秒", end - start);

        String header = request.getHeader("User-Agent");
        UserAgent userAgent = UserAgent.parseUserAgentString(header);
        log.info("【瀏覽器類型】:{},【操作系統(tǒng)】:{},【原始User-Agent】:{}", userAgent.getBrowser().toString(), userAgent.getOperatingSystem().toString(), header);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基本知識(shí) 其實(shí), 接觸了這么久的 AOP, 我感覺, AOP 給人難以理解的一個(gè)關(guān)鍵點(diǎn)是它的概念比較多, 而且坑爹...
    永順閱讀 8,659評(píng)論 5 114
  • IoC 容器 Bean 的作用域 自定義作用域?qū)崿F(xiàn) org.springframework.beans.facto...
    Hsinwong閱讀 2,615評(píng)論 0 7
  • 在倦怠中,我又嗅到了奄奄一息的味道 那是我身上的氣息 找不到什么是人生的意義 若要我樹立奉獻(xiàn)是人生的價(jià)值所在的話 ...
    像我這樣的人兒閱讀 527評(píng)論 0 0
  • 以下為張泉靈在2018(第十屆)中國(guó)商界木蘭年會(huì)上的演講實(shí)錄(有刪節(jié)): 作為投資人,我們每天會(huì)接觸大量的創(chuàng)業(yè)者,...
    牛海萍閱讀 594評(píng)論 0 0
  • 敬愛的??偅髳鄣耐蹩?,親愛的家人們: 大家好!今天是我的日精進(jìn)第69天,給大家分享我今天的進(jìn)步改變,我們互相勉勵(lì)...
    娜娜happy閱讀 212評(píng)論 0 0

友情鏈接更多精彩內(nèi)容