Skip to content

Commit 8224e74

Browse files
committed
fix(oauth): GitHub 回调缺参数时兜底重定向到登录页
之前 @RequestParam(required=true) 让 Spring 在进入方法前抛 MissingServletRequestParameterException,用户直接访问回调地址看到 500 白屏。 改 required=false + null 检查,统一重定向到前端 /login?error=oauth_failed。 顺便修 OAuthControllerIntegrationTests#callbackWithoutParametersRedirectsToFrontendErrorPage。
1 parent fdc376a commit 8224e74

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/main/java/com/involutionhell/backend/usercenter/controller/OAuthController.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,18 @@ public void renderAuth(HttpServletResponse response) throws IOException {
6969
* GitHub → localhost:3000/api/auth/callback/github → Next.js rewrite → localhost:8080/api/auth/callback/github
7070
*/
7171
@GetMapping("/api/auth/callback/github")
72-
public void login(@RequestParam String code,
73-
@RequestParam String state,
72+
public void login(@RequestParam(required = false) String code,
73+
@RequestParam(required = false) String state,
7474
HttpServletResponse response) throws IOException {
75+
// 参数缺失时直接走失败分支:若 @RequestParam 保持 required=true,Spring 在进入方法前
76+
// 就抛 MissingServletRequestParameterException → 默认 500 白屏;
77+
// 手动 null check 能把 "用户直接访问 / GitHub 异常回调" 统一兜底到前端错误页。
78+
if (code == null || state == null) {
79+
log.warn("[OAuth] GitHub callback missing code/state (direct access?), redirecting to error page");
80+
response.sendRedirect(frontEndUrl + "/login?error=oauth_failed");
81+
return;
82+
}
83+
7584
AuthCallback callback = new AuthCallback();
7685
callback.setCode(code);
7786
callback.setState(state);

0 commit comments

Comments
 (0)