手机号登录
This commit is contained in:
@@ -10,6 +10,8 @@ import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
||||
import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi;
|
||||
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
|
||||
@@ -227,22 +229,51 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
private AuthLoginRespVO createTokenAfterLoginSuccess(Long userId, String username, LoginLogTypeEnum logType) {
|
||||
// 插入登陆日志
|
||||
createLoginLog(userId, username, logType, LoginResultEnum.SUCCESS);
|
||||
|
||||
// 确保租户上下文中有正确的租户ID
|
||||
ensureTenantContext(userId);
|
||||
|
||||
// 创建访问令牌
|
||||
OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.createAccessToken(userId, getUserType().getValue(),
|
||||
OAuth2ClientConstants.CLIENT_ID_DEFAULT, null);
|
||||
|
||||
// 获取用户信息并设置租户ID
|
||||
AdminUserDO user = userService.getUser(userId);
|
||||
if (user != null && user.getTenantId() != null) {
|
||||
accessTokenDO.setTenantId(user.getTenantId());
|
||||
} else {
|
||||
log.warn("[createTokenAfterLoginSuccess][用户({}) 租户ID为空,可能导致登录后无法获取租户信息]", userId);
|
||||
}
|
||||
|
||||
// 构建返回结果
|
||||
return AuthConvert.INSTANCE.convert(accessTokenDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保租户上下文中有正确的租户ID
|
||||
*/
|
||||
private void ensureTenantContext(Long userId) {
|
||||
// 如果TenantContextHolder中没有租户ID,尝试从用户信息中获取并设置
|
||||
Long currentTenantId = TenantContextHolder.getTenantId();
|
||||
if (currentTenantId == null) {
|
||||
// 尝试从请求头中获取
|
||||
try {
|
||||
currentTenantId = WebFrameworkUtils.getTenantId(ServletUtils.getRequest());
|
||||
} catch (Exception e) {
|
||||
log.debug("[ensureTenantContext][无法获取当前请求,跳过从请求头提取租户ID]");
|
||||
}
|
||||
|
||||
// 如果还是为空,从用户信息中获取
|
||||
if (currentTenantId == null) {
|
||||
AdminUserDO user = userService.getUser(userId);
|
||||
if (user != null && user.getTenantId() != null) {
|
||||
currentTenantId = user.getTenantId();
|
||||
log.info("[ensureTenantContext][从用户({})信息中获取租户ID: {}]", userId, currentTenantId);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置到租户上下文中
|
||||
if (currentTenantId != null) {
|
||||
TenantContextHolder.setTenantId(currentTenantId);
|
||||
log.info("[ensureTenantContext][设置租户上下文: {}]", currentTenantId);
|
||||
} else {
|
||||
log.warn("[ensureTenantContext][用户({}) 无法获取租户ID,可能导致登录后无法获取租户信息]", userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthLoginRespVO refreshToken(String refreshToken) {
|
||||
OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.refreshAccessToken(refreshToken, OAuth2ClientConstants.CLIENT_ID_DEFAULT);
|
||||
@@ -364,9 +395,31 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 检查用户租户ID,如果为空则尝试修复
|
||||
if (user.getTenantId() == null) {
|
||||
Long currentTenantId = TenantContextHolder.getTenantId();
|
||||
// 如果TenantContextHolder中没有,尝试从请求头中获取
|
||||
if (currentTenantId == null) {
|
||||
try {
|
||||
currentTenantId = WebFrameworkUtils.getTenantId(ServletUtils.getRequest());
|
||||
} catch (Exception e) {
|
||||
log.debug("[getPhoneNumber][无法获取当前请求,跳过从请求头提取租户ID]");
|
||||
}
|
||||
}
|
||||
|
||||
if (currentTenantId != null) {
|
||||
log.warn("[getPhoneNumber][用户({}) 租户ID为空,使用当前租户ID: {}]", user.getId(), currentTenantId);
|
||||
user.setTenantId(currentTenantId);
|
||||
userService.updateByid(user);
|
||||
} else {
|
||||
log.error("[getPhoneNumber][用户({}) 租户ID为空,且无法获取当前租户ID]", user.getId());
|
||||
}
|
||||
}
|
||||
|
||||
user.setOpenid(reqVO.getOpenid());
|
||||
userService.updateByid(user);
|
||||
AuthLoginRespVO tokenAfterLoginSuccess = createTokenAfterLoginSuccess(user.getId(), user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL);
|
||||
AuthLoginRespVO tokenAfterLoginSuccess = createTokenAfterLoginSuccess(user.getId(), user.getUsername(), LoginLogTypeEnum.LOGIN_MOBILE);
|
||||
return tokenAfterLoginSuccess;
|
||||
} catch (Exception e) {
|
||||
log.error("[getPhoneNumber][解密手机号失败,encryptedData: {}, sessionKey: {}, iv: {}]",
|
||||
@@ -401,8 +454,30 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
if (user == null) {
|
||||
throw exception(USER_NOT_EXISTS);
|
||||
}
|
||||
|
||||
// 检查用户租户ID,如果为空则尝试修复
|
||||
if (user.getTenantId() == null) {
|
||||
Long currentTenantId = TenantContextHolder.getTenantId();
|
||||
// 如果TenantContextHolder中没有,尝试从请求头中获取
|
||||
if (currentTenantId == null) {
|
||||
try {
|
||||
currentTenantId = WebFrameworkUtils.getTenantId(ServletUtils.getRequest());
|
||||
} catch (Exception e) {
|
||||
log.debug("[getUserInfo][无法获取当前请求,跳过从请求头提取租户ID]");
|
||||
}
|
||||
}
|
||||
|
||||
if (currentTenantId != null) {
|
||||
log.warn("[getUserInfo][用户({}) 租户ID为空,使用当前租户ID: {}]", user.getId(), currentTenantId);
|
||||
user.setTenantId(currentTenantId);
|
||||
userService.updateByid(user);
|
||||
} else {
|
||||
log.error("[getUserInfo][用户({}) 租户ID为空,且无法获取当前租户ID]", user.getId());
|
||||
}
|
||||
}
|
||||
|
||||
// 创建 Token 令牌,记录登录日志
|
||||
return createTokenAfterLoginSuccess(user.getId(), reqVO.getPhone(), LoginLogTypeEnum.LOGIN_USERNAME);
|
||||
return createTokenAfterLoginSuccess(user.getId(), reqVO.getPhone(), LoginLogTypeEnum.LOGIN_MOBILE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user