From 22e183d112df30998f8f386629e03cfa05d26d31 Mon Sep 17 00:00:00 2001 From: hyeesw Date: Tue, 1 Oct 2024 21:21:16 +0900 Subject: [PATCH] =?UTF-8?q?[debug]=20#71=20=EB=B0=9C=EC=8B=A0=EC=9E=90=20?= =?UTF-8?q?=EB=8B=89=EB=84=A4=EC=9E=84=20=EB=B3=80=EA=B2=BD=20=EC=95=88?= =?UTF-8?q?=EB=90=98=EB=8A=94=20=EB=AC=B8=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/CustomUserDetailsService.java | 5 ++- .../message/controller/MessageController.java | 36 ++++++++++++------- .../message/service/MessageService.java | 7 +++- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/book/backend/domain/auth/service/CustomUserDetailsService.java b/src/main/java/com/book/backend/domain/auth/service/CustomUserDetailsService.java index fb25cea..6f9a197 100644 --- a/src/main/java/com/book/backend/domain/auth/service/CustomUserDetailsService.java +++ b/src/main/java/com/book/backend/domain/auth/service/CustomUserDetailsService.java @@ -26,10 +26,13 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx .orElseGet(() -> userRepository.findByAppleId(username) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))); - return org.springframework.security.core.userdetails.User.builder() + UserDetails userDetails = org.springframework.security.core.userdetails.User.builder() .username(username) .password("unused") .authorities("ROLE_USER") .build(); + + log.trace("CustomUserDetailsService > loadUserByUsername() > userDetails: {}", userDetails); + return userDetails; } } diff --git a/src/main/java/com/book/backend/domain/message/controller/MessageController.java b/src/main/java/com/book/backend/domain/message/controller/MessageController.java index aca0131..7515b86 100644 --- a/src/main/java/com/book/backend/domain/message/controller/MessageController.java +++ b/src/main/java/com/book/backend/domain/message/controller/MessageController.java @@ -41,19 +41,6 @@ public class MessageController { private final RequestValidate requestValidate; private final GoalService goalService; - // HTTP 단방향 채팅 저장 - @Operation(summary="메세지 저장 (HTTP)", description="임시 채팅 저장 API 입니다. opentalkId, type, text 를 입력으로 받아 저장 결과를 반환합니다.", - parameters = {@Parameter(name = "opentalkId", description = "오픈톡 DB ID"), @Parameter(name = "type", description = "메세지 타입 (text, img)"), @Parameter(name = "content", description = "메세지 내용(goal 인 경우 null)")}, - responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = MessageResponseDto.class)), - description = MessageResponseDto.description)}) - @PostMapping("/api/message/save") - public ResponseEntity httpChat(@RequestParam Long opentalkId, String type, String content){ - requestValidate.isValidType(type); - - MessageResponseDto response = messageService.saveHttpMessage(opentalkId, type, content); - return responseTemplate.success(response, HttpStatus.OK); - } - // 목표 공유하기 @Operation(summary="목표 공유하기", description="opentalkId, isbn 을 입력으로 받아 해당 목표가 있는지 확인 후 채팅방에 전송합니다.", parameters = {@Parameter(name = "opentalkId", description = "오픈톡 DB ID"), @Parameter(name = "isbn", description = "책 ISBN")}, @@ -86,6 +73,8 @@ public ResponseEntity getChat(@RequestParam Long opentalkId, int pageNo, int // 채팅 저장하기 (apic 으로 테스트) @MessageMapping("/message") public void chat(MessageRequestDto messageRequestDto) { + log.trace("---------------------------------------------"); + log.trace("stomp URL : /pub/message"); // Endpoint URL 로깅 RequestLogger.body(messageRequestDto); MessageResponseDto response = messageService.saveMessage(messageRequestDto); sendingOperations.convertAndSend("/sub/message/" + messageRequestDto.getOpentalkId(), response); // 수신자들에게 전송 @@ -102,4 +91,25 @@ public void chat(MessageRequestDto messageRequestDto) { public void chatForSwagger(MessageRequestDto messageRequestDto) { return; } + + // HTTP 단방향 채팅 저장 + @Operation(summary="메세지 저장 (HTTP)", description="임시 채팅 저장 API 입니다. opentalkId, type, text 를 입력으로 받아 저장 결과를 반환합니다.", + parameters = {@Parameter(name = "opentalkId", description = "오픈톡 DB ID"), @Parameter(name = "type", description = "메세지 타입 (text, img)"), @Parameter(name = "content", description = "메세지 내용(goal 인 경우 null)")}, + responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = MessageResponseDto.class)), + description = MessageResponseDto.description)}) + + @PostMapping("/api/message/save") + public ResponseEntity httpChat(@RequestParam Long opentalkId, String type, String content, String jwtToken){ + requestValidate.isValidType(type); + MessageRequestDto messageRequestDto = MessageRequestDto.builder() + .opentalkId(opentalkId) + .type(type) + .content(content) + .jwtToken(jwtToken) + .build(); + MessageResponseDto response = messageService.saveMessage(messageRequestDto); + +// MessageResponseDto response = messageService.saveHttpMessage(opentalkId, type, content); + return responseTemplate.success(response, HttpStatus.OK); + } } diff --git a/src/main/java/com/book/backend/domain/message/service/MessageService.java b/src/main/java/com/book/backend/domain/message/service/MessageService.java index 1d2b249..3f07afc 100644 --- a/src/main/java/com/book/backend/domain/message/service/MessageService.java +++ b/src/main/java/com/book/backend/domain/message/service/MessageService.java @@ -94,6 +94,7 @@ public MessageResponseDto saveMessage(MessageRequestDto messageRequestDto){ validateToken(token); // message DB에 저장 + log.trace("saveMessage() 유저 이름 : {}", userService.loadLoggedinUser()); Message message = messageMapper.convertToMessage(messageRequestDto); try{ messageRepository.save(message); @@ -105,6 +106,7 @@ public MessageResponseDto saveMessage(MessageRequestDto messageRequestDto){ public void validateToken(String token) { + log.trace("MessageService > validateToken()"); try{ String username = jwtUtil.getUsernameFromToken(token); // username 가져옴 // 현재 SecurityContextHolder에 인증객체가 있는지 확인 @@ -113,10 +115,13 @@ public void validateToken(String token) { userDetails = userDetailsService.loadUserByUsername(username); // 토큰 유효성 검증 - if (jwtUtil.isValidToken(token, userDetails)) { + if (!jwtUtil.isValidToken(token, userDetails)) { + throw new CustomException(ErrorCode.WRONG_JWT_TOKEN); + } else { UsernamePasswordAuthenticationToken authenticated = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(authenticated); + log.trace("validateToken() 유저 이름 : {}", userService.loadLoggedinUser()); } } } catch (ExpiredJwtException e) {