fix(auth): login existing account during registration
This commit is contained in:
parent
895193f33c
commit
45cb56b45a
@ -8,7 +8,9 @@ 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.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;
|
||||
@ -29,6 +31,7 @@ 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;
|
||||
@ -145,10 +148,17 @@ public class EndpointRestController {
|
||||
//设备禁止登录
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -239,6 +249,55 @@ public class EndpointRestController {
|
||||
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,
|
||||
|
||||
@ -70,21 +70,38 @@ 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())) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
createAccountExpandProcess.process(cmd);
|
||||
return result;
|
||||
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) {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user