<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Stopwatch;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
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.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
@Aspect
@Slf4j
public class RequestParameterAop {
@Pointcut("execution(* com.*.controller.*Controller.*(..))")
public void methodArgs() {
}
@Around("methodArgs()")
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
Object result;
Stopwatch stopwatch = Stopwatch.createStarted();
result = joinPoint.proceed();
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ipAddr = getRemoteHost(request);
String requestUrl = request.getRequestURL().toString();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
String classCommentName = methodSignature.getMethod().getDeclaringClass().getAnnotation(Api.class).tags()[0];
String[] sourceName = signature.getDeclaringTypeName().split("\\.");
String className = sourceName[sourceName.length - 1] + "[" + classCommentName +"]";
String methodCommentName = methodSignature.getMethod().getAnnotation(ApiOperation.class).value();
String methodName = signature.getName() + "[" + methodCommentName + "]";
String[] parameterNames = ((MethodSignature) signature).getParameterNames();
List<Object> argList = new ArrayList<>();
for (Object arg : joinPoint.getArgs()) {
if (arg instanceof HttpServletRequest) {
argList.add("request");
} else if (arg instanceof HttpServletResponse) {
argList.add("response");
} else {
argList.add(JSON.toJSON(arg));
}
}
stopwatch.stop();
long timeConsuming = stopwatch.elapsed(TimeUnit.MILLISECONDS);
String simpleUUID = IdUtil.simpleUUID();
log.info("RequestID:{}\n请求源IP【{}】 -> 请求URL【{}】\n{} -> {} -> 请求耗时:[{}]毫秒 \n请求参数:{} -> {}",
simpleUUID,
ipAddr, requestUrl,
className, methodName, timeConsuming,
JSON.toJSON(parameterNames), JSON.toJSON(argList));
log.info("RequestID:{}\n请求结果:{}", simpleUUID, JSON.toJSON(result) );
} catch (Exception e) {
log.error("参数获取失败: {}", e.getMessage());
}
return result;
}
private String getRemoteHost(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip.contains("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
}
}