1、恢复operationlog;2、登录性能优化;

This commit is contained in:
gaoqr
2025-07-29 18:26:25 +08:00
parent d3df61c122
commit 47919880d1
4 changed files with 49 additions and 29 deletions
@@ -1 +1,2 @@
com.cf.imes.framework.operatelog.config.ChenfengOperateLogRpcAutoConfiguration com.cf.imes.framework.operatelog.config.ChenfengOperateLogRpcAutoConfiguration
com.cf.imes.framework.operatelog.config.ChenfengOperateLogAutoConfiguration
@@ -31,12 +31,12 @@ public class OperateLogDO extends BaseDO {
/** /**
* {@link #javaMethodArgs} 的最大长度 * {@link #javaMethodArgs} 的最大长度
*/ */
public static final Integer JAVA_METHOD_ARGS_MAX_LENGTH = 8000; public static final Integer JAVA_METHOD_ARGS_MAX_LENGTH = 1000;
/** /**
* {@link #resultData} 的最大长度 * {@link #resultData} 的最大长度
*/ */
public static final Integer RESULT_MAX_LENGTH = 4000; public static final Integer RESULT_MAX_LENGTH = 1000;
/** /**
* 日志主键 * 日志主键
@@ -12,6 +12,8 @@ import com.cf.imes.framework.common.util.servlet.ServletUtils;
import com.cf.imes.framework.common.util.validation.ValidationUtils; import com.cf.imes.framework.common.util.validation.ValidationUtils;
import com.cf.imes.framework.ip.core.service.IPQueryService; import com.cf.imes.framework.ip.core.service.IPQueryService;
import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO; import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO;
import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.framework.organ.core.service.OrganFrameworkService;
import com.cf.imes.module.system.api.logger.dto.LoginLogCreateReqDTO; import com.cf.imes.module.system.api.logger.dto.LoginLogCreateReqDTO;
import com.cf.imes.module.system.api.sms.SmsCodeApi; import com.cf.imes.module.system.api.sms.SmsCodeApi;
import com.cf.imes.module.system.api.social.dto.SocialUserRespDTO; import com.cf.imes.module.system.api.social.dto.SocialUserRespDTO;
@@ -29,7 +31,6 @@ import com.cf.imes.module.system.enums.logger.LoginLogTypeEnum;
import com.cf.imes.module.system.enums.logger.LoginResultEnum; import com.cf.imes.module.system.enums.logger.LoginResultEnum;
import com.cf.imes.module.system.enums.oauth2.OAuth2ClientConstants; import com.cf.imes.module.system.enums.oauth2.OAuth2ClientConstants;
import com.cf.imes.module.system.enums.sms.SmsSceneEnum; import com.cf.imes.module.system.enums.sms.SmsSceneEnum;
import com.cf.imes.module.system.service.dept.DeptService;
import com.cf.imes.module.system.service.logger.LoginLogService; import com.cf.imes.module.system.service.logger.LoginLogService;
import com.cf.imes.module.system.service.member.MemberService; import com.cf.imes.module.system.service.member.MemberService;
import com.cf.imes.module.system.service.oauth2.OAuth2TokenService; import com.cf.imes.module.system.service.oauth2.OAuth2TokenService;
@@ -41,12 +42,15 @@ import com.cf.imes.module.system.enums.ErrorCodeConstants;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.validation.Validator; import jakarta.validation.Validator;
import org.springframework.transaction.support.TransactionTemplate;
import java.util.Objects; import java.util.Objects;
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception; import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -82,7 +86,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
@Resource @Resource
private OrganService organService; private OrganService organService;
@Resource @Resource
private DeptService deptService; private OrganFrameworkService organFrameworkService;
@Resource @Resource
private SmsCodeService smsCodeService; private SmsCodeService smsCodeService;
@@ -90,6 +94,9 @@ public class AdminAuthServiceImpl implements AdminAuthService {
@Resource @Resource
private IPQueryService ipQueryService; private IPQueryService ipQueryService;
@Autowired
private TransactionTemplate transactionTemplate;
/** /**
* 验证码的开关,默认为 true * 验证码的开关,默认为 true
*/ */
@@ -116,9 +123,10 @@ public class AdminAuthServiceImpl implements AdminAuthService {
} }
Long organId = user.getOrganId(); Long organId = user.getOrganId();
// 校验机构有效性 // 校验机构有效性
organService.validOrgan(organId); organFrameworkService.validOrgan(organId);
// 校验部门有效性 // 校验部门有效性
deptService.validUserLoginDept(user.getDeptId()); OrganContextHolder.setOrganId(organId);
organFrameworkService.validDept(user.getDeptId());
// 校验密码 // 校验密码
if (!userService.isPasswordMatch(password, user.getPassword())) { if (!userService.isPasswordMatch(password, user.getPassword())) {
createLoginLog(user.getId(), username, organId, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS); createLoginLog(user.getId(), username, organId, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
@@ -222,28 +230,36 @@ public class AdminAuthServiceImpl implements AdminAuthService {
private void createLoginLog(Long userId, String username, Long organId, private void createLoginLog(Long userId, String username, Long organId,
LoginLogTypeEnum logTypeEnum, LoginResultEnum loginResult) { LoginLogTypeEnum logTypeEnum, LoginResultEnum loginResult) {
String clientIP = getClientIP(); // 手动控制事务
// 插入登录日志 transactionTemplate.executeWithoutResult(status -> {
LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO(); try {
reqDTO.setLogType(logTypeEnum.getType()); String clientIP = getClientIP();
reqDTO.setTraceId(TracerUtils.getTraceId()); // 插入登录日志
reqDTO.setUserId(userId); LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO();
reqDTO.setUserType(getUserType().getValue()); reqDTO.setLogType(logTypeEnum.getType());
reqDTO.setUsername(username); reqDTO.setTraceId(TracerUtils.getTraceId());
reqDTO.setUserAgent(ServletUtils.getUserAgent()); reqDTO.setUserId(userId);
reqDTO.setUserIp(clientIP); reqDTO.setUserType(getUserType().getValue());
reqDTO.setResult(loginResult.getResult()); reqDTO.setUsername(username);
reqDTO.setOrganId(organId); reqDTO.setUserAgent(ServletUtils.getUserAgent());
// 设置登录时通过ip云服务查询到的地域信息 reqDTO.setUserIp(clientIP);
if (ipQueryService.serviceEnable()) { reqDTO.setResult(loginResult.getResult());
IPQueryDataRespDTO ipQueryDataRespDTO = ipQueryService.querySource(clientIP); reqDTO.setOrganId(organId);
reqDTO.setRegion(StringUtils.join(ipQueryDataRespDTO.getProv(), ipQueryDataRespDTO.getCity(), ipQueryDataRespDTO.getArea())); // 设置登录时通过ip云服务查询到的地域信息
} if (ipQueryService.serviceEnable()) {
loginLogService.createLoginLog(reqDTO); IPQueryDataRespDTO ipQueryDataRespDTO = ipQueryService.querySource(clientIP);
// 更新最后登录时间 reqDTO.setRegion(StringUtils.join(ipQueryDataRespDTO.getProv(), ipQueryDataRespDTO.getCity(), ipQueryDataRespDTO.getArea()));
if (userId != null && Objects.equals(LoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) { }
userService.updateUserLogin(userId, clientIP); loginLogService.createLoginLog(reqDTO);
} // 更新最后登录时间
if (userId != null && Objects.equals(LoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) {
userService.updateUserLogin(userId, clientIP);
}
} catch (Exception e) {
status.setRollbackOnly(); // 显式标记事务回滚
log.error("login log record error", e);
}
});
} }
@Override @Override
@@ -254,7 +254,10 @@ public class AdminUserServiceImpl implements AdminUserService {
@Override @Override
public void updateUserLogin(Long id, String loginIp) { public void updateUserLogin(Long id, String loginIp) {
userMapper.updateById(new AdminUserDO().setId(id).setLoginIp(loginIp).setLoginDate(LocalDateTime.now())); userMapper.update(new LambdaUpdateWrapper<AdminUserDO>()
.eq(AdminUserDO::getId, id)
.set(AdminUserDO::getLoginIp, loginIp)
.set(AdminUserDO::getLoginDate, LocalDateTime.now()));
} }
@Override @Override