SpringAOP获取请求信息


当需要在aop中获取请求的参数,并做拦截时,可以参考下面的方法:

package com.zxh.configure;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.http.HttpMethod;
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;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

@Aspect
@Component
@Slf4j
public class ControllerAspect {

    private static ObjectMapper objectMapper = new ObjectMapper();

    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    public void controllerGetMethodPointcut() {
    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public void controllerPostMethodPointcut() {
    }

    @Before("controllerGetMethodPointcut()")
    public void doBeforeGetMethod(JoinPoint joinPoint) throws Exception {
        Map<String, Object> requestParams = getRequestParams(joinPoint);
    }

    @Before("controllerPostMethodPointcut()")
    public void doBeforePostMethod(JoinPoint joinPoint) throws Exception {
        Map<String, Object> requestParams = this.getRequestParams(joinPoint);
        this.preHandle(requestParams);
    }

    private void preHandle(Map<String, Object> requestParams) {
        String uri = (String) requestParams.get("uri");
        String method = (String) requestParams.get("method");
        String params = ((List<String>) requestParams.get("params")).get(0);
        JSONObject headers = (JSONObject) requestParams.get("headers");
        if (!HttpMethod.POST.toString().equals(method)) {
            throw new RuntimeException("方法不被允许");
        }
     //写具体的业务,用于拦截。不符合条件时直接抛出异常,全局捕获异常进行处理

    }

    //获取请求的相关信息
    private Map<String, Object> getRequestParams(JoinPoint joinPoint) throws UnsupportedEncodingException {
        Map<String, Object> requestParams = new HashMap<>();
        //获取请求信息
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        requestParams.put("uri", request.getRequestURI());
        // 获取请求头信息,需要注意的是,请求头中的key都会转成小写
        Enumeration<String> enumeration = request.getHeaderNames();
        JSONObject headers = new JSONObject();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            String value = request.getHeader(name);
            headers.put(name, value);
        }
        requestParams.put("headers", headers);
        //获取请求的方法
        String method = request.getMethod();
        requestParams.put("method", method);
        List<String> params = new ArrayList<>();
        if (HttpMethod.GET.toString().equals(method)) {// get请求
            String queryString = request.getQueryString();
            if (StringUtils.isNotBlank(queryString)) {
                params.add(0, URLDecoder.decode(queryString, "UTF-8"));
            }
        } else {//其他请求
            Object[] paramsArray = joinPoint.getArgs();
            if (paramsArray != null && paramsArray.length > 0) {
                for (int i = 0; i < paramsArray.length; i++) {
                    if (paramsArray[i] instanceof Serializable || paramsArray[i] instanceof RequestFacade) {
                        params.add(paramsArray[i].toString());
                    } else {
                        try {
                            //使用json系列化 反射等等方法 反系列化会影响请求性能建议重写toString方法实现系列化接口
                            String param = objectMapper.writeValueAsString(paramsArray[i]);
                            if (StringUtils.isNotBlank(param))
                                params.add(param);
                        } catch (JsonProcessingException e) {
                            e.printStackTrace();
                            log.error("json序列化异常", e);
                        }
                    }
                }
            }
        }

        log.info(">>>>>>uri: {},method: {}", request.getRequestURI(), method);
        log.info(">>>>>>headers: {}", headers);
        log.info(">>>>>>params: {}", params);
        requestParams.put("params", params);
        return requestParams;
    }


}

使用时需要导入aop的依赖:

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

分别请求下面的路径,进行测试

get请求:

日志打印结果

 

post请求

日志打印结果

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM