通知参数处理
@Aspect
@Component
@Slf4j
public class LogServiceAspect {
@Autowired
private LogService logService;
@Around("execution(* com.zrar.ai.controller.WebController.*(..)) && @annotation(com.zrar.ai.annotation.Log)")
public Object addLog(ProceedingJoinPoint jp) throws Throwable {
Object result;
MethodSignature signature = (MethodSignature) jp.getSignature();
Method method1 = signature.getMethod();
String method = method1.getName();
Log annotation = method1.getAnnotation(Log.class);
String description = annotation.value();
String ip = getIpAddress();
StringBuilder params = new StringBuilder("{");
Object[] argValues = jp.getArgs();
String[] argNames = ((MethodSignature)jp.getSignature()).getParameterNames();
if(argValues != null){
for (int i = 0; i < argValues.length; i++) {
params.append(" ").append(argNames[i]).append(": ").append(argValues[i]);
}
}
String parameters = params.toString()+"}";
long befTime = System.currentTimeMillis();
result = jp.proceed();
long nowTime = System.currentTimeMillis();
Date date = new Date(nowTime);
Long cost =nowTime-befTime;
logService.addLog(method,ip,parameters,description,cost,date);
return result;
}
public static String getIpAddress() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Aspect {
String value() default "";
}