From d02cf19044bcdab411438cc53bf03fa92789dd46 Mon Sep 17 00:00:00 2001 From: LeeJiWon Date: Tue, 10 Sep 2024 14:52:29 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20proto=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?=EB=B0=8F=20=EC=97=90=EB=9F=AC=20=ED=95=B8=EB=93=A4=EB=A7=81=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20-=20proto=20=ED=8C=8C=EC=9D=BC=20response?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=20-=20gRPC=20=EC=82=AC=EC=9A=A9=20?= =?UTF-8?q?=EC=8B=9C=20=EB=B0=9C=EC=83=9D=ED=95=98=EB=8A=94=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=ED=95=B8=EB=93=A4=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wanted/gold/client/AuthGrpcClient.java | 18 +++++++++++++++--- .../gold/client/dto/UserResponseDto.java | 7 +++++++ .../com/wanted/gold/exception/ErrorCode.java | 1 + .../gold/exception/UnauthorizedException.java | 12 ++++++++++++ .../handler/GlobalExceptionHandler.java | 11 +++++++---- .../gold/order/controller/OrderController.java | 4 +++- .../gold/order/service/OrderService.java | 7 +++++-- src/main/proto/auth.proto | 1 + 8 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/wanted/gold/client/dto/UserResponseDto.java create mode 100644 src/main/java/com/wanted/gold/exception/UnauthorizedException.java diff --git a/src/main/java/com/wanted/gold/client/AuthGrpcClient.java b/src/main/java/com/wanted/gold/client/AuthGrpcClient.java index 4bced58..91f53ef 100644 --- a/src/main/java/com/wanted/gold/client/AuthGrpcClient.java +++ b/src/main/java/com/wanted/gold/client/AuthGrpcClient.java @@ -1,12 +1,17 @@ package com.wanted.gold.client; +import com.wanted.gold.client.dto.UserResponseDto; +import com.wanted.gold.exception.*; import com.wanted.gold.grpc.AuthRequest; import com.wanted.gold.grpc.AuthResponse; import com.wanted.gold.grpc.AuthServiceGrpc; import io.grpc.ManagedChannel; +import io.grpc.Status; import io.grpc.StatusRuntimeException; import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; +import org.springframework.web.server.ResponseStatusException; @Component public class AuthGrpcClient { @@ -19,15 +24,22 @@ public AuthGrpcClient() { blockingStub = AuthServiceGrpc.newBlockingStub(channel); } - public String getUserId(String accessToken) { + public UserResponseDto getUserIdAndRole(String accessToken) { AuthRequest request = AuthRequest.newBuilder() .setAccessToken(accessToken) .build(); try { AuthResponse response = blockingStub.authCall(request); - return response.getUserId(); + return new UserResponseDto(response.getUserId(), response.getRole()); } catch (StatusRuntimeException e) { - throw new RuntimeException(e.getStatus().getDescription(), e); + if(e.getStatus().getCode() == Status.Code.UNAUTHENTICATED) // 401 + throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, e.getStatus().getDescription()); + else if(e.getStatus().getCode() == Status.Code.NOT_FOUND) // 404 + throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getStatus().getDescription()); + else if(e.getStatus().getCode() == Status.Code.INVALID_ARGUMENT) // 400 + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getStatus().getDescription()); + else // 500 + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getStatus().getDescription()); } } } diff --git a/src/main/java/com/wanted/gold/client/dto/UserResponseDto.java b/src/main/java/com/wanted/gold/client/dto/UserResponseDto.java new file mode 100644 index 0000000..a48d3e7 --- /dev/null +++ b/src/main/java/com/wanted/gold/client/dto/UserResponseDto.java @@ -0,0 +1,7 @@ +package com.wanted.gold.client.dto; + +public record UserResponseDto( + String userId, + String role +) { +} diff --git a/src/main/java/com/wanted/gold/exception/ErrorCode.java b/src/main/java/com/wanted/gold/exception/ErrorCode.java index 26ac735..5b916d9 100644 --- a/src/main/java/com/wanted/gold/exception/ErrorCode.java +++ b/src/main/java/com/wanted/gold/exception/ErrorCode.java @@ -9,6 +9,7 @@ public enum ErrorCode { // 기본 BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), + UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "유효하지 않은 접근입니다."), // 주문 QUANTITY_TOO_MANY(HttpStatus.BAD_REQUEST, "주문할 수 없는 수량입니다. 수량을 다시 확인해주세요."), diff --git a/src/main/java/com/wanted/gold/exception/UnauthorizedException.java b/src/main/java/com/wanted/gold/exception/UnauthorizedException.java new file mode 100644 index 0000000..95d07c5 --- /dev/null +++ b/src/main/java/com/wanted/gold/exception/UnauthorizedException.java @@ -0,0 +1,12 @@ +package com.wanted.gold.exception; + +public class UnauthorizedException extends BaseException { + + public UnauthorizedException(ErrorCode errorCode) { + super(errorCode); + } + + public UnauthorizedException(ErrorCode errorCode, String message) { + super(errorCode, message); + } +} diff --git a/src/main/java/com/wanted/gold/exception/handler/GlobalExceptionHandler.java b/src/main/java/com/wanted/gold/exception/handler/GlobalExceptionHandler.java index 0529a97..122482f 100644 --- a/src/main/java/com/wanted/gold/exception/handler/GlobalExceptionHandler.java +++ b/src/main/java/com/wanted/gold/exception/handler/GlobalExceptionHandler.java @@ -1,9 +1,6 @@ package com.wanted.gold.exception.handler; -import com.wanted.gold.exception.BadRequestException; -import com.wanted.gold.exception.ConflictException; -import com.wanted.gold.exception.ErrorResponse; -import com.wanted.gold.exception.NotFoundException; +import com.wanted.gold.exception.*; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -18,6 +15,12 @@ public ResponseEntity handleBadRequestException(BadRequestExcepti .body(new ErrorResponse(e.getErrorCode(), e.getErrorCode().getMessage())); } + @ExceptionHandler(UnauthorizedException.class) + public ResponseEntity handleUnauthorizedException(UnauthorizedException e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED) + .body(new ErrorResponse(e.getErrorCode(), e.getErrorCode().getMessage())); + } + @ExceptionHandler(NotFoundException.class) public ResponseEntity handleNotFoundException(NotFoundException e) { return ResponseEntity.status(HttpStatus.NOT_FOUND) diff --git a/src/main/java/com/wanted/gold/order/controller/OrderController.java b/src/main/java/com/wanted/gold/order/controller/OrderController.java index 064b314..f8e51c2 100644 --- a/src/main/java/com/wanted/gold/order/controller/OrderController.java +++ b/src/main/java/com/wanted/gold/order/controller/OrderController.java @@ -1,5 +1,7 @@ package com.wanted.gold.order.controller; +import com.wanted.gold.exception.ErrorCode; +import com.wanted.gold.exception.UnauthorizedException; import com.wanted.gold.order.domain.Order; import com.wanted.gold.order.domain.OrderType; import com.wanted.gold.order.dto.CreateOrderRequestDto; @@ -26,7 +28,7 @@ public class OrderController { @PostMapping("") public ResponseEntity createOrder(@RequestHeader(value = "Authorization") String token, @Valid @RequestBody CreateOrderRequestDto requestDto) { if(token == null || !token.startsWith("Bearer ")) - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("유효하지 않은 접근입니다."); + throw new UnauthorizedException(ErrorCode.UNAUTHORIZED); String accessToken = token.split("Bearer ")[1]; String response = orderService.createOrder(accessToken, requestDto); return ResponseEntity.status(HttpStatus.CREATED).body(response); diff --git a/src/main/java/com/wanted/gold/order/service/OrderService.java b/src/main/java/com/wanted/gold/order/service/OrderService.java index 08c94b8..a159885 100644 --- a/src/main/java/com/wanted/gold/order/service/OrderService.java +++ b/src/main/java/com/wanted/gold/order/service/OrderService.java @@ -1,6 +1,7 @@ package com.wanted.gold.order.service; import com.wanted.gold.client.AuthGrpcClient; +import com.wanted.gold.client.dto.UserResponseDto; import com.wanted.gold.exception.BadRequestException; import com.wanted.gold.exception.ErrorCode; import com.wanted.gold.exception.NotFoundException; @@ -42,8 +43,10 @@ public class OrderService { // 주문 생성 @Transactional public String createOrder(String accessToken, CreateOrderRequestDto requestDto) { - // 액세스토큰으로 회원 식별번호 가져오기 - String userIdStr = authGrpcClient.getUserId(accessToken); + // 액세스토큰으로 회원 정보 가져오기 + UserResponseDto userResponseDto = authGrpcClient.getUserIdAndRole(accessToken); + // 회원 식별번호 가져오기 + String userIdStr = userResponseDto.userId(); // String -> UUID 변환 UUID userId = UUID.fromString(userIdStr); // 입력받은 금 종류로 product 찾기 diff --git a/src/main/proto/auth.proto b/src/main/proto/auth.proto index 20e8440..d6aa68a 100644 --- a/src/main/proto/auth.proto +++ b/src/main/proto/auth.proto @@ -10,6 +10,7 @@ message AuthRequest { message AuthResponse { string userId = 1; + string role = 2; } service AuthService {