TASK:谷歌登录
This commit is contained in:
@@ -51,7 +51,7 @@ public class AuthenticationFilter extends OncePerRequestFilter {
|
||||
"/api/python/flush","/api/account/healthy","/api/ali-pay/trade/notify","/api/paypal/ipn/back","/api/alipay-hk/trade/notify",
|
||||
"/api/portfolio/page", "/api/portfolio/detail", "/api/portfolio/commentPage", "/api/portfolio/viewsIncrease",
|
||||
"/api/account/designWorksRegister","/api/account/questionnaire","/api/stripe/trade/notify",
|
||||
"/notification","/api/account/activateNewEmail"
|
||||
"/notification","/api/account/activateNewEmail","/api/third/party/auth/google_callback"
|
||||
);
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.validation.Valid;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
@@ -114,4 +115,10 @@ public class ThirdPartyController {
|
||||
public Response<String> updateNoLoginRequiredNew(@RequestBody NoLoginRequiredDTO noLoginRequiredDTO, HttpServletRequest request) {
|
||||
return Response.success(accountService.updateNoLoginRequiredNew(noLoginRequiredDTO, request));
|
||||
}
|
||||
|
||||
@CrossOrigin
|
||||
@GetMapping("/auth/google_callback")
|
||||
public Response<String> googleCallback(@RequestParam("code") String code, HttpSession session) {
|
||||
return Response.success(accountService.googleCallback(code, session));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.ai.da.mapper.primary.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("account_extend")
|
||||
public class AccountExtend implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private Long accountId;
|
||||
|
||||
private String authType;
|
||||
|
||||
private String auth;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.ai.da.mapper.primary.entity;
|
||||
|
||||
import com.ai.da.common.config.mybatis.plus.CommonMapper;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Mapper 接口
|
||||
*
|
||||
* @author easy-generator
|
||||
* @since 2022-06-13
|
||||
*/
|
||||
public interface AccountExtendMapper extends CommonMapper<AccountExtend> {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.ai.da.mapper.primary.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class GoogleUser {
|
||||
private String id;
|
||||
private String email;
|
||||
private String name;
|
||||
}
|
||||
10
src/main/java/com/ai/da/model/vo/GoogleTokenResponse.java
Normal file
10
src/main/java/com/ai/da/model/vo/GoogleTokenResponse.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.ai.da.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class GoogleTokenResponse {
|
||||
private String accessToken;
|
||||
private String idToken;
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -181,4 +182,6 @@ public interface AccountService extends IService<Account> {
|
||||
String updateNoLoginRequiredNew(NoLoginRequiredDTO noLoginRequiredDTO, HttpServletRequest request);
|
||||
|
||||
void halfPricePromotion();
|
||||
|
||||
String googleCallback(String code, HttpSession session);
|
||||
}
|
||||
|
||||
@@ -34,10 +34,12 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.sql.DataSource;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
@@ -62,6 +64,9 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
@Resource
|
||||
private AccountMapper accountMapper;
|
||||
|
||||
@Resource
|
||||
private AccountExtendMapper accountExtendMapper;
|
||||
|
||||
@Resource
|
||||
private JWTTokenHelper jwtTokenHelper;
|
||||
|
||||
@@ -1822,4 +1827,82 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
log.info("邮箱绑定更改完成,用户id:{},新邮箱:{}", accountId, newMailbox);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String googleCallback(String code, HttpSession session) {
|
||||
try {
|
||||
log.info("code:" + code);
|
||||
// 使用 code 获取 Google 用户信息
|
||||
GoogleUser googleUser = getGoogleUserFromCode(code);
|
||||
log.info("googleUser:" + JSON.toJSONString(googleUser));
|
||||
|
||||
// 检查数据库中是否已有该用户
|
||||
// QueryWrapper<AccountExtend> qw = new QueryWrapper<>();
|
||||
// qw.lambda().eq(AccountExtend::getAuth, googleUser.getId());
|
||||
// List<AccountExtend> accountExtends = accountExtendMapper.selectList(qw);
|
||||
// Account existingUser = findUserByGoogleId(googleUser.getId());
|
||||
return "Login successful";
|
||||
// if (CollectionUtil.isNotEmpty(accountExtends)) {
|
||||
// // 用户已存在,直接登录
|
||||
//// session.setAttribute("user", existingUser);
|
||||
// return "Login successful";
|
||||
// } else {
|
||||
// // 用户不存在,创建新用户(自动注册)
|
||||
//// User newUser = googleAuthService.registerNewUser(googleUser);
|
||||
//// session.setAttribute("user", newUser);
|
||||
// return "Registration and login successful";
|
||||
// }
|
||||
} catch (Exception e) {
|
||||
return "Error processing Google login: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private static final String TOKEN_URL = "https://oauth2.googleapis.com/token";
|
||||
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_SECRET = "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";
|
||||
private static final String REDIRECT_URI = "https://www.api.aida.com.hk/api/third/party/auth/google_callback";
|
||||
|
||||
public GoogleUser getGoogleUserFromCode(String code) {
|
||||
// Step 1: Exchange code for access_token
|
||||
String accessToken = getAccessToken(code);
|
||||
log.info("accessToken" + accessToken);
|
||||
|
||||
// Step 2: Use access_token to get Google User info
|
||||
return getGoogleUserInfo(accessToken);
|
||||
}
|
||||
|
||||
private String getAccessToken(String code) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("client_id", CLIENT_ID);
|
||||
params.put("client_secret", CLIENT_SECRET);
|
||||
params.put("redirect_uri", REDIRECT_URI);
|
||||
params.put("grant_type", "authorization_code");
|
||||
params.put("code", code);
|
||||
|
||||
// 使用 RestTemplate 发起请求以获取 access_token
|
||||
GoogleTokenResponse response = restTemplate.postForObject(TOKEN_URL, params, GoogleTokenResponse.class);
|
||||
return response.getAccessToken();
|
||||
}
|
||||
|
||||
private GoogleUser getGoogleUserInfo(String accessToken) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String url = USER_INFO_URL + "?access_token=" + accessToken;
|
||||
return restTemplate.getForObject(url, GoogleUser.class);
|
||||
}
|
||||
|
||||
// public User findUserByGoogleId(String googleId) {
|
||||
// // 从数据库中根据 Google ID 查找用户
|
||||
// return userRepository.findByGoogleId(googleId);
|
||||
// }
|
||||
|
||||
// public User registerNewUser(GoogleUser googleUser) {
|
||||
// // 创建并保存新用户
|
||||
// User newUser = new User();
|
||||
// newUser.setGoogleId(googleUser.getId());
|
||||
// newUser.setEmail(googleUser.getEmail());
|
||||
// newUser.setUsername(googleUser.getName());
|
||||
// return userRepository.save(newUser);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user