fix(auth): login existing account during registration
This commit is contained in:
parent
895193f33c
commit
45cb56b45a
@ -6,10 +6,12 @@ import com.red.circle.auth.dto.TokenCredentialCO;
|
|||||||
import com.red.circle.auth.response.AuthErrorCode;
|
import com.red.circle.auth.response.AuthErrorCode;
|
||||||
import com.red.circle.auth.storage.RedCircleCredentialService;
|
import com.red.circle.auth.storage.RedCircleCredentialService;
|
||||||
import com.red.circle.component.redis.service.RedisService;
|
import com.red.circle.component.redis.service.RedisService;
|
||||||
import com.red.circle.framework.core.asserts.ResponseAssert;
|
import com.red.circle.framework.core.asserts.ResponseAssert;
|
||||||
import com.red.circle.framework.core.dto.CommonCommand;
|
import com.red.circle.framework.core.dto.CommonCommand;
|
||||||
import com.red.circle.framework.core.request.RequestClientEnum;
|
import com.red.circle.framework.core.exception.ResponseException;
|
||||||
import com.red.circle.framework.core.security.UserCredential;
|
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.framework.web.spring.ApplicationRequestUtils;
|
||||||
import com.red.circle.other.inner.asserts.user.UserErrorCode;
|
import com.red.circle.other.inner.asserts.user.UserErrorCode;
|
||||||
import com.red.circle.other.inner.endpoint.sys.SysCountryCodeClient;
|
import com.red.circle.other.inner.endpoint.sys.SysCountryCodeClient;
|
||||||
@ -29,10 +31,11 @@ import java.util.Objects;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
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.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@ -141,15 +144,22 @@ public class EndpointRestController {
|
|||||||
@PostMapping("/account/create")
|
@PostMapping("/account/create")
|
||||||
public TokenCredentialCO createAccount(@RequestBody @Validated CreateAccountCmd cmd, HttpServletRequest request) {
|
public TokenCredentialCO createAccount(@RequestBody @Validated CreateAccountCmd cmd, HttpServletRequest request) {
|
||||||
log.warn("cmd {}", cmd);
|
log.warn("cmd {}", cmd);
|
||||||
if (cmd.getType().equalsIgnoreCase(AuthTypeEnum.IMEI.getKey())&& cmd.getReqClient().isAndroid()) {
|
if (cmd.getType().equalsIgnoreCase(AuthTypeEnum.IMEI.getKey())&& cmd.getReqClient().isAndroid()) {
|
||||||
//设备禁止登录
|
//设备禁止登录
|
||||||
ResponseAssert.failure(AuthErrorCode.AUTH_DEVICE_NOT_SUPPORT);
|
ResponseAssert.failure(AuthErrorCode.AUTH_DEVICE_NOT_SUPPORT);
|
||||||
}
|
}
|
||||||
UserAccountDTO account = ResponseAssert.requiredSuccess(appUserAccountClient.create(cmd));
|
try {
|
||||||
ResponseAssert.notNull(UserErrorCode.REGISTRATION_FAILED, account);
|
UserAccountDTO account = ResponseAssert.requiredSuccess(appUserAccountClient.create(cmd));
|
||||||
TokenCredentialCO tokenCredentialCO = processToken.createUserCredential(account);
|
return createTokenCredential(account, cmd.requireReqSysOrigin(), cmd.getReqZoneId(), request);
|
||||||
return submitLoginAccessValidation(tokenCredentialCO, 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用户注册.
|
* 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) {
|
public TokenCredentialCO accountLogin1(@RequestBody @Validated AccountLoginCmd cmd, HttpServletRequest request) {
|
||||||
TokenCredentialCO tokenCredentialCO = processToken.createUserCredential(
|
TokenCredentialCO tokenCredentialCO = processToken.createUserCredential(
|
||||||
ResponseAssert.requiredSuccess(appUserAccountClient.accountLogin(cmd)));
|
ResponseAssert.requiredSuccess(appUserAccountClient.accountLogin(cmd)));
|
||||||
return submitLoginAccessValidation(tokenCredentialCO, cmd.requireReqSysOrigin(), cmd.getReqZoneId(), request);
|
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(
|
private TokenCredentialCO submitLoginAccessValidation(
|
||||||
TokenCredentialCO tokenCredentialCO,
|
TokenCredentialCO tokenCredentialCO,
|
||||||
String reqSysOrigin,
|
String reqSysOrigin,
|
||||||
|
|||||||
@ -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.infra.database.rds.service.user.user.AuthTypeService;
|
||||||
import com.red.circle.other.inner.asserts.DynamicErrorCode;
|
import com.red.circle.other.inner.asserts.DynamicErrorCode;
|
||||||
import com.red.circle.other.inner.model.cmd.user.UserProfileCmd;
|
import com.red.circle.other.inner.model.cmd.user.UserProfileCmd;
|
||||||
import com.red.circle.tool.core.phone.PhoneNumberUtils;
|
import com.red.circle.tool.core.phone.PhoneNumberUtils;
|
||||||
import com.red.circle.tool.core.sequence.IdWorkerUtils;
|
import com.red.circle.tool.core.sequence.IdWorkerUtils;
|
||||||
import com.red.circle.tool.core.text.StringUtils;
|
import com.red.circle.tool.core.text.StringUtils;
|
||||||
import com.red.circle.other.inner.asserts.user.UserErrorCode;
|
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.CreateAccountCmd;
|
||||||
import com.red.circle.other.inner.model.cmd.user.account.MobileAuthCmd;
|
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.cmd.user.device.AllowDeviceRegisterCmd;
|
||||||
import com.red.circle.other.inner.model.dto.user.account.UserAccountDTO;
|
import com.red.circle.other.inner.model.dto.user.account.UserAccountDTO;
|
||||||
@ -48,11 +48,11 @@ public class CreateAccountAspect {
|
|||||||
|
|
||||||
private final EnvProperties evnProperties;
|
private final EnvProperties evnProperties;
|
||||||
private final AuthTypeService authTypeService;
|
private final AuthTypeService authTypeService;
|
||||||
private final ArchiveDeviceService archiveDeviceService;
|
private final ArchiveDeviceService archiveDeviceService;
|
||||||
private final RegisterDeviceGateway registerDeviceGateway;
|
private final RegisterDeviceGateway registerDeviceGateway;
|
||||||
private final RegisterRewardStreamProducer registerRewardStreamProducer;
|
private final RegisterRewardStreamProducer registerRewardStreamProducer;
|
||||||
private final CreateAccountExpandProcess createAccountExpandProcess;
|
private final CreateAccountExpandProcess createAccountExpandProcess;
|
||||||
private final CheckRegisterUserProfileProcess checkRegisterUserProfileProcess;
|
private final CheckRegisterUserProfileProcess checkRegisterUserProfileProcess;
|
||||||
|
|
||||||
@Pointcut("@annotation(com.red.circle.other.infra.annotation.CreateAccount)")
|
@Pointcut("@annotation(com.red.circle.other.infra.annotation.CreateAccount)")
|
||||||
public void userRegisterAspectPointCut() {
|
public void userRegisterAspectPointCut() {
|
||||||
@ -70,23 +70,40 @@ public class CreateAccountAspect {
|
|||||||
|
|
||||||
ResultResponse<UserAccountDTO> result = (ResultResponse<UserAccountDTO>) joinPoint.proceed();
|
ResultResponse<UserAccountDTO> result = (ResultResponse<UserAccountDTO>) joinPoint.proceed();
|
||||||
log.warn("cmd {} result.getBody {}", cmd, result.getBody());
|
log.warn("cmd {} result.getBody {}", cmd, result.getBody());
|
||||||
if (Objects.isNull(cmd.getProfile())) {
|
if (result.checkSuccess() && Objects.nonNull(result.getBody())) {
|
||||||
UserProfileCmd userProfile = new UserProfileCmd();
|
if (Objects.isNull(cmd.getProfile())) {
|
||||||
userProfile.setCountryId(result.getBody().getUserProfile().getCountryId());
|
UserProfileCmd userProfile = new UserProfileCmd();
|
||||||
cmd.setProfile(userProfile);
|
userProfile.setCountryId(result.getBody().getUserProfile().getCountryId());
|
||||||
}
|
cmd.setProfile(userProfile);
|
||||||
if (result.checkSuccess() && Objects.nonNull(result.getBody())) {
|
}
|
||||||
registerRewardStreamProducer.publish(
|
publishRegisterReward(cmd);
|
||||||
SysOriginPlatformEnum.valueOf(cmd.requireReqSysOrigin()),
|
processRegisterExpand(cmd);
|
||||||
cmd.requiredReqUserId(),
|
}
|
||||||
cmd.getProfile().getCountryId()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
createAccountExpandProcess.process(cmd);
|
|
||||||
return result;
|
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) {
|
private void generateUserId(CreateAccountCmd cmd) {
|
||||||
cmd.setReqUserId(IdWorkerUtils.getId());
|
cmd.setReqUserId(IdWorkerUtils.getId());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -91,6 +91,7 @@ public class UserAccountCommon {
|
|||||||
ResponseAssert.notNull(UserErrorCode.AUTH_TYPE_NOT_SUPPORTED, AuthTypeEnum.toEnum(cmd.getType()));
|
ResponseAssert.notNull(UserErrorCode.AUTH_TYPE_NOT_SUPPORTED, AuthTypeEnum.toEnum(cmd.getType()));
|
||||||
|
|
||||||
BaseInfo baseInfo = toBaseInfo(cmd);
|
BaseInfo baseInfo = toBaseInfo(cmd);
|
||||||
|
String userSig = requestUserSign(baseInfo.getId());
|
||||||
try {
|
try {
|
||||||
// 客户端时区判断问题出现了,新用户注册就被冻结现象
|
// 客户端时区判断问题出现了,新用户注册就被冻结现象
|
||||||
baseInfo.setFreezingTime(TimestampUtils.convert(LocalDateTime.now().minusDays(1)));
|
baseInfo.setFreezingTime(TimestampUtils.convert(LocalDateTime.now().minusDays(1)));
|
||||||
@ -108,7 +109,7 @@ public class UserAccountCommon {
|
|||||||
|
|
||||||
return new UserAccountDTO()
|
return new UserAccountDTO()
|
||||||
.setUserProfile(userProfileInfraConvertor.toUserProfileDTO(baseInfo))
|
.setUserProfile(userProfileInfraConvertor.toUserProfileDTO(baseInfo))
|
||||||
.setUserSig(requestUserSign(baseInfo.getId()));
|
.setUserSig(userSig);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -416,7 +416,11 @@ public class UserAccountClientServiceImpl implements UserAccountClientService {
|
|||||||
getTipsUserAccount(CommonErrorCode.DEVICE_UNAVAILABLE, userProfile),
|
getTipsUserAccount(CommonErrorCode.DEVICE_UNAVAILABLE, userProfile),
|
||||||
archiveDeviceService.existsDevice(imei, userProfile.getOriginSys()));
|
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()
|
return new UserAccountDTO()
|
||||||
.setUserProfile(userProfileInnerConvertor.toUserProfileDTO(userProfile))
|
.setUserProfile(userProfileInnerConvertor.toUserProfileDTO(userProfile))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user