Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.dnd.moddo.domain.auth.controller;

import java.io.IOException;
import java.util.Collections;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand All @@ -21,6 +23,7 @@
import com.dnd.moddo.global.jwt.dto.TokenResponse;
import com.dnd.moddo.global.jwt.service.JwtService;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -52,13 +55,18 @@ public RefreshResponse reissueAccessToken(@RequestHeader(value = "Authorization"
}

@GetMapping("/login/oauth2/callback")
public ResponseEntity<Void> kakaoLoginCallback(@RequestParam @NotBlank String code) {
public ResponseEntity<?> kakaoLoginCallback(@RequestParam @NotBlank String code,
HttpServletResponse response) throws
IOException {

TokenResponse tokenResponse = authService.loginOrRegisterWithKakao(code);

String cookie = createCookie("accessToken", tokenResponse.accessToken()).toString();
response.addHeader("Set-Cookie", cookie);
response.sendRedirect("https://www.moddo.kr");
Comment on lines +65 to +66
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

하드코딩된 URL을 설정값으로 분리하고 보안을 고려해주세요.

현재 구현에서 몇 가지 개선점이 있습니다:

  1. 하드코딩된 URL: "https://www.moddo.kr"이 하드코딩되어 있어 환경별 설정이 어렵습니다.
  2. 보안 고려사항: 외부 도메인으로의 리다이렉트 시 검증이 필요할 수 있습니다.

다음과 같이 개선을 제안합니다:

+@Value("${app.client.redirect-url:https://www.moddo.kr}")
+private String clientRedirectUrl;

 public void kakaoLoginCallback(@RequestParam @NotBlank String code,
     HttpServletResponse response) throws IOException {
     
     TokenResponse tokenResponse = authService.loginOrRegisterWithKakao(code);
     
     String cookie = createCookie("accessToken", tokenResponse.accessToken()).toString();
     response.addHeader("Set-Cookie", cookie);
-    response.sendRedirect("https://www.moddo.kr");
+    response.sendRedirect(clientRedirectUrl);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
response.addHeader("Set-Cookie", cookie);
response.sendRedirect("https://www.moddo.kr");
// Add this at the top of your controller class (and import org.springframework.beans.factory.annotation.Value)
@Value("${app.client.redirect-url:https://www.moddo.kr}")
private String clientRedirectUrl;
public void kakaoLoginCallback(@RequestParam @NotBlank String code,
HttpServletResponse response) throws IOException {
TokenResponse tokenResponse = authService.loginOrRegisterWithKakao(code);
String cookie = createCookie("accessToken", tokenResponse.accessToken()).toString();
response.addHeader("Set-Cookie", cookie);
response.sendRedirect(clientRedirectUrl);
}
🤖 Prompt for AI Agents
In src/main/java/com/dnd/moddo/domain/auth/controller/AuthController.java at
lines 64-65, the redirect URL "https://www.moddo.kr" is hardcoded, which reduces
flexibility and may pose security risks. To fix this, externalize the URL into a
configuration property (e.g., application.properties or environment variable)
and inject it into the controller. Additionally, implement validation to ensure
the redirect URL is safe and allowed before calling response.sendRedirect,
preventing open redirect vulnerabilities.


return ResponseEntity.ok()
return ResponseEntity.status(HttpStatus.FOUND)
.header(HttpHeaders.LOCATION, "https://www.moddo.kr")
.header(HttpHeaders.SET_COOKIE, cookie)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public KakaoTokenResponse join(String code) {
.body(KakaoTokenResponse.class);

} catch (RestClientResponseException e) {
log.error("[KAKAO_API][GET_TOKEN][HTTP_ERROR] HTTP 에러 발생: status={}, body={}", e.getStatusCode(),
e.getResponseBodyAsString());
log.error("[KAKAO_API][GET_PROFILE][HTTP_ERROR] HTTP 에러 발생: status={}, body={}, error={}",
e.getStatusCode(), e.getResponseBodyAsString(), e.getMessage());
throw new ModdoException(HttpStatus.INTERNAL_SERVER_ERROR, "카카오 API HTTP 에러");
} catch (Exception e) {
log.info("[USER_LOGIN_FAIL] 로그인 실패 : code = {}", code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void kakaoLoginCallback() throws Exception {
//when & then
mockMvc.perform(get("/api/v1/login/oauth2/callback")
.param("code", "test code"))
.andExpect(status().isOk())
.andExpect(status().is3xxRedirection())
.andDo(document("login",
queryParameters(
parameterWithName("code").description("카카오 인가 코드")
Expand Down
Loading