From e61d4ecc8b6ddee5359baa2f7184626f0465066a Mon Sep 17 00:00:00 2001 From: Eungi Jeong Date: Thu, 25 May 2023 19:51:11 +0900 Subject: [PATCH] =?UTF-8?q?[Fix]=20DTO=20Response=20Model=20=EC=9E=AC?= =?UTF-8?q?=EC=A0=95=EC=9D=98=20(=EC=9D=BC=EB=B6=80)=20=3D=20#33?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../favor/favor/auth/JwtTokenProvider.java | 5 +- .../configuration/SecurityConfiguration.java | 1 + .../CustomAuthenticationEntryPoint.java | 2 + .../com/favor/favor/user/UserController.java | 47 ++++---- .../com/favor/favor/user/UserResponseDto.java | 109 ++++++++++++++++-- .../com/favor/favor/user/UserService.java | 2 +- 6 files changed, 131 insertions(+), 35 deletions(-) diff --git a/favor/src/main/java/com/favor/favor/auth/JwtTokenProvider.java b/favor/src/main/java/com/favor/favor/auth/JwtTokenProvider.java index ea2fa61..94c9400 100644 --- a/favor/src/main/java/com/favor/favor/auth/JwtTokenProvider.java +++ b/favor/src/main/java/com/favor/favor/auth/JwtTokenProvider.java @@ -38,9 +38,10 @@ protected void init() { } // JWT 토큰 생성 - public String createToken(String email, Role role) { + public String createToken(String email, Role role, Long userNo) { Claims claims = Jwts.claims().setSubject(email); // JWT payload 에 저장되는 정보단위 claims.put("roles", role); // 정보는 key / value 쌍으로 저장된다. + claims.put("userNo", userNo); Date now = new Date(); String token = Jwts.builder() @@ -66,6 +67,8 @@ public String getUserEmail(String token) { return info; } + + // Request의 Header에서 token 값을 가져옵니다. "X-AUTH-TOKEN" : "TOKEN값' public String resolveToken(HttpServletRequest request) { return request.getHeader("X-AUTH-TOKEN"); diff --git a/favor/src/main/java/com/favor/favor/configuration/SecurityConfiguration.java b/favor/src/main/java/com/favor/favor/configuration/SecurityConfiguration.java index 650dcf9..402f088 100644 --- a/favor/src/main/java/com/favor/favor/configuration/SecurityConfiguration.java +++ b/favor/src/main/java/com/favor/favor/configuration/SecurityConfiguration.java @@ -62,6 +62,7 @@ protected void configure(HttpSecurity http) throws Exception { .and() .exceptionHandling().authenticationEntryPoint(new CustomAuthenticationEntryPoint()) + .and() .addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class); diff --git a/favor/src/main/java/com/favor/favor/exception/CustomAuthenticationEntryPoint.java b/favor/src/main/java/com/favor/favor/exception/CustomAuthenticationEntryPoint.java index 24f73a3..8b033de 100644 --- a/favor/src/main/java/com/favor/favor/exception/CustomAuthenticationEntryPoint.java +++ b/favor/src/main/java/com/favor/favor/exception/CustomAuthenticationEntryPoint.java @@ -26,6 +26,8 @@ public void commence(HttpServletRequest request, responseJson.put("ResponseCode", "AUTHENTICATION_FAILED"); responseJson.put("ResponseMessage", "인증에 실패하였습니다."); + + response.getWriter().print(responseJson); } } \ No newline at end of file diff --git a/favor/src/main/java/com/favor/favor/user/UserController.java b/favor/src/main/java/com/favor/favor/user/UserController.java index a9aac7c..7f0770e 100644 --- a/favor/src/main/java/com/favor/favor/user/UserController.java +++ b/favor/src/main/java/com/favor/favor/user/UserController.java @@ -264,7 +264,7 @@ public ResponseEntity> updatePassword( @ApiResponses(value={ @ApiResponse(code = 200, message = "REMINDERS_FOUND", - response = ReminderResponseDto.class), + response = UserResponseDto.class), @ApiResponse(code = 401, message = "UNAUTHORIZED_USER"), @ApiResponse(code = 404, @@ -380,58 +380,59 @@ public ResponseEntity> readFriendList(@PathVariable L .build()); } - @ApiOperation("회원의 기념일 전체 조회") + + @ApiOperation("전체 회원 조회") @ApiResponses(value={ @ApiResponse(code = 200, - message = "ANNIVERSARY_FOUND", - response = AnniversaryResponseDto.class), + message = "USERS_FOUND", + response = UserResponseDto.class), @ApiResponse(code = 401, message = "UNAUTHORIZED_USER"), - @ApiResponse(code = 404, - message = "USER_NOT_FOUND"), @ApiResponse(code = 500, message = "SERVER_ERROR") }) @ResponseStatus(HttpStatus.OK) @Transactional - @GetMapping("/anniversary-list/{userNo}") - public ResponseEntity> readAnniversaryList(@PathVariable Long userNo){ - - userService.isExistingUserNo(userNo); + @GetMapping + public ResponseEntity> readAll(){ - List friends = userService.readAnniversaryList(userNo); + List dto = userService.readAll(); return ResponseEntity.status(200) .body(DefaultResponseDto.builder() - .responseCode("ANNIVERSARY_FOUND") - .responseMessage("회원의 기념일 전체 조회 완료") - .data(friends) + .responseCode("USERS_FOUND") + .responseMessage("전체 회원 조회 완료") + .data(dto) .build()); } - @ApiOperation("전체 회원 조회") + @ApiOperation("회원의 기념일 전체 조회") @ApiResponses(value={ @ApiResponse(code = 200, - message = "USERS_FOUND", - response = UserResponseDto.class), + message = "ANNIVERSARY_FOUND", + response = AnniversaryResponseDto.class), @ApiResponse(code = 401, message = "UNAUTHORIZED_USER"), + @ApiResponse(code = 404, + message = "USER_NOT_FOUND"), @ApiResponse(code = 500, message = "SERVER_ERROR") }) @ResponseStatus(HttpStatus.OK) @Transactional - @GetMapping - public ResponseEntity> readAll(){ + @GetMapping("/anniversary-list/{userNo}") + public ResponseEntity> readAnniversaryList(@PathVariable Long userNo){ - List dto = userService.readAll(); + userService.isExistingUserNo(userNo); + + List friends = userService.readAnniversaryList(userNo); return ResponseEntity.status(200) .body(DefaultResponseDto.builder() - .responseCode("USERS_FOUND") - .responseMessage("전체 회원 조회 완료") - .data(dto) + .responseCode("ANNIVERSARY_FOUND") + .responseMessage("회원의 기념일 전체 조회 완료") + .data(friends) .build()); } diff --git a/favor/src/main/java/com/favor/favor/user/UserResponseDto.java b/favor/src/main/java/com/favor/favor/user/UserResponseDto.java index a2c6285..1722126 100644 --- a/favor/src/main/java/com/favor/favor/user/UserResponseDto.java +++ b/favor/src/main/java/com/favor/favor/user/UserResponseDto.java @@ -17,25 +17,114 @@ @AllArgsConstructor @Builder public class UserResponseDto { - @ApiModelProperty(value = "1") + @ApiModelProperty(value = "1", example = "1") private final Long userNo; - @ApiModelProperty(value = "favor@gmail.com") + + @ApiModelProperty(value = "favor@gmail.com", example = "favor@gmail.com") private String email; - @ApiModelProperty(value = "페이버") + + @ApiModelProperty(value = "페이버", example = "페이버") private String name; - @ApiModelProperty(value = "favor") + + @ApiModelProperty(value = "favor", example = "favor") private String userid; - @ApiModelProperty(value = "USER") + + @ApiModelProperty(value = "USER", example = "USER") private Role role; - @ApiModelProperty(value = "") + + @ApiModelProperty(value = "", example = "[\n" + + " {\n" + + " \"reminderNo\": 1,\n" + + " \"reminderTitle\": \"제목\",\n" + + " \"reminderDate\": \"1996-02-29\",\n" + + " \"reminderMemo\": \"메모\",\n" + + " \"isAlarmSet\": false,\n" + + " \"alarmTime\": \"1996-02-29T00:00:00\",\n" + + " \"userNo\": 1,\n" + + " \"friendNo\": 1\n" + + " }\n" + + " ]") private List reminderList; - @ApiModelProperty(value = "") + + @ApiModelProperty(value = "", example = "[\n" + + " {\n" + + " \"giftNo\": 1,\n" + + " \"giftName\": \"선물이름\",\n" + + " \"giftDate\": \"1996-02-29\",\n" + + " \"giftMemo\": \"선물메모\",\n" + + " \"category\": \"생일\",\n" + + " \"emotion\": \"기뻐요\",\n" + + " \"isPinned\": false,\n" + + " \"isGiven\": false,\n" + + " \"userNo\": 1,\n" + + " \"friendList\": [\n" + + " {\n" + + " \"isUser\": true,\n" + + " \"friendNo\": 1,\n" + + " \"friendName\": \"페이버2\",\n" + + " \"friendMemo\": \"메모\",\n" + + " \"reminderList\": [],\n" + + " \"giftList\": [],\n" + + " \"favorList\": [],\n" + + " \"friendUserNo\": 2,\n" + + " \"anniversaryList\": [],\n" + + " \"userNo\": 1\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]") private List giftList; - @ApiModelProperty(value = "") + + @ApiModelProperty(value = "", example = "[\n" + + " {\n" + + " \"isUser\": true,\n" + + " \"friendNo\": 1,\n" + + " \"friendName\": \"페이버2\",\n" + + " \"friendMemo\": \"메모\",\n" + + " \"reminderList\": [],\n" + + " \"giftList\": [],\n" + + " \"favorList\": [],\n" + + " \"friendUserNo\": 2,\n" + + " \"anniversaryList\": [],\n" + + " \"userNo\": 1\n" + + " },\n" + + " {\n" + + " \"isUser\": false,\n" + + " \"friendNo\": 2,\n" + + " \"friendName\": \"이름11\",\n" + + " \"friendMemo\": \"메모\",\n" + + " \"reminderList\": [],\n" + + " \"giftList\": [],\n" + + " \"favorList\": [],\n" + + " \"friendUserNo\": null,\n" + + " \"anniversaryList\": [],\n" + + " \"userNo\": 1\n" + + " }\n" + + " ]") private List friendList; - @ApiModelProperty(value = "") + + @ApiModelProperty(value = "", example = "[\n" + + " {\n" + + " \"anniversaryNo\": 1,\n" + + " \"anniversaryTitle\": \"제목\",\n" + + " \"anniversaryDate\": \"1996-02-29\",\n" + + " \"isPinned\": false,\n" + + " \"userNo\": 1\n" + + " },\n" + + " {\n" + + " \"anniversaryNo\": 2,\n" + + " \"anniversaryTitle\": \"제목\",\n" + + " \"anniversaryDate\": \"2024-02-29\",\n" + + " \"isPinned\": false,\n" + + " \"userNo\": 1\n" + + " }\n" + + " ]") private List anniversaryList; - @ApiModelProperty(value = "") + + @ApiModelProperty(value = "", example = "[\n" + + " \"심플한\",\n" + + " \"귀여운\"\n" + + " ]") private List favorList; @Builder diff --git a/favor/src/main/java/com/favor/favor/user/UserService.java b/favor/src/main/java/com/favor/favor/user/UserService.java index 1497fcf..e7daa3a 100644 --- a/favor/src/main/java/com/favor/favor/user/UserService.java +++ b/favor/src/main/java/com/favor/favor/user/UserService.java @@ -95,7 +95,7 @@ public SignInResponseDto signIn(SignDto dto){ isRightPassword(password, user); - String token = jwtTokenProvider.createToken(user.getEmail(), user.getRole()); + String token = jwtTokenProvider.createToken(user.getEmail(), user.getRole(), user.getUserNo()); return new SignInResponseDto(token); }