BUGFIX:绑定谷歌;
This commit is contained in:
@@ -48,8 +48,10 @@ import javax.annotation.Resource;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
import java.io.IOException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -2572,54 +2574,55 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AccountExtend bindGoogle(String credential) {
|
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 {
|
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");
|
||||||
|
|
||||||
QueryWrapper<AccountExtend> qw = new QueryWrapper<>();
|
QueryWrapper<AccountExtend> qw = new QueryWrapper<>();
|
||||||
qw.lambda().eq(AccountExtend::getAuth, userId);
|
qw.lambda().eq(AccountExtend::getAuth, userId);
|
||||||
qw.lambda().eq(AccountExtend::getAuthType, "Google");
|
qw.lambda().eq(AccountExtend::getAuthType, "Google");
|
||||||
List<AccountExtend> accountExtends = accountExtendMapper.selectList(qw);
|
List<AccountExtend> accountExtends = accountExtendMapper.selectList(qw);
|
||||||
|
|
||||||
if (CollectionUtil.isNotEmpty(accountExtends)) {
|
if (CollectionUtil.isNotEmpty(accountExtends)) {
|
||||||
throw new BusinessException("The Google has been bound.");
|
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.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
AccountExtend accountExtendInsert = new AccountExtend();
|
||||||
e.printStackTrace();
|
accountExtendInsert.setAuth(userId);
|
||||||
throw new RuntimeException("Failed to verify ID token: " + e.getMessage());
|
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