提交 875b527f 编写于 作者: Chongwen.jiang's avatar Chongwen.jiang

微信/苹果登录 验证码校验注册业务时需要原子性校验

上级 7edca772
流水线 #22631 已失败 于阶段
in 0 second
......@@ -63,4 +63,7 @@ public class Constants {
/** 验证码缓存key前缀 */
public static final String AUTH_CODE_PREFIX = "authCode-";
/** 注册逻辑幂等处理缓存key */
public static final String REPEAT_REGISTER_PREFIX = "repeat-register—";
}
......@@ -118,7 +118,6 @@ public class LoginController extends AccountBaseController {
@PostMapping(value = "/login/wechat/step2")
public PicaResponse<LoginResult> loginByWeChatStep(@RequestBody EncryptEntity entity) throws Exception {
BaseRequest request = CryptoUtil.decrypt(entity, BaseRequest.class);
accountUtils.checkMobilePhoneAndAuthCode(request.getMobile(), AccountTypeEnum.SYSCODE_TYPE_WE_CHAT.getCode() + "", request.getAuthCode());
request.setProductType(super.getProductType());
request.setSourceType(super.getSourceType());
request.setLoginIp(super.getIpAddr());
......
......@@ -245,6 +245,7 @@ public class LoginServiceImpl implements LoginService {
public LoginResult loginAndRegister(BaseRequest baseRequest) {
String mobile = baseRequest.getMobile();
AccountInfoEntity accountInfoEntity = accountInfoDetailMapper.selectByMobile(AESUtil.encryptV0(mobile));
logger.info("loginAndRegister-accountInfoEntity is null {}, clientIp:{}", (accountInfoEntity == null), baseRequest.getLoginIp());
if (accountInfoEntity == null) {
//说明是注册功能
accountUtils.checkRegisterMobilePhoneAndAuthCode(baseRequest.getMobile(), baseRequest.getFlag() + "", baseRequest.getAuthCode());
......@@ -359,8 +360,13 @@ public class LoginServiceImpl implements LoginService {
public LoginResult loginByWeChatStep(BaseRequest request) {
//判断当前手机号是否注册过,注册过,直接登录;没有注册过,进行注册操操作
AccountInfoEntity accountInfoEntity = accountInfoDetailMapper.selectByMobile(AESUtil.encryptV0(request.getMobile()));
logger.info("loginByWeChatStep-accountInfoEntity is null {}, clientIp:{}", (accountInfoEntity == null), request.getLoginIp());
LoginResult result;
if (accountInfoEntity == null) {
accountUtils.checkRegisterMobilePhoneAndAuthCode(
request.getMobile(),
AccountTypeEnum.SYSCODE_TYPE_WE_CHAT.getCode() + "",
request.getAuthCode());
result = registerService.register(request);
if (doubleWritingMode) {
//双写模式下,要在doctor表存储unionId
......@@ -372,6 +378,10 @@ public class LoginServiceImpl implements LoginService {
}
}
} else {
accountUtils.checkMobilePhoneAndAuthCode(
request.getMobile(),
AccountTypeEnum.SYSCODE_TYPE_WE_CHAT.getCode() + "",
request.getAuthCode());
result = processLogin(request, accountInfoEntity.getId(), AccountTypeEnum.LOGIN_WE_CHAT.getCode());
}
result.setMobile(request.getMobile());
......@@ -776,25 +786,35 @@ public class LoginServiceImpl implements LoginService {
*/
@Override
public LoginResult loginByAppleStep(BaseRequest request) {
// 验证码校验
accountUtils.checkMobilePhoneAndAuthCode(request.getMobile(),
AccountTypeEnum.SYSCODE_TYPE_APPLE.getCode() + "",
request.getAuthCode());
// 判断当前手机号是否注册过: 没有注册过,进行注册操操作, 注册过,直接登录;
AccountInfoEntity accountInfoDb = accountInfoDetailMapper.selectByMobile(AESUtil.encryptV0(request.getMobile()));
logger.info("loginByAppleStep-account is null {}, request:{}", accountInfoDb == null, JSON.toJSONString(request));
LoginResult result;
accountInfoDb= null;
if (accountInfoDb == null) {
// 验证码校验
accountUtils.checkRegisterMobilePhoneAndAuthCode(
request.getMobile(),
AccountTypeEnum.SYSCODE_TYPE_APPLE.getCode() + "",
request.getAuthCode());
result = registerService.register(request);
logger.info("loginByAppleStep-register");
} else {
// 验证码校验
accountUtils.checkMobilePhoneAndAuthCode(request.getMobile(),
AccountTypeEnum.SYSCODE_TYPE_APPLE.getCode() + "",
request.getAuthCode());
result = processLogin(request, accountInfoDb.getId(),
AccountTypeEnum.LOGIN_APPLE.getCode());
logger.info("loginByAppleStep-processLogin");
}
result.setMobile(request.getMobile());
AccountInfoEntity accountInfo = accountInfoDetailMapper.selectByMobile(AESUtil.encryptV0(request.getMobile()));
Integer acctId = accountInfo.getId();
// insert account_apple_info表数据
logger.info("loginByAppleStep-insert-account_apple_info-start");
processAccountUnionApple(acctId, request.getAppleUserId());
logger.info("loginByAppleStep-insert-account_apple_info-end");
return result;
}
......
package com.pica.cloud.account.account.server.service.impl;
import com.pica.cloud.account.account.common.req.OCINRequest;
import com.pica.cloud.account.account.server.constants.Constants;
import com.pica.cloud.account.account.server.entity.*;
import com.pica.cloud.account.account.server.enums.AccountExceptionEnum;
import com.pica.cloud.account.account.server.enums.AccountTypeEnum;
......@@ -30,9 +31,6 @@ import java.util.concurrent.ExecutorService;
@Service
public class RegisterServiceImpl implements RegisterService {
private final String REPEAT_REGISTER_PREFIX = "repeat-register—";
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
......@@ -41,9 +39,6 @@ public class RegisterServiceImpl implements RegisterService {
@Autowired
private AccountInfoDetailMapper accountInfoDetailMapper;
@Autowired
private AccountPatientInfoMapper accountUserInfoMapper;
@Autowired
private AccountMapper accountMapper;
......@@ -56,9 +51,6 @@ public class RegisterServiceImpl implements RegisterService {
@Autowired
private TokenUtils tokenUtils;
// @Autowired
// private QueueProducer queueProducer;
@Autowired
private AgreementEntityMapper agreementEntityMapper;
......@@ -87,12 +79,12 @@ public class RegisterServiceImpl implements RegisterService {
public LoginResult register(BaseRequest baseRequest,QueryMobileEntity queryMobileEntity) {
String mobile = baseRequest.getMobile();
//对注册接口做幂等性处理:注册成功,删除缓存,注册失败提示用户
String exist = redisClient.get(REPEAT_REGISTER_PREFIX + mobile);
String exist = redisClient.get(Constants.REPEAT_REGISTER_PREFIX + mobile);
if (StringUtils.isBlank(exist)) {
String mobileEncrypt = AESUtil.encryptV0(mobile);
AccountInfoEntity accountInfoEntity = accountInfoDetailMapper.selectByMobile(mobileEncrypt);
if (accountInfoEntity == null) {
redisClient.set(REPEAT_REGISTER_PREFIX + mobile, mobile, 30);
redisClient.set(Constants.REPEAT_REGISTER_PREFIX + mobile, mobile, 30);
try {
Date currentTime = new Date();
int productType = baseRequest.getProductType();
......@@ -146,14 +138,6 @@ public class RegisterServiceImpl implements RegisterService {
result.setEntireFlag(1);
result.setDoctorId(EncryptUtils.encryptContent(userId + "", EncryptConstants.ENCRYPT_TYPE_ID));
result.setMobile(mobile);
/* ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
try {
dos.writeLong(userId);
} catch (IOException e) {
e.printStackTrace();
}
queueProducer.send(bos.toByteArray());*/
LogLoginEntity entity = AccountLogEntityUtils.getLogLoginEntity(acctId, productType, baseRequest.getSourceType(),
AccountTypeEnum.LOGIN_REGISTER.getCode(), baseRequest.getLoginIp(), AccountTypeEnum.LOGIN_STATUS_SUCCESS.getCode(),
AccountTypeEnum.LOG_TYPE_LOGIN.getCode());
......@@ -163,7 +147,7 @@ public class RegisterServiceImpl implements RegisterService {
picaLogUtils.info(entity);
processAgreement(userId);
processRoleMap(userId);
redisClient.del(REPEAT_REGISTER_PREFIX + mobile);
redisClient.del(Constants.REPEAT_REGISTER_PREFIX + mobile);
return result;
} catch (Exception e) {
//向上抛出异常,让异常处理框架捕获到
......@@ -171,7 +155,7 @@ public class RegisterServiceImpl implements RegisterService {
throw new AccountException(AccountExceptionEnum.PICA_REGISTER_FAIL);
} finally {
//如果在注册过程中抛出异常,就删除redis中的注册标记
redisClient.del(REPEAT_REGISTER_PREFIX + mobile);
redisClient.del(Constants.REPEAT_REGISTER_PREFIX + mobile);
}
} else {
throw new AccountException(AccountExceptionEnum.PICA_ALREADY_REGISTER);
......@@ -277,33 +261,6 @@ public class RegisterServiceImpl implements RegisterService {
private void processAgreement(Long userId) {
ExecutorService executor = ExecutorServiceUtils.getExecutor();
executor.submit(() -> {
// //用户协议
// Date currentTime = new Date();
// Integer userVersion = agreementEntityMapper.selectByType(AccountAgreementEnum.USER_AGREEMENT.getCode());
// AgreementLogEntity userAgreementLogEntity = new AgreementLogEntity();
// userAgreementLogEntity.setAgreement_type(AccountAgreementEnum.USER_AGREEMENT.getCode());
// userAgreementLogEntity.setDoctor_id(userId);
// userAgreementLogEntity.setVersion(userVersion.toString());
// userAgreementLogEntity.setCreated_id(userId);
// userAgreementLogEntity.setCreated_time(currentTime);
// userAgreementLogEntity.setModified_id(userId);
// userAgreementLogEntity.setModified_time(currentTime);
// userAgreementLogEntity.setDelete_flag(1);
// agreementLogEntityMapper.insert(userAgreementLogEntity);
// //隐私协议
// Integer privateVersion = agreementEntityMapper.selectByType(AccountAgreementEnum.PRIVACY_AGREEMENT.getCode());
// AgreementLogEntity privateAgreementLogEntity = new AgreementLogEntity();
// privateAgreementLogEntity.setAgreement_type(AccountAgreementEnum.PRIVACY_AGREEMENT.getCode());
// privateAgreementLogEntity.setDoctor_id(userId);
// privateAgreementLogEntity.setVersion(privateVersion.toString());
// privateAgreementLogEntity.setCreated_id(userId);
// privateAgreementLogEntity.setCreated_time(currentTime);
// privateAgreementLogEntity.setModified_id(userId);
// privateAgreementLogEntity.setModified_time(currentTime);
// privateAgreementLogEntity.setDelete_flag(1);
// agreementLogEntityMapper.insert(privateAgreementLogEntity);
Integer protocolId = agreementLogEntityMapper.getLatestProtocolId(2); //获取最新用户协议ID
PProtocolLog log = new PProtocolLog();
log.setUserId(userId.toString());
......
......@@ -50,7 +50,7 @@ public class AccountUtils {
}
//获取验证码redis key
public static String getAuthCodeKey(String mobilePhone, String flag) {
public String getAuthCodeKey(String mobilePhone, String flag) {
return Constants.AUTH_CODE_PREFIX + flag + "-" + AESUtil.encryptV0(mobilePhone);
}
......@@ -85,7 +85,7 @@ public class AccountUtils {
logger.info("key" + authCodeKey);
logger.info("success" + num);
if (num == -1) {
throw new PicaException(PicaResultCode.RESULE_DATA_NONE.code(), "短信验证码已失效,请重新获取");
throw new PicaException(PicaResultCode.RESULE_DATA_NONE.code(), "验证码错误,请重新输入");
}
if (num > 0 && num < 99) {
throw new PicaException(PicaResultCode.RESULE_DATA_NONE.code(), "您已注册成功,请直接登录");
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册