转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80669451
本文出自【赵彦军的博客】
前言
不了解 AOP
概念的同学,请先看 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() {
//请求后处理
}
}
参考资料
个人微信号:zhaoyanjun125 , 欢迎关注