BUGFIX:绑定谷歌;
This commit is contained in:
@@ -48,8 +48,10 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.sql.DataSource;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@@ -2572,54 +2574,55 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
|
||||
@Override
|
||||
public AccountExtend bindGoogle(String credential) {
|
||||
// 配置 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");
|
||||
// 提取用户信息
|
||||
String userId = payload.getSubject();
|
||||
String email = payload.getEmail();
|
||||
String name = (String) payload.get("name");
|
||||
String pictureUrl = (String) payload.get("picture");
|
||||
|
||||
QueryWrapper<AccountExtend> qw = new QueryWrapper<>();
|
||||
qw.lambda().eq(AccountExtend::getAuth, userId);
|
||||
qw.lambda().eq(AccountExtend::getAuthType, "Google");
|
||||
List<AccountExtend> accountExtends = accountExtendMapper.selectList(qw);
|
||||
QueryWrapper<AccountExtend> qw = new QueryWrapper<>();
|
||||
qw.lambda().eq(AccountExtend::getAuth, userId);
|
||||
qw.lambda().eq(AccountExtend::getAuthType, "Google");
|
||||
List<AccountExtend> accountExtends = accountExtendMapper.selectList(qw);
|
||||
|
||||
if (CollectionUtil.isNotEmpty(accountExtends)) {
|
||||
throw new BusinessException("The Google has been bound.");
|
||||
}
|
||||
|
||||
AccountExtend accountExtendInsert = new AccountExtend();
|
||||
accountExtendInsert.setAuth(userId);
|
||||
accountExtendInsert.setAuthType("Google");
|
||||
accountExtendInsert.setHeadImgUrl(pictureUrl);
|
||||
accountExtendInsert.setName(name);
|
||||
|
||||
AuthPrincipalVo authPrincipalVo = UserContext.getUserHolder();
|
||||
accountExtendInsert.setAccountId(authPrincipalVo.getId());
|
||||
accountExtendMapper.insert(accountExtendInsert);
|
||||
|
||||
return accountExtendInsert;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid ID token.");
|
||||
if (CollectionUtil.isNotEmpty(accountExtends)) {
|
||||
throw new BusinessException("The Google has been bound.");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Failed to verify ID token: " + e.getMessage());
|
||||
AccountExtend accountExtendInsert = new AccountExtend();
|
||||
accountExtendInsert.setAuth(userId);
|
||||
accountExtendInsert.setAuthType("Google");
|
||||
accountExtendInsert.setHeadImgUrl(pictureUrl);
|
||||
accountExtendInsert.setName(name);
|
||||
|
||||
AuthPrincipalVo authPrincipalVo = UserContext.getUserHolder();
|
||||
accountExtendInsert.setAccountId(authPrincipalVo.getId());
|
||||
accountExtendMapper.insert(accountExtendInsert);
|
||||
|
||||
return accountExtendInsert;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid ID token.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user