第三方登录-google-google email与email注册的account绑定

This commit is contained in:
khalil
2025-07-10 11:46:10 +08:00
parent d0d6e4b5be
commit e5c66e89a1
4 changed files with 79 additions and 48 deletions

View File

@@ -1,6 +1,7 @@
package com.accompany.core.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@@ -32,5 +33,7 @@ public class GoogleOpenidRef implements Serializable {
*/
private Date createTime;
@TableField(exist = false)
private boolean register;
}

View File

@@ -48,10 +48,9 @@ public class AccountService extends ServiceImpl<AccountMapper, Account> {
* @param unionId
* @return
*/
public Account getAccountByThird(Byte type, String unionId, String signApp) {
public Account getAccountByThird(Byte type, String unionId) {
QueryWrapper<Account> wrapper = new QueryWrapper<>();
wrapper.lambda().eq(Account::getUnionId, unionId).eq(Account::getThirdLoginType, type)
.eq(StringUtils.hasText(signApp), Account::getSignupApp, signApp);
wrapper.lambda().eq(Account::getUnionId, unionId).eq(Account::getThirdLoginType, type);
List<Account> accounts = list(wrapper);
if (CollectionUtils.isEmpty(accounts)) {
return null;

View File

@@ -25,24 +25,35 @@ public class GoogleOpenidRefServiceImpl extends ServiceImpl<GoogleOpenidRefMappe
@Override
public GoogleOpenidRef getRefByEmail(String email, String idToken) {
GoogleOpenidRef googleOpenidRef = baseMapper.selectById(email);
if (null != googleOpenidRef){
return googleOpenidRef;
}
if (StringUtils.isBlank(idToken)){
if (!isValidEmailFormat(email) || StringUtils.isBlank(idToken)){
return null;
}
GoogleOpenidRef googleOpenidRef = null;
GoogleTokenVerifier.GoogleUserInfo sub = googleTokenVerifier.verifyAndGetUserInfo(idToken);
if (sub != null && StringUtils.isNotEmpty(sub.getUserId()) && email.equals(sub.getEmail())) {
googleOpenidRef = new GoogleOpenidRef();
googleOpenidRef.setEmail(sub.getEmail());
googleOpenidRef.setOpenId(sub.getUserId());
googleOpenidRef.setCreateTime(new Date());
}
if (null == googleOpenidRef){
return null;
}
GoogleOpenidRef dbRef = baseMapper.selectById(googleOpenidRef.getEmail());
if (null == dbRef){
baseMapper.insert(googleOpenidRef);
return googleOpenidRef;
googleOpenidRef.setRegister(true);
}
return null;
return googleOpenidRef;
}
private boolean isValidEmailFormat(String email) {
String regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
return email.matches(regex);
}
}

View File

@@ -142,6 +142,7 @@ public class AccountManageService {
throw new ServiceException(BusiStatus.REQUEST_FAST);
}
Account account = null;
String thirdAccountEmail = null;
if (LoginTypeEnum.GOOGLE.getValue() == type) {
@@ -150,53 +151,34 @@ public class AccountManageService {
openid = ref.getOpenId();
unionId = ref.getOpenId();
thirdAccountEmail = ref.getEmail();
if (ref.isRegister()){
Account emailAccount = accountService.getAccountByEmail(thirdAccountEmail);
if (null != emailAccount && null == emailAccount.getThirdLoginType()){
emailAccount.setThirdLoginType(type);
emailAccount.setUnionId(unionId);
emailAccount.setOpenId(openid);
accountMapper.updateById(emailAccount);
account = emailAccount;
}
}
}
}
Account account = accountService.getAccountByThird(type, unionId, null);
if (account == null) {
checkRegisterLimit(deviceInfo.getDeviceId(), ipAddress);
if (null == account){
account = accountService.getAccountByThird(type, unionId);
}
Date date = new Date();
account = new Account();
account.setNeteaseToken(UUIDUtil.get());
account.setLastLoginTime(date);
account.setLastLoginIp(ipAddress);
account.setUpdateTime(date);
account.setRegisterIp(ipAddress);
account.setSignTime(date);
account.setState("1");
account.setErbanNo(erBanNoService.getErBanNo());
account.setPhone(CommonUtil.genSpecialPhoneForInitAccount(account.getErbanNo().toString()));
// 三方登录信息
account.setThirdLoginType(type);
account.setUnionId(unionId);
account.setOpenId(openid);
account.setEmail(thirdAccountEmail);
account = fillDeviceInfo(account, deviceInfo);
accountMapper.insert(account);
//写缓存
accountService.writeAche(account);
String uidStr = String.valueOf(account.getUid());
TokenRet tokenRet = netEaseService.createNetEaseAcc(uidStr, account.getNeteaseToken(), "", "", null);
if (tokenRet.getCode() != 200) {
log.info("注册云信账号失败,openid=" + openid + "&uid=" + uidStr + ",异常原因code=" + tokenRet.getCode());
log.error("注册云信账号失败,openid=" + openid + "&uid=" + uidStr + ",异常原因code=" + tokenRet.getCode());
throw new Exception("第三方登录失败,openid=" + openid + ",异常原因code=" + tokenRet.getCode());
}
} else {
if (null != account){
// 已存在用户先判断account中的unionId是否为空或者不一致如果是的话则更新不是则跳过
String state = account.getState();
if ("2".equals(state)) {
if (Constant.AccountState.block.equals(state)) {
throw new CustomOAuth2Exception(CustomOAuth2Exception.INVALID_USER,
"用户账号异常请联系官方客服uid=" + account.getUid());
}
Boolean bol =
(BlankUtil.isBlank(account.getUnionId())) || !account.getUnionId().equals(unionId);
if (bol) {
if (BlankUtil.isBlank(account.getUnionId()) || !account.getUnionId().equals(unionId)) {
account.setUnionId(unionId);
}
account.setLastLoginTime(new Date());
@@ -204,7 +186,43 @@ public class AccountManageService {
account.setUpdateTime(new Date());
accountMapper.updateById(account);
}
checkRegisterLimit(deviceInfo.getDeviceId(), ipAddress);
Date date = new Date();
account = new Account();
account.setLastLoginTime(date);
account.setLastLoginIp(ipAddress);
account.setUpdateTime(date);
account.setRegisterIp(ipAddress);
account.setSignTime(date);
account.setState(Constant.AccountState.normal);
account.setErbanNo(erBanNoService.getErBanNo());
account.setPhone(CommonUtil.genSpecialPhoneForInitAccount(account.getErbanNo().toString()));
// 三方登录信息
account.setThirdLoginType(type);
account.setUnionId(unionId);
account.setOpenId(openid);
account.setEmail(thirdAccountEmail);
account.setNeteaseToken(UUIDUtil.get());
account = fillDeviceInfo(account, deviceInfo);
accountMapper.insert(account);
//写缓存
accountService.writeAche(account);
String uidStr = String.valueOf(account.getUid());
TokenRet tokenRet = netEaseService.createNetEaseAcc(uidStr, account.getNeteaseToken(), "", "", null);
if (tokenRet.getCode() != 200) {
log.info("注册云信账号失败,openid=" + openid + "&uid=" + uidStr + ",异常原因code=" + tokenRet.getCode());
log.error("注册云信账号失败,openid=" + openid + "&uid=" + uidStr + ",异常原因code=" + tokenRet.getCode());
throw new Exception("第三方登录失败,openid=" + openid + ",异常原因code=" + tokenRet.getCode());
}
return account;
} finally {
jedisLockService.unlock(locKey, lockVal);
}