|
@@ -0,0 +1,132 @@
|
|
|
|
+package com.fdkankan.manage.aop;
|
|
|
|
+
|
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import cn.hutool.extra.servlet.ServletUtil;
|
|
|
|
+import cn.hutool.http.ContentType;
|
|
|
|
+import cn.hutool.http.useragent.UserAgent;
|
|
|
|
+import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.fdkankan.common.constant.ServerCode;
|
|
|
|
+import com.fdkankan.manage.config.SaTokenConfigure;
|
|
|
|
+import com.fdkankan.manage.entity.OperLog;
|
|
|
|
+import com.fdkankan.manage.entity.SysUser;
|
|
|
|
+import com.fdkankan.manage.service.ISysUserService;
|
|
|
|
+import com.fdkankan.redis.util.RedisUtil;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
|
+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.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.util.*;
|
|
|
|
+
|
|
|
|
+@Component
|
|
|
|
+@Aspect
|
|
|
|
+@Slf4j
|
|
|
|
+public class InnerVisitLogInterceptor {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisUtil redisUtil;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ISysUserService userService;
|
|
|
|
+
|
|
|
|
+ @Value("${server.servlet.context-path:null}")
|
|
|
|
+ private String contextPath;
|
|
|
|
+
|
|
|
|
+ // 切入点表达式
|
|
|
|
+ @Pointcut("execution(public * com.fdkankan.manage.inner.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);
|
|
|
|
+
|
|
|
|
+ StringBuilder paramsBuf = new StringBuilder();
|
|
|
|
+ // 获取请求参数集合并进行遍历拼接
|
|
|
|
+ for (int i = 0; i < args.length; i++) {
|
|
|
|
+ if (paramsBuf.length() > 0) {
|
|
|
|
+ paramsBuf.append("|");
|
|
|
|
+ }
|
|
|
|
+ paramsBuf.append(parameterNamesArgs[i]).append(" = ").append(args[i]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 打印请求参数参数
|
|
|
|
+ // 记录开始时间
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ log.warn("请求| ip:{} , 请求接口:{} ,请求时间:{}, 参数:{} , 请求token:{} ",
|
|
|
|
+ ip, requestURL, start,paramsBuf.toString(), request.getHeader("token"));
|
|
|
|
+ // 执行目标方法
|
|
|
|
+ result = pjp.proceed();
|
|
|
|
+ // 获取执行完的时间 打印返回报文
|
|
|
|
+ log.warn("返回| 请求接口:{} , 请求时间:{} , 处理时间:{} 毫秒 , 返回结果 :{}",
|
|
|
|
+ requestURL, start, (System.currentTimeMillis() - start), result);
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|