google登入验证

This commit is contained in:
lu7213 committed 2026-10-14 21:01:36 -04:00
1 parent f69926dee6
commit d014cd15f0
10 files changed
+569 -1

No files matched your search

@@ -0,0 +1,83 @@
package com.ruoyi.web.controller.system;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.GoogleOauthService;
import com.ruoyi.system.domain.SysUserGoogleOauth;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;
import java.util.HashMap;
import java.util.Map;
/** Google OpenID Connect登录与当前本地账号绑定接口。 */
@RestController
@RequestMapping("/auth/google")
public class GoogleOauthController {
private final GoogleOauthService googleOauthService;
public GoogleOauthController(GoogleOauthService googleOauthService) {
this.googleOauthService = googleOauthService;
}
/** 匿名登录授权入口,只返回Google官方授权地址。 */
@GetMapping("/login-url")
public AjaxResult loginUrl() {
AjaxResult result = AjaxResult.success();
result.put("enabled", googleOauthService.isEnabled());
if (googleOauthService.isEnabled()) {
result.put("authorizationUrl", googleOauthService.buildAuthorizationUrl("LOGIN", null));
}
return result;
}
/** 已登录用户发起绑定,绑定目标用户ID从当前JWT读取。 */
@GetMapping("/bind-url")
public AjaxResult bindUrl() {
return AjaxResult.success(googleOauthService.buildAuthorizationUrl("BIND", SecurityUtils.getUserId()));
}
/** Google控制台登记的固定回调地址。 */
@GetMapping("/callback")
public RedirectView callback(@RequestParam(value = "code", required = false) String code,
@RequestParam(value = "state", required = false) String state,
@RequestParam(value = "error", required = false) String error) {
return new RedirectView(googleOauthService.handleCallback(code, state, error));
}
/** 前端使用60秒一次性票据换取原有RuoYi系统JWT。 */
@PostMapping("/exchange")
public AjaxResult exchange(@RequestBody Map<String, String> request) {
AjaxResult result = AjaxResult.success();
result.put(Constants.TOKEN, googleOauthService.exchangeTicket(request.get("ticket")));
return result;
}
@GetMapping("/binding")
public AjaxResult binding() {
SysUserGoogleOauth binding = googleOauthService.getBinding(SecurityUtils.getUserId());
Map<String, Object> view = new HashMap<>();
view.put("enabled", googleOauthService.isEnabled());
view.put("bound", binding != null);
if (binding != null) {
view.put("email", binding.getEmail());
view.put("displayName", binding.getDisplayName());
view.put("avatarUrl", binding.getAvatarUrl());
view.put("createdAt", binding.getCreatedAt());
}
return AjaxResult.success(view);
}
@DeleteMapping("/binding")
public AjaxResult unbind() {
googleOauthService.unbind(SecurityUtils.getUserId());
return AjaxResult.success("Google账号已解绑");
}
}