aop springboot 传入参数_SpringBoot使用AOP获取请求参数

该博客介绍了一个SpringBoot应用中使用AOP来捕获并记录控制器方法的请求参数的实现。通过定义切点,拦截Controller层的方法调用,获取请求IP、请求URL、方法名、参数及耗时,并进行日志记录。
摘要由CSDN通过智能技术生成

1 importcom.alibaba.fastjson.JSON;2 importcom.google.common.base.Stopwatch;3 importio.swagger.annotations.Api;4 importio.swagger.annotations.ApiOperation;5 importlombok.extern.slf4j.Slf4j;6 importorg.aspectj.lang.ProceedingJoinPoint;7 importorg.aspectj.lang.Signature;8 importorg.aspectj.lang.annotation.Around;9 importorg.aspectj.lang.annotation.Aspect;10 importorg.aspectj.lang.annotation.Pointcut;11 importorg.aspectj.lang.reflect.MethodSignature;12 importorg.springframework.stereotype.Component;13 importorg.springframework.web.context.request.RequestContextHolder;14 importorg.springframework.web.context.request.ServletRequestAttributes;15

16 importjavax.servlet.http.HttpServletRequest;17 importjavax.servlet.http.HttpServletResponse;18 importjava.util.ArrayList;19 importjava.util.List;20 importjava.util.concurrent.TimeUnit;21

22 /**

23 * @Description //请求参数aop24 **/

25 @Component26 @Aspect27 @Slf4j28 public classRequestParameterAop {29

30 /**

31 * @Description: 定义需要拦截的切面32 * @Pointcut("execution(* com.*.controller.*Controller.*(..))")33 * @Return: void34 **/

35 @Pointcut("execution(* com.*.controller.*Controller.*(..))")36 public voidmethodArgs() {37

38 }39

40 @Around("methodArgs()")41 public Object invoke(ProceedingJoinPoint joinPoint) throwsThrowable {42 Object result;43

44 Stopwatch stopwatch =Stopwatch.createStarted();45 result =joinPoint.proceed();46 try{47 HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();48 //ip地址

49 String ipAddr =getRemoteHost(request);50 //请求路径

51 String requestUrl =request.getRequestURL().toString();52

53 //获取请求参数进行打印

54 Signature signature =joinPoint.getSignature();55 MethodSignature methodSignature =(MethodSignature) signature;56

57 //类名58 //swagger中文注释名

59 String classCommentName = methodSignature.getMethod().getDeclaringClass().getAnnotation(Api.class).tags()[0];60 String[] sourceName = signature.getDeclaringTypeName().split("\\.");61 String className = sourceName[sourceName.length - 1] + "[" + classCommentName +"]";62

63 //方法名64 //swagger中文注释名

65 String methodCommentName = methodSignature.getMethod().getAnnotation(ApiOperation.class).value();66 String methodName = signature.getName() + "[" + methodCommentName + "]";67

68 //参数名数组

69 String[] parameterNames =((MethodSignature) signature).getParameterNames();70 //构造参数组集合

71 List argList = new ArrayList<>();72 for(Object arg : joinPoint.getArgs()) {73 //request/response无法使用toJSON

74 if (arg instanceofHttpServletRequest) {75 argList.add("request");76 } else if (arg instanceofHttpServletResponse) {77 argList.add("response");78 } else{79 argList.add(JSON.toJSON(arg));80 }81 }82

83 stopwatch.stop();84 long timeConsuming =stopwatch.elapsed(TimeUnit.MILLISECONDS);85

86 log.error("请求源IP【{}】 -> 请求URL【{}】\n{} -> {} -> 请求耗时:[{}]毫秒 \n请求参数:{} -> {}\n请求结果:{}",87 ipAddr, requestUrl,88 className, methodName, timeConsuming,89 JSON.toJSON(parameterNames), JSON.toJSON(argList),90 JSON.toJSON(result));91 } catch(Exception e) {92 log.error("参数获取失败: {}", e.getMessage());93 }94

95 returnresult;96 }97

98 /**

99 * 获取目标主机的ip100 *@paramrequest101 *@return

102 */

103 privateString getRemoteHost(HttpServletRequest request) {104 String ip = request.getHeader("x-forwarded-for");105 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {106 ip = request.getHeader("Proxy-Client-IP");107 }108 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {109 ip = request.getHeader("WL-Proxy-Client-IP");110 }111 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {112 ip =request.getRemoteAddr();113 }114 return ip.contains("0:0:0:0:0:0:0:1") ? "127.0.0.1": ip;115 }116

117 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值