84 lines
3.3 KiB
Java
84 lines
3.3 KiB
Java
|
|
package com.yupi.springbootinit.service.impl;
|
||
|
|
|
||
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||
|
|
import com.yupi.springbootinit.mapper.SystemLoginLogMapper;
|
||
|
|
import com.yupi.springbootinit.model.dto.user.SystemUsersDTO;
|
||
|
|
import com.yupi.springbootinit.model.entity.SystemLoginLog;
|
||
|
|
import com.yupi.springbootinit.model.vo.user.SystemUsersVO;
|
||
|
|
import com.yupi.springbootinit.service.SystemLoginLogService;
|
||
|
|
import com.yupi.springbootinit.utils.NetUtils;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
||
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||
|
|
|
||
|
|
import javax.servlet.http.HttpServletRequest;
|
||
|
|
import java.util.Date;
|
||
|
|
import java.util.UUID;
|
||
|
|
/*
|
||
|
|
* @author: ziin
|
||
|
|
* @date: 2026/3/10 11:32
|
||
|
|
*/
|
||
|
|
|
||
|
|
@Service
|
||
|
|
public class SystemLoginLogServiceImpl extends ServiceImpl<SystemLoginLogMapper, SystemLoginLog> implements SystemLoginLogService{
|
||
|
|
|
||
|
|
private static final Long HOST_LOGIN_LOG_TYPE = 1L;
|
||
|
|
private static final Byte LOGIN_SUCCESS = 1;
|
||
|
|
private static final Byte LOGIN_FAILURE = 0;
|
||
|
|
private static final String USER_AGENT_HEADER = "User-Agent";
|
||
|
|
private static final String TRACE_ID_HEADER = "X-Trace-Id";
|
||
|
|
private static final String SYSTEM_OPERATOR = "system";
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void recordDoLoginLog(SystemUsersDTO usersDTO, SystemUsersVO usersVO, boolean success) {
|
||
|
|
HttpServletRequest request = getCurrentRequest();
|
||
|
|
Date now = new Date();
|
||
|
|
SystemLoginLog loginLog = new SystemLoginLog();
|
||
|
|
loginLog.setLogType(HOST_LOGIN_LOG_TYPE);
|
||
|
|
loginLog.setTraceId(resolveTraceId(request));
|
||
|
|
loginLog.setUserId(usersVO == null ? null : usersVO.getId());
|
||
|
|
loginLog.setUsername(resolveUsername(usersDTO, usersVO));
|
||
|
|
loginLog.setResult(success ? LOGIN_SUCCESS : LOGIN_FAILURE);
|
||
|
|
loginLog.setUserIp(NetUtils.getIpAddress(request));
|
||
|
|
loginLog.setUserAgent(request.getHeader(USER_AGENT_HEADER));
|
||
|
|
loginLog.setCreator(SYSTEM_OPERATOR);
|
||
|
|
loginLog.setCreateTime(now);
|
||
|
|
loginLog.setUpdater(SYSTEM_OPERATOR);
|
||
|
|
loginLog.setUpdateTime(now);
|
||
|
|
loginLog.setDeleted(Boolean.FALSE);
|
||
|
|
loginLog.setTenantId(resolveTenantId(usersDTO, usersVO));
|
||
|
|
boolean saved = save(loginLog);
|
||
|
|
if (!saved) {
|
||
|
|
throw new IllegalStateException("保存 doLogin 登录日志失败");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private HttpServletRequest getCurrentRequest() {
|
||
|
|
ServletRequestAttributes attributes =
|
||
|
|
(ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
|
||
|
|
return attributes.getRequest();
|
||
|
|
}
|
||
|
|
|
||
|
|
private String resolveTraceId(HttpServletRequest request) {
|
||
|
|
String traceId = request.getHeader(TRACE_ID_HEADER);
|
||
|
|
if (traceId == null || traceId.isBlank()) {
|
||
|
|
return UUID.randomUUID().toString();
|
||
|
|
}
|
||
|
|
return traceId;
|
||
|
|
}
|
||
|
|
|
||
|
|
private String resolveUsername(SystemUsersDTO usersDTO, SystemUsersVO usersVO) {
|
||
|
|
if (usersVO != null && usersVO.getUsername() != null) {
|
||
|
|
return usersVO.getUsername();
|
||
|
|
}
|
||
|
|
return usersDTO.getUsername();
|
||
|
|
}
|
||
|
|
|
||
|
|
private Long resolveTenantId(SystemUsersDTO usersDTO, SystemUsersVO usersVO) {
|
||
|
|
if (usersVO != null && usersVO.getTenantId() != null) {
|
||
|
|
return usersVO.getTenantId();
|
||
|
|
}
|
||
|
|
return usersDTO.getTenantId();
|
||
|
|
}
|
||
|
|
}
|