|
@@ -1,112 +1,112 @@
|
|
-package com.fdkankan.common.aop;
|
|
|
|
-
|
|
|
|
-import com.fdkankan.common.constant.LogFormatConstant;
|
|
|
|
-import com.fdkankan.common.factory.LogFactory;
|
|
|
|
-import java.util.Enumeration;
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
-import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
-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.slf4j.Logger;
|
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
-import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
-import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
-
|
|
|
|
-@Component
|
|
|
|
-@Aspect
|
|
|
|
-@Slf4j
|
|
|
|
-public class VisitLogInterceptor {
|
|
|
|
-
|
|
|
|
- // 切入点表达式
|
|
|
|
- @Pointcut("execution(public * com.fdkankan.*..controller..*.*(..))")
|
|
|
|
- public void privilege() {
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Around("privilege()")
|
|
|
|
- public Object around(ProceedingJoinPoint pjp) throws Throwable {
|
|
|
|
-
|
|
|
|
- // 获取类名
|
|
|
|
- String className = pjp.getTarget().getClass().getName();// pjp.getTarget().getClass().getSimpleName();
|
|
|
|
- // 获取执行的方法名称
|
|
|
|
- String methodName = pjp.getSignature().getName();
|
|
|
|
- // 获取参数名称
|
|
|
|
- String[] parameterNamesArgs = ((MethodSignature) pjp.getSignature()).getParameterNames();
|
|
|
|
- // 定义返回参数
|
|
|
|
- Object result = null;
|
|
|
|
- // 获取方法参数
|
|
|
|
- Object[] args = pjp.getArgs();
|
|
|
|
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
|
- // 请求的URL
|
|
|
|
- String requestURL = request.getRequestURL().toString();
|
|
|
|
- String ip = getIpAddr(request);
|
|
|
|
-
|
|
|
|
- StringBuffer paramsBuf = new StringBuffer();
|
|
|
|
- // 获取请求参数集合并进行遍历拼接
|
|
|
|
- for (int i = 0; i < args.length; i++) {
|
|
|
|
- if (paramsBuf.length() > 0) {
|
|
|
|
- paramsBuf.append("|");
|
|
|
|
- }
|
|
|
|
- paramsBuf.append(parameterNamesArgs[i]).append(" = ").append(args[i]);
|
|
|
|
- }
|
|
|
|
- StringBuffer headerBuf = new StringBuffer();
|
|
|
|
- Enumeration<String> headerNames = request.getHeaderNames();
|
|
|
|
- while (headerNames.hasMoreElements()) {
|
|
|
|
- String key = (String) headerNames.nextElement();
|
|
|
|
- String value = request.getHeader(key);
|
|
|
|
- if (headerBuf.length() > 0) {
|
|
|
|
- headerBuf.append("|");
|
|
|
|
- }
|
|
|
|
- headerBuf.append(key).append("=").append(value);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 打印请求参数参数
|
|
|
|
- // 记录开始时间
|
|
|
|
- long start = System.currentTimeMillis();
|
|
|
|
-
|
|
|
|
- log.info(LogFormatConstant.REQUEST_LOG_START, ip, requestURL, className, methodName, paramsBuf.toString(), headerBuf.toString());
|
|
|
|
-
|
|
|
|
- // 执行目标方法
|
|
|
|
- result = pjp.proceed();
|
|
|
|
-
|
|
|
|
- // 获取执行完的时间 打印返回报文
|
|
|
|
- log.info(LogFormatConstant.REQUEST_LOG_END, requestURL, className, methodName, result, (System.currentTimeMillis() - start));
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @Title: getIpAddr
|
|
|
|
- * @Description: 获取ip
|
|
|
|
- * @param request
|
|
|
|
- * @return
|
|
|
|
- * @return String 返回类型
|
|
|
|
- */
|
|
|
|
- public String getIpAddr(HttpServletRequest request) {
|
|
|
|
- String ipAddress = null;
|
|
|
|
- ipAddress = request.getHeader("x-forwarded-for");
|
|
|
|
- if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
|
- ipAddress = request.getHeader("Proxy-Client-IP");
|
|
|
|
- }
|
|
|
|
- if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
|
- ipAddress = request.getHeader("WL-Proxy-Client-IP");
|
|
|
|
- }
|
|
|
|
- if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
|
- ipAddress = request.getRemoteAddr();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
|
- if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
|
|
|
|
- // = 15
|
|
|
|
- if (ipAddress.indexOf(",") > 0) {
|
|
|
|
- ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // 或者这样也行,对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
|
- //return ipAddress!=null&&!"".equals(ipAddress)?ipAddress.split(",")[0]:null;
|
|
|
|
- return ipAddress;
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
|
|
+//package com.fdkankan.common.aop;
|
|
|
|
+//
|
|
|
|
+//import com.fdkankan.common.constant.LogFormatConstant;
|
|
|
|
+//import com.fdkankan.common.factory.LogFactory;
|
|
|
|
+//import java.util.Enumeration;
|
|
|
|
+//import javax.servlet.http.HttpServletRequest;
|
|
|
|
+//import lombok.extern.slf4j.Slf4j;
|
|
|
|
+//import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
+//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.slf4j.Logger;
|
|
|
|
+//import org.slf4j.LoggerFactory;
|
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
|
+//import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
+//import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
+//
|
|
|
|
+//@Component
|
|
|
|
+//@Aspect
|
|
|
|
+//@Slf4j
|
|
|
|
+//public class VisitLogInterceptor {
|
|
|
|
+//
|
|
|
|
+// // 切入点表达式
|
|
|
|
+// @Pointcut("execution(public * com.fdkankan.*..controller..*.*(..))")
|
|
|
|
+// public void privilege() {
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// @Around("privilege()")
|
|
|
|
+// public Object around(ProceedingJoinPoint pjp) throws Throwable {
|
|
|
|
+//
|
|
|
|
+// // 获取类名
|
|
|
|
+// String className = pjp.getTarget().getClass().getName();// pjp.getTarget().getClass().getSimpleName();
|
|
|
|
+// // 获取执行的方法名称
|
|
|
|
+// String methodName = pjp.getSignature().getName();
|
|
|
|
+// // 获取参数名称
|
|
|
|
+// String[] parameterNamesArgs = ((MethodSignature) pjp.getSignature()).getParameterNames();
|
|
|
|
+// // 定义返回参数
|
|
|
|
+// Object result = null;
|
|
|
|
+// // 获取方法参数
|
|
|
|
+// Object[] args = pjp.getArgs();
|
|
|
|
+// HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
|
+// // 请求的URL
|
|
|
|
+// String requestURL = request.getRequestURL().toString();
|
|
|
|
+// String ip = getIpAddr(request);
|
|
|
|
+//
|
|
|
|
+// StringBuffer paramsBuf = new StringBuffer();
|
|
|
|
+// // 获取请求参数集合并进行遍历拼接
|
|
|
|
+// for (int i = 0; i < args.length; i++) {
|
|
|
|
+// if (paramsBuf.length() > 0) {
|
|
|
|
+// paramsBuf.append("|");
|
|
|
|
+// }
|
|
|
|
+// paramsBuf.append(parameterNamesArgs[i]).append(" = ").append(args[i]);
|
|
|
|
+// }
|
|
|
|
+// StringBuffer headerBuf = new StringBuffer();
|
|
|
|
+// Enumeration<String> headerNames = request.getHeaderNames();
|
|
|
|
+// while (headerNames.hasMoreElements()) {
|
|
|
|
+// String key = (String) headerNames.nextElement();
|
|
|
|
+// String value = request.getHeader(key);
|
|
|
|
+// if (headerBuf.length() > 0) {
|
|
|
|
+// headerBuf.append("|");
|
|
|
|
+// }
|
|
|
|
+// headerBuf.append(key).append("=").append(value);
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// // 打印请求参数参数
|
|
|
|
+// // 记录开始时间
|
|
|
|
+// long start = System.currentTimeMillis();
|
|
|
|
+//
|
|
|
|
+// log.info(LogFormatConstant.REQUEST_LOG_START, ip, requestURL, className, methodName, paramsBuf.toString(), headerBuf.toString());
|
|
|
|
+//
|
|
|
|
+// // 执行目标方法
|
|
|
|
+// result = pjp.proceed();
|
|
|
|
+//
|
|
|
|
+// // 获取执行完的时间 打印返回报文
|
|
|
|
+// log.info(LogFormatConstant.REQUEST_LOG_END, requestURL, className, methodName, result, (System.currentTimeMillis() - start));
|
|
|
|
+// return result;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// /**
|
|
|
|
+// * @Title: getIpAddr
|
|
|
|
+// * @Description: 获取ip
|
|
|
|
+// * @param request
|
|
|
|
+// * @return
|
|
|
|
+// * @return String 返回类型
|
|
|
|
+// */
|
|
|
|
+// public String getIpAddr(HttpServletRequest request) {
|
|
|
|
+// String ipAddress = null;
|
|
|
|
+// ipAddress = request.getHeader("x-forwarded-for");
|
|
|
|
+// if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
|
+// ipAddress = request.getHeader("Proxy-Client-IP");
|
|
|
|
+// }
|
|
|
|
+// if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
|
+// ipAddress = request.getHeader("WL-Proxy-Client-IP");
|
|
|
|
+// }
|
|
|
|
+// if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
|
+// ipAddress = request.getRemoteAddr();
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
|
+// if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
|
|
|
|
+// // = 15
|
|
|
|
+// if (ipAddress.indexOf(",") > 0) {
|
|
|
|
+// ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// // 或者这样也行,对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
|
+// //return ipAddress!=null&&!"".equals(ipAddress)?ipAddress.split(",")[0]:null;
|
|
|
|
+// return ipAddress;
|
|
|
|
+// }
|
|
|
|
+//}
|
|
|
|
+//
|