From 285a297296077d8259e77681b349768d116eb467 Mon Sep 17 00:00:00 2001 From: ajeong Date: Thu, 26 Dec 2024 23:11:05 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20Redirect=20URL=20=EC=88=98=EC=A0=95=20#1?= =?UTF-8?q?49?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/KakaoLoginController.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/WELLET/welletServer/kakaologin/controller/KakaoLoginController.java b/src/main/java/WELLET/welletServer/kakaologin/controller/KakaoLoginController.java index 5b508e0..6be149e 100644 --- a/src/main/java/WELLET/welletServer/kakaologin/controller/KakaoLoginController.java +++ b/src/main/java/WELLET/welletServer/kakaologin/controller/KakaoLoginController.java @@ -68,7 +68,7 @@ public String login(HttpServletResponse response) throws IOException { @GetMapping("/auth/kakao/callback") - public String callback(@RequestParam("code") String code, HttpServletResponse response) throws IOException { + public String callback(@RequestParam("code") String code, HttpServletResponse response, HttpServletRequest request) throws IOException { try { // 1. 카카오에서 accessToken 받아오기 String accessToken = kakaoService.getAccessTokenFromKakao(code); @@ -92,7 +92,7 @@ public String callback(@RequestParam("code") String code, HttpServletResponse re String jwtToken = jwtService.generateToken(member); // 생성된 또는 업데이트된 사용자로 JWT 생성 // //// // 5. 쿠키에 JWT 저장 - Cookie jwtCookie = new Cookie("jwtToken", jwtToken); + Cookie jwtCookie = new Cookie("Set-Cookie", jwtToken); jwtCookie.setHttpOnly(true); // JavaScript로 쿠키에 접근 불가 jwtCookie.setSecure(true); // HTTPS에서만 전송 jwtCookie.setMaxAge(60 * 60 * 24); // 쿠키 유효 시간 설정 @@ -127,10 +127,8 @@ public String callback(@RequestParam("code") String code, HttpServletResponse re // 리다이렉트 URL 설정 - String redirectUrl = frontendUrl + "/home"; // 원하는 프론트엔드 URL + String redirectUrl = getFrontendUrl(request) + "/home"; response.sendRedirect(redirectUrl); - - return "로그인 성공"; } catch (Exception e) { @@ -138,6 +136,29 @@ public String callback(@RequestParam("code") String code, HttpServletResponse re return "로그인 처리 중 오류" + HttpStatus.INTERNAL_SERVER_ERROR; } } + + private String getFrontendUrl(HttpServletRequest request) { + String redirectUrl; + + String origin = request.getHeader("referer"); // 요청의 출처를 가져옵니다. + + if (origin != null) { + // 요청의 출처가 localhost일 경우 + if (origin.contains("localhost:8000")) { + redirectUrl = "http://localhost:8000"; + } + // 요청의 출처가 wellet.com일 경우 + else if (origin.contains(frontendUrl)) { + redirectUrl = frontendUrl; + } else { + redirectUrl = frontendUrl; + } + } else { + redirectUrl = frontendUrl; + } + + return redirectUrl; + } }