BUGFIX:绑定谷歌;
This commit is contained in:
@@ -2240,119 +2240,120 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
|
||||
@Override
|
||||
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 {
|
||||
// 配置 Google ID Token 验证器
|
||||
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(
|
||||
new NetHttpTransport(),
|
||||
JacksonFactory.getDefaultInstance())
|
||||
.setAudience(Collections.singletonList(CLIENT_ID))
|
||||
.build();
|
||||
|
||||
// 验证并解析 ID Token
|
||||
GoogleIdToken idToken = verifier.verify(credential);
|
||||
idToken = verifier.verify(credential);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
if (idToken != null) {
|
||||
GoogleIdToken.Payload payload = idToken.getPayload();
|
||||
if (idToken != null) {
|
||||
GoogleIdToken.Payload payload = idToken.getPayload();
|
||||
|
||||
// 提取用户信息
|
||||
String userId = payload.getSubject();
|
||||
String email = payload.getEmail();
|
||||
String name = (String) payload.get("name");
|
||||
String pictureUrl = (String) payload.get("picture");
|
||||
log.info(userId);
|
||||
log.info(email);
|
||||
log.info(name);
|
||||
Account account = new Account();
|
||||
if (type == 1) {
|
||||
// 注册
|
||||
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
|
||||
accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google");
|
||||
accountExtendQW.lambda().eq(AccountExtend::getAuth, userId);
|
||||
List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW);
|
||||
if (CollectionUtil.isNotEmpty(accountExtends)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
// 提取用户信息
|
||||
String userId = payload.getSubject();
|
||||
String email = payload.getEmail();
|
||||
String name = (String) payload.get("name");
|
||||
String pictureUrl = (String) payload.get("picture");
|
||||
log.info(userId);
|
||||
log.info(email);
|
||||
log.info(name);
|
||||
Account account = new Account();
|
||||
if (type == 1) {
|
||||
// 注册
|
||||
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
|
||||
accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google");
|
||||
accountExtendQW.lambda().eq(AccountExtend::getAuth, userId);
|
||||
List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW);
|
||||
if (CollectionUtil.isNotEmpty(accountExtends)) {
|
||||
throw new BusinessException("This Google account has been registered for AiDA, please use Google Quick Login directly.");
|
||||
}else {
|
||||
// 登录
|
||||
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
|
||||
accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google");
|
||||
accountExtendQW.lambda().eq(AccountExtend::getAuth, userId);
|
||||
List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW);
|
||||
if (CollectionUtil.isNotEmpty(accountExtends)) {
|
||||
AccountExtend accountExtend = accountExtends.get(0);
|
||||
account = accountMapper.selectById(accountExtend.getAccountId());
|
||||
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 {
|
||||
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);
|
||||
response.setEmail(account.getUserEmail());
|
||||
String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId()));
|
||||
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 {
|
||||
// 登录
|
||||
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
|
||||
accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google");
|
||||
accountExtendQW.lambda().eq(AccountExtend::getAuth, userId);
|
||||
List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW);
|
||||
if (CollectionUtil.isNotEmpty(accountExtends)) {
|
||||
AccountExtend accountExtend = accountExtends.get(0);
|
||||
account = accountMapper.selectById(accountExtend.getAccountId());
|
||||
}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.");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Failed to verify ID token: " + e.getMessage());
|
||||
AccountLoginVO response = CopyUtil.copyObject(account, AccountLoginVO.class);
|
||||
response.setEmail(account.getUserEmail());
|
||||
String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId()));
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user