diff --git a/server/src/main/java/server/haengdong/application/KakaoClient.java b/server/src/main/java/server/haengdong/application/KakaoClient.java index 9dae17ef0..4981549b6 100644 --- a/server/src/main/java/server/haengdong/application/KakaoClient.java +++ b/server/src/main/java/server/haengdong/application/KakaoClient.java @@ -1,6 +1,5 @@ package server.haengdong.application; -import java.net.URI; import lombok.RequiredArgsConstructor; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.http.HttpHeaders; @@ -22,11 +21,11 @@ public class KakaoClient { private final KakaoProperties kakaoProperties; private final RestClient restClient; - public KakaoTokenResponse join(String code) { + public KakaoTokenResponse join(String code, String redirectUri) { MultiValueMap params = new LinkedMultiValueMap<>(); params.add("grant_type", "authorization_code"); params.add("client_id", kakaoProperties.clientId()); - params.add("redirect_uri", kakaoProperties.redirectUri()); + params.add("redirect_uri", redirectUri); params.add("code", code); try { @@ -41,7 +40,7 @@ public KakaoTokenResponse join(String code) { } } - public URI getKakaoPageURI() { - return URI.create(kakaoProperties.oauthCodeUri().formatted(kakaoProperties.clientId(), kakaoProperties.redirectUri())); + public String getClientId() { + return kakaoProperties.clientId(); } } diff --git a/server/src/main/java/server/haengdong/application/KakaoUserService.java b/server/src/main/java/server/haengdong/application/KakaoUserService.java index 9e57f3c43..6ba52f5b5 100644 --- a/server/src/main/java/server/haengdong/application/KakaoUserService.java +++ b/server/src/main/java/server/haengdong/application/KakaoUserService.java @@ -2,7 +2,6 @@ import com.auth0.jwt.JWT; import com.auth0.jwt.interfaces.DecodedJWT; -import java.net.URI; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import server.haengdong.application.response.KakaoTokenResponse; @@ -16,8 +15,8 @@ public class KakaoUserService { private final UserService userService; private final KakaoClient kakaoClient; - public Long joinByKakao(String code) { - KakaoTokenResponse kakaoToken = kakaoClient.join(code); + public Long joinByKakao(String code, String redirectUri) { + KakaoTokenResponse kakaoToken = kakaoClient.join(code, redirectUri); String idToken = kakaoToken.idToken(); DecodedJWT decodedJWT = JWT.decode(idToken); @@ -27,7 +26,7 @@ public Long joinByKakao(String code) { return userService.join(memberNumber, nickname); } - public URI getRedirectURI() { - return kakaoClient.getKakaoPageURI(); + public String getClientId() { + return kakaoClient.getClientId(); } } diff --git a/server/src/main/java/server/haengdong/config/KakaoProperties.java b/server/src/main/java/server/haengdong/config/KakaoProperties.java index a62121548..c4dcc32db 100644 --- a/server/src/main/java/server/haengdong/config/KakaoProperties.java +++ b/server/src/main/java/server/haengdong/config/KakaoProperties.java @@ -6,7 +6,6 @@ public record KakaoProperties( String baseUri, String clientId, - String redirectUri, String tokenRequestUri, String oauthCodeUri ) { diff --git a/server/src/main/java/server/haengdong/presentation/UserController.java b/server/src/main/java/server/haengdong/presentation/UserController.java index a3fad6bd5..7fc5ca533 100644 --- a/server/src/main/java/server/haengdong/presentation/UserController.java +++ b/server/src/main/java/server/haengdong/presentation/UserController.java @@ -1,13 +1,10 @@ package server.haengdong.presentation; import jakarta.validation.Valid; -import java.net.URI; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseCookie; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; @@ -21,6 +18,7 @@ import server.haengdong.config.Login; import server.haengdong.infrastructure.auth.CookieProperties; import server.haengdong.presentation.request.UserUpdateRequest; +import server.haengdong.presentation.response.KakaoClientId; @Slf4j @RequiredArgsConstructor @@ -33,38 +31,35 @@ public class UserController { private final AuthService authService; private final CookieProperties cookieProperties; - @Value("${login-success.uri}") - private String loginSuccessUri; - @PatchMapping("/api/admin/users") public ResponseEntity updateUser( @Login Long userId, @Valid @RequestBody UserUpdateRequest request ) { userService.updateUser(request.toAppRequest(userId)); - return ResponseEntity.ok().build(); } - @GetMapping("/api/login/kakao-page") - public ResponseEntity kakaoPage() { - URI redirectURI = kakaoUserService.getRedirectURI(); + @GetMapping("/api/kakao-client-id") + public ResponseEntity kakaoPage() { + String clientId = kakaoUserService.getClientId(); + KakaoClientId kakaoClientId = new KakaoClientId(clientId); - return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY) - .location(redirectURI) - .build(); + return ResponseEntity.ok(kakaoClientId); } @GetMapping("/api/login/kakao") - public ResponseEntity kakaoLogin(@RequestParam String code) { - log.info("Kakao login code: {}", code); - Long userId = kakaoUserService.joinByKakao(code); + public ResponseEntity kakaoLogin( + @RequestParam String code, + @RequestParam("redirect_uri") String redirectUri + ) { + log.info("Kakao login code, redirectUri: {}, {}", code, redirectUri); + Long userId = kakaoUserService.joinByKakao(code, redirectUri); String jwtToken = authService.createGuestToken(userId); ResponseCookie responseCookie = createResponseCookie(jwtToken); - return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY) + return ResponseEntity.ok() .header(HttpHeaders.SET_COOKIE, responseCookie.toString()) - .location(URI.create(loginSuccessUri)) .build(); } diff --git a/server/src/main/java/server/haengdong/presentation/response/KakaoClientId.java b/server/src/main/java/server/haengdong/presentation/response/KakaoClientId.java new file mode 100644 index 000000000..f3fa62992 --- /dev/null +++ b/server/src/main/java/server/haengdong/presentation/response/KakaoClientId.java @@ -0,0 +1,6 @@ +package server.haengdong.presentation.response; + +public record KakaoClientId( + String clientId +) { +} diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index 6298b671a..a6553aa14 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -72,14 +72,10 @@ server: kakao: base-uri: https://kauth.kakao.com - redirect-uri: http://localhost:8080/api/login/kakao token-request-uri: /oauth/token client-id: 52f24834ff7304ed2c47294b3f57b053 oauth-code-uri: https://kauth.kakao.com/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code&scope=openid -login-success: - uri: https://dev.haengdong.pro/event/create - --- spring: diff --git a/server/src/main/resources/config b/server/src/main/resources/config index 6cc0c6a58..9b8b97777 160000 --- a/server/src/main/resources/config +++ b/server/src/main/resources/config @@ -1 +1 @@ -Subproject commit 6cc0c6a583932dea92a8e98e86ecf7b5b350d286 +Subproject commit 9b8b97777048f7afa19bacac7da704d91adabdb1