BUGFIX:绑定谷歌;

This commit is contained in:
shahaibo
2025-02-05 13:38:10 +08:00
parent 627f264ef3
commit 9e5d2f47e2

View File

@@ -2240,119 +2240,120 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
@Override @Override
public AccountLoginVO parseGoogleCredential(String credential, Integer type) { public AccountLoginVO parseGoogleCredential(String credential, Integer type) {
// 配置 Google ID Token 验证器
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance())
.setAudience(Collections.singletonList(CLIENT_ID))
.build();
// 验证并解析 ID Token
GoogleIdToken idToken = null;
try { try {
// 配置 Google ID Token 验证器 idToken = verifier.verify(credential);
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder( } catch (GeneralSecurityException e) {
new NetHttpTransport(), throw new RuntimeException(e);
JacksonFactory.getDefaultInstance()) } catch (IOException e) {
.setAudience(Collections.singletonList(CLIENT_ID)) throw new RuntimeException(e);
.build(); }
// 验证并解析 ID Token
GoogleIdToken idToken = verifier.verify(credential);
if (idToken != null) { if (idToken != null) {
GoogleIdToken.Payload payload = idToken.getPayload(); GoogleIdToken.Payload payload = idToken.getPayload();
// 提取用户信息 // 提取用户信息
String userId = payload.getSubject(); String userId = payload.getSubject();
String email = payload.getEmail(); String email = payload.getEmail();
String name = (String) payload.get("name"); String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture"); String pictureUrl = (String) payload.get("picture");
log.info(userId); log.info(userId);
log.info(email); log.info(email);
log.info(name); log.info(name);
Account account = new Account(); Account account = new Account();
if (type == 1) { if (type == 1) {
// 注册 // 注册
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>(); QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google"); accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google");
accountExtendQW.lambda().eq(AccountExtend::getAuth, userId); accountExtendQW.lambda().eq(AccountExtend::getAuth, userId);
List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW); List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW);
if (CollectionUtil.isNotEmpty(accountExtends)) { if (CollectionUtil.isNotEmpty(accountExtends)) {
throw new BusinessException("This Google account has been registered for AiDA, please use Google Quick Login directly."); throw new BusinessException("This Google account has been registered for AiDA, please use Google Quick Login directly.");
}else {
QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
accountQueryWrapper.lambda().eq(Account::getUserEmail, email); // 根据邮箱查询用户
List<Account> accounts = accountMapper.selectList(accountQueryWrapper);
if (CollectionUtil.isNotEmpty(accounts)) {
account = accounts.get(0);
AccountExtend accountExtendInsert = new AccountExtend();
accountExtendInsert.setAuth(userId);
accountExtendInsert.setAuthType("Google");
accountExtendInsert.setHeadImgUrl(pictureUrl);
accountExtendInsert.setName(name);
accountExtendInsert.setAccountId(account.getId());
accountExtendMapper.insert(accountExtendInsert);
}else {
Account newUser = new Account();
newUser.setUserEmail(email);
newUser.setUserName(name);
newUser.setUserPassword("Third-000000");
newUser.setLanguage(Language.ENGLISH.name());
newUser.setValidStartTime(System.currentTimeMillis());
newUser.setValidEndTime(toDayEnd(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli()));
newUser.setCreateDate(new Date());
newUser.setIsTrial(1);
newUser.setIsBeginner(1);
newUser.setCredits(BigDecimal.valueOf(100));
newUser.setSystemUser(3);
accountMapper.insert(newUser);
account = newUser;
AccountExtend accountExtendInsert = new AccountExtend();
accountExtendInsert.setAuth(userId);
accountExtendInsert.setAuthType("Google");
accountExtendInsert.setHeadImgUrl(pictureUrl);
accountExtendInsert.setName(name);
accountExtendInsert.setAccountId(newUser.getId());
accountExtendMapper.insert(accountExtendInsert);
}
}
}else { }else {
// 登录 QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>(); accountQueryWrapper.lambda().eq(Account::getUserEmail, email); // 根据邮箱查询用户
accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google"); List<Account> accounts = accountMapper.selectList(accountQueryWrapper);
accountExtendQW.lambda().eq(AccountExtend::getAuth, userId); if (CollectionUtil.isNotEmpty(accounts)) {
List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW); account = accounts.get(0);
if (CollectionUtil.isNotEmpty(accountExtends)) { AccountExtend accountExtendInsert = new AccountExtend();
AccountExtend accountExtend = accountExtends.get(0); accountExtendInsert.setAuth(userId);
account = accountMapper.selectById(accountExtend.getAccountId()); accountExtendInsert.setAuthType("Google");
accountExtendInsert.setHeadImgUrl(pictureUrl);
accountExtendInsert.setName(name);
accountExtendInsert.setAccountId(account.getId());
accountExtendMapper.insert(accountExtendInsert);
}else { }else {
throw new BusinessException("This Google account has not been bound to an AiDA account yet. Please register an AiDA account and bind it to a Google account, or bind the Google account to an existing AiDA account."); Account newUser = new Account();
newUser.setUserEmail(email);
newUser.setUserName(name);
newUser.setUserPassword("Third-000000");
newUser.setLanguage(Language.ENGLISH.name());
newUser.setValidStartTime(System.currentTimeMillis());
newUser.setValidEndTime(toDayEnd(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli()));
newUser.setCreateDate(new Date());
newUser.setIsTrial(1);
newUser.setIsBeginner(1);
newUser.setCredits(BigDecimal.valueOf(100));
newUser.setSystemUser(3);
accountMapper.insert(newUser);
account = newUser;
AccountExtend accountExtendInsert = new AccountExtend();
accountExtendInsert.setAuth(userId);
accountExtendInsert.setAuthType("Google");
accountExtendInsert.setHeadImgUrl(pictureUrl);
accountExtendInsert.setName(name);
accountExtendInsert.setAccountId(newUser.getId());
accountExtendMapper.insert(accountExtendInsert);
} }
} }
AccountLoginVO response = CopyUtil.copyObject(account, AccountLoginVO.class); }else {
response.setEmail(account.getUserEmail()); // 登录
String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId())); QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
if (StringUtils.isNotBlank(token)) { accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google");
//用户已登入 accountExtendQW.lambda().eq(AccountExtend::getAuth, userId);
response.setToken(token); List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW);
} else { if (CollectionUtil.isNotEmpty(accountExtends)) {
response.setToken(createAccountToken(account)); AccountExtend accountExtend = accountExtends.get(0);
} account = accountMapper.selectById(accountExtend.getAccountId());
response.setUserId(account.getId());
response.setSystemUser(account.getSystemUser());
// 设置头像
String avatar;
if (StringUtil.isNullOrEmpty(account.getAvatar())){
avatar = CommonConstant.DEFAULT_AVATAR;
}else { }else {
avatar = account.getAvatar(); throw new BusinessException("This Google account has not been bound to an AiDA account yet. Please register an AiDA account and bind it to a Google account, or bind the Google account to an existing AiDA account.");
} }
response.setAvatar(minioUtil.getPreSignedUrl(avatar, CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
response.setFolloweeCount(portfolioService.getFolloweeCount(account.getId()));
response.setFollowerCount(portfolioService.getFollowerCount(account.getId()));
return response;
} else {
throw new IllegalArgumentException("Invalid ID token.");
} }
AccountLoginVO response = CopyUtil.copyObject(account, AccountLoginVO.class);
} catch (Exception e) { response.setEmail(account.getUserEmail());
e.printStackTrace(); String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId()));
throw new RuntimeException("Failed to verify ID token: " + e.getMessage()); if (StringUtils.isNotBlank(token)) {
//用户已登入
response.setToken(token);
} else {
response.setToken(createAccountToken(account));
}
response.setUserId(account.getId());
response.setSystemUser(account.getSystemUser());
// 设置头像
String avatar;
if (StringUtil.isNullOrEmpty(account.getAvatar())){
avatar = CommonConstant.DEFAULT_AVATAR;
}else {
avatar = account.getAvatar();
}
response.setAvatar(minioUtil.getPreSignedUrl(avatar, CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
response.setFolloweeCount(portfolioService.getFolloweeCount(account.getId()));
response.setFollowerCount(portfolioService.getFollowerCount(account.getId()));
return response;
} else {
throw new IllegalArgumentException("Invalid ID token.");
} }
} }