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 }