TASK:谷歌登录
This commit is contained in:
@@ -4,7 +4,11 @@ import lombok.Data;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class GoogleUser {
|
public class GoogleUser {
|
||||||
private String id;
|
private String sub; // 用户唯一标识
|
||||||
private String email;
|
private String email;
|
||||||
private String name;
|
private String name;
|
||||||
|
private String picture;
|
||||||
|
private boolean emailVerified;
|
||||||
|
private String givenName;
|
||||||
|
private String familyName;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,5 +6,8 @@ import lombok.Data;
|
|||||||
public class GoogleTokenResponse {
|
public class GoogleTokenResponse {
|
||||||
private String accessToken;
|
private String accessToken;
|
||||||
private String idToken;
|
private String idToken;
|
||||||
|
private long expiresIn;
|
||||||
|
private String tokenType;
|
||||||
|
private String scope;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1857,27 +1857,39 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
|||||||
@Override
|
@Override
|
||||||
public String googleCallback(String code, HttpSession session) {
|
public String googleCallback(String code, HttpSession session) {
|
||||||
try {
|
try {
|
||||||
log.info("code:" + code);
|
log.info("code: " + code);
|
||||||
// 使用 code 获取 Google 用户信息
|
// 使用 code 获取 Google 用户信息
|
||||||
GoogleUser googleUser = getGoogleUserFromCode(code);
|
GoogleUser googleUser = getGoogleUserFromCode(code);
|
||||||
log.info("googleUser:" + JSON.toJSONString(googleUser));
|
log.info("googleUser: " + JSON.toJSONString(googleUser));
|
||||||
|
|
||||||
// 检查数据库中是否已有该用户
|
// 检查数据库中是否已有该用户
|
||||||
// QueryWrapper<AccountExtend> qw = new QueryWrapper<>();
|
QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
|
||||||
// qw.lambda().eq(AccountExtend::getAuth, googleUser.getId());
|
queryWrapper.lambda().eq(Account::getUserEmail, googleUser.getEmail()); // 根据邮箱查询用户
|
||||||
// List<AccountExtend> accountExtends = accountExtendMapper.selectList(qw);
|
List<Account> accounts = accountMapper.selectList(queryWrapper);
|
||||||
// Account existingUser = findUserByGoogleId(googleUser.getId());
|
|
||||||
return "Login successful";
|
if (CollectionUtil.isNotEmpty(accounts)) {
|
||||||
// if (CollectionUtil.isNotEmpty(accountExtends)) {
|
// 用户已存在,直接登录
|
||||||
// // 用户已存在,直接登录
|
session.setAttribute("user", accounts.get(0));
|
||||||
//// session.setAttribute("user", existingUser);
|
return "Login successful";
|
||||||
// return "Login successful";
|
} else {
|
||||||
// } else {
|
// 用户不存在,创建新用户(自动注册)
|
||||||
// // 用户不存在,创建新用户(自动注册)
|
Account newUser = new Account();
|
||||||
//// User newUser = googleAuthService.registerNewUser(googleUser);
|
newUser.setUserEmail(googleUser.getEmail());
|
||||||
//// session.setAttribute("user", newUser);
|
newUser.setUserName(googleUser.getName());
|
||||||
// return "Registration and login successful";
|
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);
|
||||||
|
session.setAttribute("user", newUser);
|
||||||
|
|
||||||
|
return "Registration and login successful";
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "Error processing Google login: " + e.getMessage();
|
return "Error processing Google login: " + e.getMessage();
|
||||||
}
|
}
|
||||||
@@ -1887,6 +1899,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
|||||||
private static final String USER_INFO_URL = "https://www.googleapis.com/oauth2/v3/userinfo";
|
private static final String USER_INFO_URL = "https://www.googleapis.com/oauth2/v3/userinfo";
|
||||||
private static final String CLIENT_ID = "194770296147-njd68pm7tnapgonkj2h48mhf63n15n3f.apps.googleusercontent.com";
|
private static final String CLIENT_ID = "194770296147-njd68pm7tnapgonkj2h48mhf63n15n3f.apps.googleusercontent.com";
|
||||||
private static final String CLIENT_SECRET = "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";
|
private static final String CLIENT_SECRET = "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";
|
||||||
|
// private static final String REDIRECT_URI = "https://0551-117-143-125-51.ngrok-free.app/api/third/party/auth/google_callback";
|
||||||
private static final String REDIRECT_URI = "https://develop.api.aida.com.hk/api/third/party/auth/google_callback";
|
private static final String REDIRECT_URI = "https://develop.api.aida.com.hk/api/third/party/auth/google_callback";
|
||||||
|
|
||||||
public GoogleUser getGoogleUserFromCode(String code) {
|
public GoogleUser getGoogleUserFromCode(String code) {
|
||||||
|
|||||||
Reference in New Issue
Block a user