Browse Source

fix: 登录日志

jay 2 years ago
parent
commit
aadef99a30

+ 5 - 0
iot-common/iot-common-log/src/main/java/cc/iotkit/common/log/event/LogininforEvent.java

@@ -3,6 +3,7 @@ package cc.iotkit.common.log.event;
 import cn.hutool.http.useragent.UserAgent;
 import lombok.Data;
 
+import javax.servlet.http.HttpServletRequest;
 import java.io.Serializable;
 
 /**
@@ -45,6 +46,10 @@ public class LogininforEvent implements Serializable {
      */
     private UserAgent userAgent;
 
+    /**
+     * 请求体
+     */
+    private HttpServletRequest request;
     /**
      * 其他参数
      */

+ 7 - 2
iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysLogininforServiceImpl.java

@@ -7,18 +7,21 @@ import cc.iotkit.common.log.event.LogininforEvent;
 import cc.iotkit.common.utils.MapstructUtils;
 import cc.iotkit.common.utils.StringUtils;
 import cc.iotkit.common.utils.ip.AddressUtils;
+import cc.iotkit.common.web.utils.ServletUtils;
 import cc.iotkit.data.system.ISysLogininforData;
 import cc.iotkit.model.system.SysLogininfor;
 import cc.iotkit.system.dto.bo.SysLogininforBo;
 import cc.iotkit.system.dto.vo.SysLogininforVo;
 import cc.iotkit.system.service.ISysLogininforService;
 import cn.hutool.http.useragent.UserAgent;
+import cn.hutool.http.useragent.UserAgentUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.event.EventListener;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -43,7 +46,10 @@ public class SysLogininforServiceImpl implements ISysLogininforService {
     @Async
     @EventListener
     public void recordLogininfor(LogininforEvent logininforEvent) {
-        String ip = logininforEvent.getIp();
+        HttpServletRequest request = logininforEvent.getRequest();
+        final UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
+        final String ip = ServletUtils.getClientIP();
+
         String address = AddressUtils.getRealAddressByIP(ip);
         StringBuilder s = new StringBuilder();
         s.append(getBlock(ip));
@@ -53,7 +59,6 @@ public class SysLogininforServiceImpl implements ISysLogininforService {
         s.append(getBlock(logininforEvent.getMessage()));
         // 打印信息到日志
         log.info(s.toString(), logininforEvent.getArgs());
-        UserAgent userAgent = logininforEvent.getUserAgent();
         // 获取客户端操作系统
         String os = userAgent.getOs().getName();
         // 获取客户端浏览器

+ 9 - 5
iot-starter/src/main/java/cc/iotkit/web/service/SysLoginService.java

@@ -7,16 +7,14 @@ import cc.iotkit.common.enums.LoginType;
 import cc.iotkit.common.enums.UserStatus;
 import cc.iotkit.common.exception.BizException;
 import cc.iotkit.common.exception.user.UserException;
+import cc.iotkit.common.log.event.LogininforEvent;
 import cc.iotkit.common.redis.utils.RedisUtils;
 import cc.iotkit.common.satoken.utils.LoginHelper;
 import cc.iotkit.common.tenant.helper.TenantHelper;
 import cc.iotkit.common.undefined.LoginUser;
 import cc.iotkit.common.undefined.RoleDTO;
 import cc.iotkit.common.undefined.XcxLoginUser;
-import cc.iotkit.common.utils.DateUtils;
-import cc.iotkit.common.utils.MapstructUtils;
-import cc.iotkit.common.utils.MessageUtils;
-import cc.iotkit.common.utils.StringUtils;
+import cc.iotkit.common.utils.*;
 import cc.iotkit.common.web.config.properties.CaptchaProperties;
 import cc.iotkit.common.web.utils.ServletUtils;
 import cc.iotkit.data.system.ISysUserData;
@@ -173,7 +171,13 @@ public class SysLoginService {
      * @param message  消息内容
      */
     private void recordLogininfor(String tenantId, String username, String status, String message) {
-
+        LogininforEvent logininforEvent = new LogininforEvent();
+        logininforEvent.setTenantId(tenantId);
+        logininforEvent.setUsername(username);
+        logininforEvent.setStatus(status);
+        logininforEvent.setMessage(message);
+        logininforEvent.setRequest(ServletUtils.getRequest());
+        SpringUtils.context().publishEvent(logininforEvent);
     }
 
     /**