86 lines
3.7 KiB
Java
86 lines
3.7 KiB
Java
package com.whdc.zhdbaqapi.aspect;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
|
|
import com.whdc.zhdbaqapi.utils.ReqUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.aspectj.lang.JoinPoint;
|
|
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.Before;
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import javax.servlet.ServletRequest;
|
|
import javax.servlet.ServletResponse;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
/**
|
|
* @author 李赛
|
|
* @date 2022-06-26 0:34
|
|
*/
|
|
|
|
@Aspect
|
|
@Component
|
|
@Slf4j
|
|
public class WebLogAspect {
|
|
|
|
private void print(String fmt, Object... str) {
|
|
log.info(fmt, str);
|
|
}
|
|
|
|
@Pointcut("execution(public * com.whdc.zhdbaqapi.controller.*Controller.*(..))")
|
|
public void controllerPointcut() {
|
|
}
|
|
|
|
@Before("controllerPointcut()")
|
|
public void doBefore(JoinPoint joinPoint) throws Throwable {
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
HttpServletRequest request = attributes.getRequest();
|
|
Signature signature = joinPoint.getSignature();
|
|
String name = signature.getName();
|
|
|
|
print("------------- 开始 -------------");
|
|
print("请求: {} {}", name, request.getRequestURI(), request.getMethod());
|
|
print("方法: {} {}.{}", name, signature.getDeclaringTypeName(), name);
|
|
print("地址: {} {}", name, ReqUtils.getIpAddress(request));
|
|
|
|
Object[] args = joinPoint.getArgs();
|
|
Object[] arguments = new Object[args.length];
|
|
for (int i = 0; i < args.length; i++) {
|
|
if (args[i] instanceof ServletRequest
|
|
|| args[i] instanceof ServletResponse
|
|
|| args[i] instanceof MultipartFile) {
|
|
continue;
|
|
}
|
|
arguments[i] = args[i];
|
|
}
|
|
|
|
String[] excludeProperties = {"password", "file"};
|
|
PropertyPreFilters filters = new PropertyPreFilters();
|
|
PropertyPreFilters.MySimplePropertyPreFilter excludeFilter = filters.addFilter();
|
|
excludeFilter.addExcludes(excludeProperties);
|
|
print("参数: {} {}", name, JSONObject.toJSONString(arguments, excludeFilter));
|
|
}
|
|
|
|
@Around("controllerPointcut()")
|
|
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
|
long startTime = System.currentTimeMillis();
|
|
Signature signature = proceedingJoinPoint.getSignature();
|
|
String name = signature.getName();
|
|
Object result = proceedingJoinPoint.proceed();
|
|
|
|
String[] excludeProperties = {"password", "file"};
|
|
PropertyPreFilters filters = new PropertyPreFilters();
|
|
PropertyPreFilters.MySimplePropertyPreFilter excludeFilter = filters.addFilter();
|
|
excludeFilter.addExcludes(excludeProperties);
|
|
// print("返回结果: {}", JSONObject.toJSONString(result, excludeFilter));
|
|
print("------------- 耗时:{} {} ms -------------", name, System.currentTimeMillis() - startTime);
|
|
return result;
|
|
}
|
|
} |