فهرست منبع

Merge remote-tracking branch 'origin/V1' into V1

xiao547607 5 سال پیش
والد
کامیت
419bd95dc7

+ 60 - 0
common/src/main/java/com/jpsoft/smart/modules/common/utils/HttpUtil.java

@@ -0,0 +1,60 @@
+package com.jpsoft.smart.modules.common.utils;
+
+import lombok.extern.slf4j.Slf4j;
+
+import javax.servlet.http.HttpServletRequest;
+
+@Slf4j
+public class HttpUtil {
+    public final static String getIpAddress(HttpServletRequest request){
+        // 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址  
+
+        String ip = request.getHeader("X-Forwarded-For");
+        if (log.isInfoEnabled()) {
+            log.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip);
+        }
+
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+                ip = request.getHeader("Proxy-Client-IP");
+                if (log.isInfoEnabled()) {
+                    log.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip);
+                }
+            }
+            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+                ip = request.getHeader("WL-Proxy-Client-IP");
+                if (log.isInfoEnabled()) {
+                    log.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip);
+                }
+            }
+            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+                ip = request.getHeader("HTTP_CLIENT_IP");
+                if (log.isInfoEnabled()) {
+                    log.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip);
+                }
+            }
+            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
+                if (log.isInfoEnabled()) {
+                    log.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" + ip);
+                }
+            }
+            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+                ip = request.getRemoteAddr();
+                if (log.isInfoEnabled()) {
+                    log.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip);
+                }
+            }
+        } else if (ip.length() > 15) {
+            String[] ips = ip.split(",");
+            for (int index = 0; index < ips.length; index++) {
+                String strIp = (String) ips[index];
+                if (!("unknown".equalsIgnoreCase(strIp))) {
+                    ip = strIp;
+                    break;
+                }
+            }
+        }
+        return ip;
+    }
+}

+ 1 - 1
lapi/src/main/java/com/jpsoft/smart/lapi/handler/HeartReportHandler.java

@@ -54,7 +54,7 @@ public class HeartReportHandler extends SimpleChannelInboundHandler<HeartReportI
         else{
             int minutes = Minutes.minutesBetween(lastHeartTime,DateTime.now()).getMinutes();
 
-            if(minutes>=1){
+            if(minutes>=10){
                 ctx.channel().attr(LapiAttrKeys.LAST_HEART_TIME).set(DateTime.now());
                 needWrite = true;
             }

+ 48 - 2
web/src/main/java/com/jpsoft/smart/advice/PermissionAdvice.java

@@ -1,7 +1,11 @@
 package com.jpsoft.smart.advice;
 
+import cn.hutool.core.date.DateTime;
 import com.jpsoft.smart.modules.common.dto.MessageResult;
+import com.jpsoft.smart.modules.common.utils.HttpUtil;
+import com.jpsoft.smart.modules.sys.entity.SysLog;
 import com.jpsoft.smart.modules.sys.service.PermissionService;
+import com.jpsoft.smart.modules.sys.service.SysLogService;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
@@ -14,15 +18,18 @@ import org.springframework.stereotype.Component;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
-
 import javax.servlet.http.HttpServletRequest;
 import java.lang.reflect.Method;
+import java.util.Date;
 
 @Aspect
 @Component
 public class PermissionAdvice {
     private  Logger logger= LoggerFactory.getLogger("root");
 
+    @Autowired
+    private SysLogService sysLogService;
+
     @Autowired
     private PermissionService permissionService;
 
@@ -72,13 +79,42 @@ public class PermissionAdvice {
         ServletRequestAttributes requestAttrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
 
         Object obj = null;
+        SysLog sysLog = new SysLog();
 
         if(requestAttrs!=null) {
             HttpServletRequest request = requestAttrs.getRequest();
-            logger.warn("访问地址:" + request.getRequestURL().toString());
 
+            sysLog.setRemoteIp(HttpUtil.getIpAddress(request));
+            sysLog.setUrl(request.getRequestURI());
+
+            logger.warn("访问地址:" + request.getRequestURL().toString());
             logger.warn("path=" + pathBuilder.toString());
 
+            //1.这里获取到所有的参数值的数组
+            Object[] args = point.getArgs();
+            String[] parameterNames = methodSignature.getParameterNames();
+
+            StringBuilder argBuilder = new StringBuilder();
+
+            for (int i = 0;i<Math.min(args.length, parameterNames.length);i++) {
+                if (argBuilder.length()!=0){
+                    argBuilder.append("&");
+                }
+
+                if(args[i] != null) {
+                    String value = args[i].toString();
+                    value = value.length() > 100 ? value.substring(0, 100) : value;
+
+                    if (parameterNames[i].equals("password")){
+                        value = "******";
+                    }
+
+                    argBuilder.append(parameterNames[i] + "=" + value);
+                }
+            }
+
+            sysLog.setData(argBuilder.toString());
+
             String userId = (String)request.getAttribute("subject");
 
             boolean existed = permissionService.exist(pathBuilder.toString(), request.getMethod());
@@ -116,6 +152,16 @@ public class PermissionAdvice {
         System.out.println(classType);
         logger.warn(String.format("调用类%s方法%s耗时%s毫秒",clazzSimpleName,methodName,elapse));
 
+        if(elapse>3000) {
+            //执行时间超过3秒则记录数据库
+            sysLog.setElapse(elapse);
+            sysLog.setPointcut(clazzSimpleName + "->" + methodName);
+            sysLog.setRemark("方法耗时统计");
+            sysLog.setCreateTime(new Date());
+
+            sysLogService.insert(sysLog);
+        }
+
         return obj;
     }
 }