package com.gkhy.safePlatform.specialWork.common.aop; import com.alibaba.fastjson.JSONObject; import com.gkhy.safePlatform.commons.co.ContextCacheUser; import com.gkhy.safePlatform.specialWork.model.annotation.CommonLogEnable; import com.gkhy.safePlatform.specialWork.model.constant.LogTemplate; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @Aspect @Component("specialWorkControllerReqLogAspect") public class CommonLogAspect { private static Logger log = LoggerFactory.getLogger(CommonLogAspect.class); @Around("execution(* com.gkhy.safePlatform.specialWork.controller.*.*(..)) && @annotation(commonLogEnable)") public Object controllerReqParamLog(ProceedingJoinPoint point, CommonLogEnable commonLogEnable) throws Throwable { this.record(point); // 执行 Object proceed = point.proceed(); return proceed; } private void record(ProceedingJoinPoint point) { log.info(LogTemplate.REQUEST_RECORD_START); MethodSignature methodSignature = (MethodSignature) point.getSignature(); Method method = methodSignature.getMethod(); CommonLogEnable annotation = method.getAnnotation(CommonLogEnable.class); // 模块 log.info(LogTemplate.REQUEST_MODULE, annotation.module().value); log.info(LogTemplate.REQUEST_INTRO, annotation.content()); // 请求参数 ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (requestAttributes != null) { HttpServletRequest request = requestAttributes.getRequest(); log.info(LogTemplate.REQUEST_IP, getIp(request)); if (request.getUserPrincipal() instanceof Authentication) { Authentication authentication = (Authentication) request.getUserPrincipal(); ContextCacheUser requester = (ContextCacheUser) authentication.getPrincipal(); log.info(LogTemplate.REQUEST_OWNER, requester.getRealName(), requester.getUid(), requester.getRoleCode()); } log.info(LogTemplate.REQUEST_AGENT, request.getHeader("User-Agent")); log.info(LogTemplate.REQUEST_URL, request.getRequestURL()); log.info(LogTemplate.REQUEST_PARAMS, JSONObject.toJSONString(request.getParameterMap())); // 支持 application/json 类型返回,不建议使用 request 读取 body ,getInputStream() 仅能使用一次 String body = ""; Annotation[][] annotations = method.getParameterAnnotations(); for (int i = 0; i < annotations.length; i++) { for (Annotation b : annotations[i]) { if (b.annotationType().equals(RequestBody.class)) { body = JSONObject.toJSONString(point.getArgs()[i]); break; } } } log.info(LogTemplate.REQUEST_BODY, body); } String className = point.getTarget().getClass().getName(); String methodName = methodSignature.getName(); log.info(LogTemplate.REQUEST_LOCATION,className,methodName); log.info(LogTemplate.REQUEST_RECORD_END); } private String getIp(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; } }