第三方登录-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; package com.accompany.core.model;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data; import lombok.Data;
@@ -32,5 +33,7 @@ public class GoogleOpenidRef implements Serializable {
*/ */
private Date createTime; private Date createTime;
@TableField(exist = false)
private boolean register;
} }

View File

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

View File

@@ -25,24 +25,35 @@ public class GoogleOpenidRefServiceImpl extends ServiceImpl<GoogleOpenidRefMappe
@Override @Override
public GoogleOpenidRef getRefByEmail(String email, String idToken) { public GoogleOpenidRef getRefByEmail(String email, String idToken) {
GoogleOpenidRef googleOpenidRef = baseMapper.selectById(email); if (!isValidEmailFormat(email) || StringUtils.isBlank(idToken)){
if (null != googleOpenidRef){
return googleOpenidRef;
}
if (StringUtils.isBlank(idToken)){
return null; return null;
} }
GoogleOpenidRef googleOpenidRef = null;
GoogleTokenVerifier.GoogleUserInfo sub = googleTokenVerifier.verifyAndGetUserInfo(idToken); GoogleTokenVerifier.GoogleUserInfo sub = googleTokenVerifier.verifyAndGetUserInfo(idToken);
if (sub != null && StringUtils.isNotEmpty(sub.getUserId()) && email.equals(sub.getEmail())) { if (sub != null && StringUtils.isNotEmpty(sub.getUserId()) && email.equals(sub.getEmail())) {
googleOpenidRef = new GoogleOpenidRef(); googleOpenidRef = new GoogleOpenidRef();
googleOpenidRef.setEmail(sub.getEmail()); googleOpenidRef.setEmail(sub.getEmail());
googleOpenidRef.setOpenId(sub.getUserId()); googleOpenidRef.setOpenId(sub.getUserId());
googleOpenidRef.setCreateTime(new Date()); googleOpenidRef.setCreateTime(new Date());
}
if (null == googleOpenidRef){
return null;
}
GoogleOpenidRef dbRef = baseMapper.selectById(googleOpenidRef.getEmail());
if (null == dbRef){
baseMapper.insert(googleOpenidRef); 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); throw new ServiceException(BusiStatus.REQUEST_FAST);
} }
Account account = null;
String thirdAccountEmail = null; String thirdAccountEmail = null;
if (LoginTypeEnum.GOOGLE.getValue() == type) { if (LoginTypeEnum.GOOGLE.getValue() == type) {
@@ -150,53 +151,34 @@ public class AccountManageService {
openid = ref.getOpenId(); openid = ref.getOpenId();
unionId = ref.getOpenId(); unionId = ref.getOpenId();
thirdAccountEmail = ref.getEmail(); 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 (null == account){
if (account == null) { account = accountService.getAccountByThird(type, unionId);
checkRegisterLimit(deviceInfo.getDeviceId(), ipAddress); }
Date date = new Date(); if (null != account){
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 {
// 已存在用户先判断account中的unionId是否为空或者不一致如果是的话则更新不是则跳过 // 已存在用户先判断account中的unionId是否为空或者不一致如果是的话则更新不是则跳过
String state = account.getState(); String state = account.getState();
if ("2".equals(state)) { if (Constant.AccountState.block.equals(state)) {
throw new CustomOAuth2Exception(CustomOAuth2Exception.INVALID_USER, throw new CustomOAuth2Exception(CustomOAuth2Exception.INVALID_USER,
"用户账号异常请联系官方客服uid=" + account.getUid()); "用户账号异常请联系官方客服uid=" + account.getUid());
} }
Boolean bol =
(BlankUtil.isBlank(account.getUnionId())) || !account.getUnionId().equals(unionId); if (BlankUtil.isBlank(account.getUnionId()) || !account.getUnionId().equals(unionId)) {
if (bol) {
account.setUnionId(unionId); account.setUnionId(unionId);
} }
account.setLastLoginTime(new Date()); account.setLastLoginTime(new Date());
@@ -204,7 +186,43 @@ public class AccountManageService {
account.setUpdateTime(new Date()); account.setUpdateTime(new Date());
accountMapper.updateById(account); 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; return account;
} finally { } finally {
jedisLockService.unlock(locKey, lockVal); jedisLockService.unlock(locKey, lockVal);
} }