fix: 登录日志

V0.5.x
jay 2023-06-14 11:18:54 +08:00
parent 071ce2adbe
commit aadef99a30
3 changed files with 21 additions and 7 deletions

View File

@ -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;
/**
*
*/

View File

@ -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();
// 获取客户端浏览器

View File

@ -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);
}
/**