fix(auth): login existing account during registration

This commit is contained in:
hy001 2026-05-08 11:16:46 +08:00
parent 895193f33c
commit 45cb56b45a
4 changed files with 121 additions and 40 deletions

View File

@ -6,10 +6,12 @@ import com.red.circle.auth.dto.TokenCredentialCO;
import com.red.circle.auth.response.AuthErrorCode;
import com.red.circle.auth.storage.RedCircleCredentialService;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.framework.core.request.RequestClientEnum;
import com.red.circle.framework.core.security.UserCredential;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.framework.core.exception.ResponseException;
import com.red.circle.framework.core.request.RequestClientEnum;
import com.red.circle.framework.core.response.ResponseErrorCode;
import com.red.circle.framework.core.security.UserCredential;
import com.red.circle.framework.web.spring.ApplicationRequestUtils;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.endpoint.sys.SysCountryCodeClient;
@ -29,10 +31,11 @@ import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@ -141,15 +144,22 @@ public class EndpointRestController {
@PostMapping("/account/create")
public TokenCredentialCO createAccount(@RequestBody @Validated CreateAccountCmd cmd, HttpServletRequest request) {
log.warn("cmd {}", cmd);
if (cmd.getType().equalsIgnoreCase(AuthTypeEnum.IMEI.getKey())&& cmd.getReqClient().isAndroid()) {
//设备禁止登录
ResponseAssert.failure(AuthErrorCode.AUTH_DEVICE_NOT_SUPPORT);
if (cmd.getType().equalsIgnoreCase(AuthTypeEnum.IMEI.getKey())&& cmd.getReqClient().isAndroid()) {
//设备禁止登录
ResponseAssert.failure(AuthErrorCode.AUTH_DEVICE_NOT_SUPPORT);
}
UserAccountDTO account = ResponseAssert.requiredSuccess(appUserAccountClient.create(cmd));
ResponseAssert.notNull(UserErrorCode.REGISTRATION_FAILED, account);
TokenCredentialCO tokenCredentialCO = processToken.createUserCredential(account);
return submitLoginAccessValidation(tokenCredentialCO, cmd.requireReqSysOrigin(), cmd.getReqZoneId(), request);
}
try {
UserAccountDTO account = ResponseAssert.requiredSuccess(appUserAccountClient.create(cmd));
return createTokenCredential(account, cmd.requireReqSysOrigin(), cmd.getReqZoneId(), request);
} catch (ResponseException ex) {
if (!isRegisteredError(ex)) {
throw ex;
}
log.warn("account create already registered, fallback login. type={}, errorCode={}",
cmd.getType(), ex.getErrorCode());
return loginRegisteredAccount(cmd, request);
}
}
/**
* App用户注册.
@ -232,13 +242,62 @@ public class EndpointRestController {
/**
* 账号登录.
*/
@PostMapping(value = "/account/losgin")
@PostMapping(value = "/account/losgin")
public TokenCredentialCO accountLogin1(@RequestBody @Validated AccountLoginCmd cmd, HttpServletRequest request) {
TokenCredentialCO tokenCredentialCO = processToken.createUserCredential(
ResponseAssert.requiredSuccess(appUserAccountClient.accountLogin(cmd)));
return submitLoginAccessValidation(tokenCredentialCO, cmd.requireReqSysOrigin(), cmd.getReqZoneId(), request);
}
private TokenCredentialCO loginRegisteredAccount(CreateAccountCmd cmd, HttpServletRequest request) {
if (cmd.checkTypeMobile()) {
MobileCredentialCmd loginCmd = new MobileCredentialCmd();
BeanUtils.copyProperties(cmd, loginCmd);
ResponseAssert.notNull(ResponseErrorCode.REQUEST_PARAMETER_ERROR, cmd.getMobile());
loginCmd.setPhonePrefix(cmd.getMobile().getPhonePrefix());
loginCmd.setPhoneNumber(cmd.getMobile().getPhoneNumber());
loginCmd.setPwd(cmd.getMobile().getPwd());
if (Objects.equals(loginCmd.getPhoneNumber(), "13684965043")) {
loginCmd.setPhonePrefix(86);
}
return createTokenCredential(
ResponseAssert.requiredSuccess(appUserAccountClient.mobileCredential(loginCmd)),
loginCmd.requireReqSysOrigin(),
loginCmd.getReqZoneId(),
request
);
}
AuthTypeEnum authType = AuthTypeEnum.toEnum(cmd.getType());
ResponseAssert.notNull(UserErrorCode.AUTH_TYPE_NOT_SUPPORTED, authType);
UserChannelCredentialCmd loginCmd = new UserChannelCredentialCmd();
BeanUtils.copyProperties(cmd, loginCmd);
loginCmd.setAuthType(authType);
loginCmd.setOpenId(cmd.getOpenId());
return createTokenCredential(
ResponseAssert.requiredSuccess(appUserAccountClient.channelCredential(loginCmd)),
loginCmd.requireReqSysOrigin(),
loginCmd.getReqZoneId(),
request
);
}
private boolean isRegisteredError(ResponseException ex) {
return Objects.equals(ex.getErrorCode(), UserErrorCode.USER_REGISTERED.getCode())
|| Objects.equals(ex.getErrorCode(), UserErrorCode.SOURCE_REGISTERED.getCode());
}
private TokenCredentialCO createTokenCredential(
UserAccountDTO account,
String reqSysOrigin,
String reqZoneId,
HttpServletRequest request
) {
ResponseAssert.notNull(UserErrorCode.REGISTRATION_FAILED, account);
TokenCredentialCO tokenCredentialCO = processToken.createUserCredential(account);
return submitLoginAccessValidation(tokenCredentialCO, reqSysOrigin, reqZoneId, request);
}
private TokenCredentialCO submitLoginAccessValidation(
TokenCredentialCO tokenCredentialCO,
String reqSysOrigin,

View File

@ -17,11 +17,11 @@ import com.red.circle.other.infra.database.rds.service.user.device.ArchiveDevice
import com.red.circle.other.infra.database.rds.service.user.user.AuthTypeService;
import com.red.circle.other.inner.asserts.DynamicErrorCode;
import com.red.circle.other.inner.model.cmd.user.UserProfileCmd;
import com.red.circle.tool.core.phone.PhoneNumberUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.model.cmd.user.account.CreateAccountCmd;
import com.red.circle.tool.core.phone.PhoneNumberUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.model.cmd.user.account.CreateAccountCmd;
import com.red.circle.other.inner.model.cmd.user.account.MobileAuthCmd;
import com.red.circle.other.inner.model.cmd.user.device.AllowDeviceRegisterCmd;
import com.red.circle.other.inner.model.dto.user.account.UserAccountDTO;
@ -48,11 +48,11 @@ public class CreateAccountAspect {
private final EnvProperties evnProperties;
private final AuthTypeService authTypeService;
private final ArchiveDeviceService archiveDeviceService;
private final RegisterDeviceGateway registerDeviceGateway;
private final RegisterRewardStreamProducer registerRewardStreamProducer;
private final CreateAccountExpandProcess createAccountExpandProcess;
private final CheckRegisterUserProfileProcess checkRegisterUserProfileProcess;
private final ArchiveDeviceService archiveDeviceService;
private final RegisterDeviceGateway registerDeviceGateway;
private final RegisterRewardStreamProducer registerRewardStreamProducer;
private final CreateAccountExpandProcess createAccountExpandProcess;
private final CheckRegisterUserProfileProcess checkRegisterUserProfileProcess;
@Pointcut("@annotation(com.red.circle.other.infra.annotation.CreateAccount)")
public void userRegisterAspectPointCut() {
@ -70,23 +70,40 @@ public class CreateAccountAspect {
ResultResponse<UserAccountDTO> result = (ResultResponse<UserAccountDTO>) joinPoint.proceed();
log.warn("cmd {} result.getBody {}", cmd, result.getBody());
if (Objects.isNull(cmd.getProfile())) {
UserProfileCmd userProfile = new UserProfileCmd();
userProfile.setCountryId(result.getBody().getUserProfile().getCountryId());
cmd.setProfile(userProfile);
}
if (result.checkSuccess() && Objects.nonNull(result.getBody())) {
registerRewardStreamProducer.publish(
SysOriginPlatformEnum.valueOf(cmd.requireReqSysOrigin()),
cmd.requiredReqUserId(),
cmd.getProfile().getCountryId()
);
}
createAccountExpandProcess.process(cmd);
if (result.checkSuccess() && Objects.nonNull(result.getBody())) {
if (Objects.isNull(cmd.getProfile())) {
UserProfileCmd userProfile = new UserProfileCmd();
userProfile.setCountryId(result.getBody().getUserProfile().getCountryId());
cmd.setProfile(userProfile);
}
publishRegisterReward(cmd);
processRegisterExpand(cmd);
}
return result;
}
private void publishRegisterReward(CreateAccountCmd cmd) {
try {
registerRewardStreamProducer.publish(
SysOriginPlatformEnum.valueOf(cmd.requireReqSysOrigin()),
cmd.requiredReqUserId(),
cmd.getProfile().getCountryId()
);
} catch (Exception ex) {
log.error("register reward publish failed, userId={}, openId={}",
cmd.getReqUserId(), cmd.registerOpenId(), ex);
}
}
private void processRegisterExpand(CreateAccountCmd cmd) {
try {
createAccountExpandProcess.process(cmd);
} catch (Exception ex) {
log.error("register expand process failed, userId={}, openId={}",
cmd.getReqUserId(), cmd.registerOpenId(), ex);
}
}
private void generateUserId(CreateAccountCmd cmd) {
cmd.setReqUserId(IdWorkerUtils.getId());
}

View File

@ -91,6 +91,7 @@ public class UserAccountCommon {
ResponseAssert.notNull(UserErrorCode.AUTH_TYPE_NOT_SUPPORTED, AuthTypeEnum.toEnum(cmd.getType()));
BaseInfo baseInfo = toBaseInfo(cmd);
String userSig = requestUserSign(baseInfo.getId());
try {
// 客户端时区判断问题出现了新用户注册就被冻结现象
baseInfo.setFreezingTime(TimestampUtils.convert(LocalDateTime.now().minusDays(1)));
@ -108,7 +109,7 @@ public class UserAccountCommon {
return new UserAccountDTO()
.setUserProfile(userProfileInfraConvertor.toUserProfileDTO(baseInfo))
.setUserSig(requestUserSign(baseInfo.getId()));
.setUserSig(userSig);
}
/**

View File

@ -416,7 +416,11 @@ public class UserAccountClientServiceImpl implements UserAccountClientService {
getTipsUserAccount(CommonErrorCode.DEVICE_UNAVAILABLE, userProfile),
archiveDeviceService.existsDevice(imei, userProfile.getOriginSys()));
voidConsumer.accept();
try {
voidConsumer.accept();
} catch (Exception ex) {
log.error("login side effect failed, userId={}", userId, ex);
}
return new UserAccountDTO()
.setUserProfile(userProfileInnerConvertor.toUserProfileDTO(userProfile))