SpringBoot AOP完全讲解二:统一处理请求日志

转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80669451
本文出自【赵彦军的博客】

前言

不了解 AOP 概念的同学,请先看 AOP 基础文章。

SpringBoot AOP完全讲解一:基础概念

添加依赖

maven

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

gradle

compile('org.springframework.boot:spring-boot-starter-web',
        'org.springframework.boot:spring-boot-starter-aop' )

新建切面文章

新建切面类 ControllerAspect

package com.yiba.didiapi.aop;

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

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

@Aspect
@Component
public class ControllerAspect {

    //对 com.yiba.didiapi.controller 这个包做切面
    @Pointcut("execution(public * com.yiba.didiapi.controller.*.*(..))")
    public void postController() {

    }

    @Before("postController()")
    public void beforePost() {
        //请求前处理

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

        Enumeration<String> headerNames = request.getHeaderNames();

        //获取请求的 header
        HashMap<String, String> headMap = new HashMap<>();
        while (headerNames.hasMoreElements()) {//以此取出头信息
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);//取出头信息内容
            headMap.put(headerName, headerValue);
        }

        //获取完整的url路径:http://localhost:8083/sdk/wii/swbu
        StringBuffer url = request.getRequestURL();

        //获取请求的资源名部分:/sdk/wii/swbu
        String uri = request.getRequestURI();

        //获取请求类型:POST
        String method = request.getMethod();

        //获取请求ip:12.45.36.23
        String ip = request.getRemoteAddr();

        System.out.println("url:" + url);
        System.out.println("uri:" + uri);
        System.out.println("method:" + method);
        System.out.println("ip:" + ip);

        headMap.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(String s, String s2) {
                System.out.println("head name:" + s + "  value:" + s2);
            }
        });

        //获取请求表单参数
        Map<String, String[]> paramsMap = request.getParameterMap();
        String queryString = "";
        for (String key : paramsMap.keySet()) {
            String[] values = paramsMap.get(key);
            for (int i = 0; i < values.length; i++) {
                String value = values[i];
                queryString += key + "=" + value + "&";
            }
        }

        // 去掉最后一个空格
        if (queryString.length() > 0) {
            queryString = queryString.substring(0, queryString.length() - 1);
            System.out.println("queryString:" + queryString);
        }

        //获取post请求的body内容
        try {
            StringBuffer sb = new StringBuffer();
            InputStream is = request.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
            String s = "";
            while ((s = br.readLine()) != null) {
                sb.append(s);
            }
            //获取post请求的body内容
            System.out.println("request body:" + sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @After("postController()")
    public void afterPost() {
      //请求后处理
    }

}

参考资料

SpringBoot AOP统一处理请求日志


个人微信号:zhaoyanjun125 , 欢迎关注

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值