Skip to content

Commit f7e7647

Browse files
authored
Merge pull request #261 from CodIN-INU/fix/login-redirect-issue
fix: redirect 요청 세분화
2 parents 1b2a096 + e520ff8 commit f7e7647

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/main/java/inu/codin/codin/common/security/controller/AuthController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ public class AuthController {
3232
@GetMapping("/google")
3333
public ResponseEntity<SingleResponse<?>> googleLogin(
3434
HttpServletResponse response,
35-
@RequestParam(required = false, value = "redirect_url") String redirect_url) throws IOException {
36-
authSessionService.setSession(redirect_url);
35+
@RequestParam(required = false, value = "redirect_host") String redirect_host,
36+
@RequestParam(required = false, value = "redirect_path") String redirect_path
37+
) throws IOException {
38+
authSessionService.setSession(redirect_host);
39+
authSessionService.setSession(redirect_path);
3740
response.sendRedirect("/api/oauth2/authorization/google");
3841
return ResponseEntity.ok()
3942
.body(new SingleResponse<>(200, "google OAuth2 Login Redirect",null));

src/main/java/inu/codin/codin/common/security/util/OAuth2LoginSuccessHandler.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,42 @@ private void handleLoginResult(HttpServletRequest request, HttpServletResponse r
5555
response.setContentType("application/json;charset=UTF-8");
5656
PrintWriter writer = response.getWriter();
5757

58-
String redirectUrl = (String) request.getSession().getAttribute("redirect_url");
59-
if (Objects.equals(redirectUrl, null)) redirectUrl = BASEURL;
60-
request.getSession().removeAttribute("redirect_url");
58+
String redirectHost = (String) request.getSession().getAttribute("redirect_host");
59+
String redirectPath = (String) request.getSession().getAttribute("redirect_path");
60+
61+
boolean isPathExists = true;
62+
if (Objects.isNull(redirectHost)) {
63+
redirectHost = BASEURL;
64+
}
65+
if (Objects.isNull(redirectPath)) {
66+
isPathExists = false;
67+
}
68+
request.getSession().removeAttribute("redirect_host");
69+
request.getSession().removeAttribute("redirect_path");
6170

6271
switch (result) {
6372
case LOGIN_SUCCESS -> {
64-
getRedirectStrategy().sendRedirect(request, response, redirectUrl);
73+
if (isPathExists) {
74+
getRedirectStrategy().sendRedirect(request, response, redirectHost + redirectPath);
75+
} else {
76+
getRedirectStrategy().sendRedirect(request, response, redirectHost + "/main");
77+
}
6578
log.info("{\"code\":200, \"message\":\"정상 로그인 완료: {}\"}", email);
6679
}
6780
case NEW_USER_REGISTERED -> {
68-
getRedirectStrategy().sendRedirect(request, response, redirectUrl + "/auth/profile?email=" + email);
81+
getRedirectStrategy().sendRedirect(request, response, redirectHost + "/auth/profile?email=" + email);
6982
log.info("{\"code\":201, \"message\":\"신규 회원 등록 완료: {}\"}", email);
7083
}
7184
case PROFILE_INCOMPLETE -> {
72-
getRedirectStrategy().sendRedirect(request, response, redirectUrl + "/auth/profile?email=" + email);
85+
getRedirectStrategy().sendRedirect(request, response, redirectHost + "/auth/profile?email=" + email);
7386
log.info("{\"code\":200, \"message\":\"회원 프로필 설정 미완료: {}\"}", email);
7487
}
7588
case SUSPENDED_USER -> {
76-
getRedirectStrategy().sendRedirect(request, response, redirectUrl + "/api/suspends");
89+
getRedirectStrategy().sendRedirect(request, response, redirectHost + "/api/suspends");
7790
log.info("{\"code\":200, \"message\":\"정지된 회원: {}\"}", email);
7891
}
7992
default -> {
80-
getRedirectStrategy().sendRedirect(request, response, redirectUrl + "/login");
93+
getRedirectStrategy().sendRedirect(request, response, redirectHost + "/login");
8194
log.info("{\"code\":500, \"message\":\"알 수 없는 오류 발생: {}\"}", email);
8295
}
8396
}

0 commit comments

Comments
 (0)