From 68b312c72810f21c8756671fb9f377fa2c92780a Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 22 Jul 2024 00:52:31 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=EC=97=90=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80,=20Response=20=EC=83=9D=EC=84=B1=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=20=EC=BA=A1=EC=8A=90=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 카카오 닉네임, 프로필 이미지 경로 --- .../touroot/authentication/dto/LoginResponse.java | 12 +++++++++++- .../touroot/authentication/service/LoginService.java | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/woowacourse/touroot/authentication/dto/LoginResponse.java b/backend/src/main/java/woowacourse/touroot/authentication/dto/LoginResponse.java index 156f99fc..a4cccb33 100644 --- a/backend/src/main/java/woowacourse/touroot/authentication/dto/LoginResponse.java +++ b/backend/src/main/java/woowacourse/touroot/authentication/dto/LoginResponse.java @@ -1,4 +1,14 @@ package woowacourse.touroot.authentication.dto; -public record LoginResponse(String accessToken) { +import io.swagger.v3.oas.annotations.media.Schema; +import woowacourse.touroot.member.domain.Member; + +public record LoginResponse( + @Schema(description = "로그인된 유저의 닉네임") String nickname, + @Schema(description = "로그인된 유저의 프로필 이미지 경로") String profileImageUrl, + @Schema(description = "인가에 필요한 accessToken") String accessToken) { + + public static LoginResponse of(Member member, String accessToken) { + return new LoginResponse(member.getNickname(), member.getProfileImageUri(), accessToken); + } } diff --git a/backend/src/main/java/woowacourse/touroot/authentication/service/LoginService.java b/backend/src/main/java/woowacourse/touroot/authentication/service/LoginService.java index 9f02b4e7..abb1a04a 100644 --- a/backend/src/main/java/woowacourse/touroot/authentication/service/LoginService.java +++ b/backend/src/main/java/woowacourse/touroot/authentication/service/LoginService.java @@ -22,9 +22,9 @@ public LoginResponse login(String code) { Member member = memberRepository.findByKakaoId(userInformation.socialLoginId()) .orElseGet(() -> signUp(userInformation)); - return new LoginResponse(tokenProvider.createToken(member)); + return LoginResponse.of(member, tokenProvider.createToken(member)); } - + private Member signUp(OauthUserInformationResponse userInformation) { return memberRepository.save( new Member(userInformation.socialLoginId(), userInformation.nickname(), userInformation.profileImage()) From 5312823020ce678d3c2d7f81835f259451a6a9d0 Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 22 Jul 2024 01:44:20 +0900 Subject: [PATCH 2/5] =?UTF-8?q?refactor:=20RestClient=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infrastructure/KakaoOauthClient.java | 15 +++++++++++++++ .../touroot/global/exception/ClientException.java | 8 ++++++++ .../global/exception/GlobalExceptionHandler.java | 8 ++++++++ 3 files changed, 31 insertions(+) create mode 100644 backend/src/main/java/woowacourse/touroot/global/exception/ClientException.java diff --git a/backend/src/main/java/woowacourse/touroot/authentication/infrastructure/KakaoOauthClient.java b/backend/src/main/java/woowacourse/touroot/authentication/infrastructure/KakaoOauthClient.java index ccb2c1a3..461bea3b 100644 --- a/backend/src/main/java/woowacourse/touroot/authentication/infrastructure/KakaoOauthClient.java +++ b/backend/src/main/java/woowacourse/touroot/authentication/infrastructure/KakaoOauthClient.java @@ -1,18 +1,24 @@ package woowacourse.touroot.authentication.infrastructure; +import java.io.IOException; import java.time.Duration; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.client.ClientHttpRequestFactories; import org.springframework.boot.web.client.ClientHttpRequestFactorySettings; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpRequest; +import org.springframework.http.HttpStatusCode; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestClient; import woowacourse.touroot.authentication.dto.KakaoAccessTokenResponse; import woowacourse.touroot.authentication.dto.OauthUserInformationResponse; +import woowacourse.touroot.global.exception.BadRequestException; +import woowacourse.touroot.global.exception.ClientException; @Component public class KakaoOauthClient { @@ -55,6 +61,7 @@ public OauthUserInformationResponse requestUserInformation(String authorizationC .uri(userInformationRequestUri) .header(HttpHeaders.AUTHORIZATION, "Bearer " + kakaoAccessTokenResponse.accessToken()) .retrieve() + .onStatus(HttpStatusCode::isError, this::handleClientError) .toEntity(OauthUserInformationResponse.class) .getBody(); } @@ -71,7 +78,15 @@ private KakaoAccessTokenResponse requestAccessToken(String authorizationCode) { .contentType(MediaType.APPLICATION_FORM_URLENCODED) .body(params) .retrieve() + .onStatus(HttpStatusCode::isError, this::handleClientError) .toEntity(KakaoAccessTokenResponse.class) .getBody(); } + + private void handleClientError(HttpRequest request, ClientHttpResponse response) throws IOException { + if (response.getStatusCode().is4xxClientError()) { + throw new BadRequestException("잘못된 로그인 요청입니다. 인가코드를 확인해주세요"); + } + throw new ClientException("외부 서비스의 장애로 카카오로그인을 이용할 수 없습니다"); + } } diff --git a/backend/src/main/java/woowacourse/touroot/global/exception/ClientException.java b/backend/src/main/java/woowacourse/touroot/global/exception/ClientException.java new file mode 100644 index 00000000..5abf4cd8 --- /dev/null +++ b/backend/src/main/java/woowacourse/touroot/global/exception/ClientException.java @@ -0,0 +1,8 @@ +package woowacourse.touroot.global.exception; + +public class ClientException extends RuntimeException { + + public ClientException(String message) { + super(message); + } +} diff --git a/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java b/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java index c8f0872e..6e2bd4f3 100644 --- a/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java @@ -34,4 +34,12 @@ public ResponseEntity handleMethodArgumentNotValidException( return ResponseEntity.badRequest() .body(data); } + + @ExceptionHandler(ClientException.class) + public ResponseEntity handleClientException(ClientException exception) { + log.error("CLIENT_EXCEPTION :: message = {}", exception.getMessage()); + + ExceptionResponse data = new ExceptionResponse(exception.getMessage()); + return ResponseEntity.internalServerError().body(data); + } } From e69c133bba6b28221a8f7683f5ce2da84d9bf855 Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 22 Jul 2024 01:47:01 +0900 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=EC=84=9C=EB=B2=84=20=EB=82=B4?= =?UTF-8?q?=EB=B6=80=20=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC=20=ED=95=B8?= =?UTF-8?q?=EB=93=A4=EB=9F=AC=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../touroot/global/exception/GlobalExceptionHandler.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java b/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java index 6e2bd4f3..c4963999 100644 --- a/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java @@ -42,4 +42,12 @@ public ResponseEntity handleClientException(ClientException e ExceptionResponse data = new ExceptionResponse(exception.getMessage()); return ResponseEntity.internalServerError().body(data); } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleInternalServerError(Exception exception) { + log.error("INTERNAl_SERVER_ERROR :: message = {}", exception); + + ExceptionResponse data = new ExceptionResponse("서버 내부적으로 에러가 발생하였습니다. 서버 관리자에게 문의하세요"); + return ResponseEntity.internalServerError().body(data); + } } From d8a50bf5157b5c838dd963ec38342ffbf85269d4 Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 22 Jul 2024 01:58:28 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20Swagger=20api=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LoginController.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/backend/src/main/java/woowacourse/touroot/authentication/controller/LoginController.java b/backend/src/main/java/woowacourse/touroot/authentication/controller/LoginController.java index 40fc3fae..abb762b8 100644 --- a/backend/src/main/java/woowacourse/touroot/authentication/controller/LoginController.java +++ b/backend/src/main/java/woowacourse/touroot/authentication/controller/LoginController.java @@ -1,5 +1,10 @@ package woowacourse.touroot.authentication.controller; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -8,7 +13,9 @@ import org.springframework.web.bind.annotation.RestController; import woowacourse.touroot.authentication.dto.LoginResponse; import woowacourse.touroot.authentication.service.LoginService; +import woowacourse.touroot.global.exception.dto.ExceptionResponse; +@Tag(name = "로그인") @RequiredArgsConstructor @RestController @RequestMapping("/api/v1/login") @@ -16,6 +23,16 @@ public class LoginController { private final LoginService loginService; + @Operation( + summary = "카카오 로그인", + responses = { + @ApiResponse( + responseCode = "400", + description = "유효하지 않은 인가 코드로 로그인 요청을 했을 때", + content = @Content(schema = @Schema(implementation = ExceptionResponse.class)) + ) + } + ) @GetMapping("/oauth/kakao") public ResponseEntity login(@RequestParam(name = "code") String authorizationCode) { return ResponseEntity.ok() From 90c8cfd4f30c2301912fefee3163aed0e5fd58a1 Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 22 Jul 2024 02:00:06 +0900 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20=EC=B5=9C=EC=83=81=EC=9C=84=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../touroot/global/exception/GlobalExceptionHandler.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java b/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java index c4963999..6e2bd4f3 100644 --- a/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/woowacourse/touroot/global/exception/GlobalExceptionHandler.java @@ -42,12 +42,4 @@ public ResponseEntity handleClientException(ClientException e ExceptionResponse data = new ExceptionResponse(exception.getMessage()); return ResponseEntity.internalServerError().body(data); } - - @ExceptionHandler(Exception.class) - public ResponseEntity handleInternalServerError(Exception exception) { - log.error("INTERNAl_SERVER_ERROR :: message = {}", exception); - - ExceptionResponse data = new ExceptionResponse("서버 내부적으로 에러가 발생하였습니다. 서버 관리자에게 문의하세요"); - return ResponseEntity.internalServerError().body(data); - } }