From e09ad650553b3db96ad7da4ec8c3e56c3c300278 Mon Sep 17 00:00:00 2001 From: khee2 <21keunghee@gmail.com> Date: Wed, 12 Jun 2024 13:50:14 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20websocket=20=EC=97=B0=EA=B2=B0=20log?= =?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 --- .../message/controller/MessageController.java | 17 +++++++------ .../global/config/AuthChannelInterceptor.java | 25 ++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/SafeNet/Backend/domain/message/controller/MessageController.java b/src/main/java/com/SafeNet/Backend/domain/message/controller/MessageController.java index 8221b23..4b76a2e 100644 --- a/src/main/java/com/SafeNet/Backend/domain/message/controller/MessageController.java +++ b/src/main/java/com/SafeNet/Backend/domain/message/controller/MessageController.java @@ -42,14 +42,17 @@ public class MessageController { @MessageMapping("/chat/message") public void message(@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken, MessageDto messageDto) { - - // Access Token 검증 - if (accessToken == null || !jwtTokenProvider.validateToken(accessToken)) { // 메시지 전송 전 유효한 토큰인지 검증 - throw new AccessDeniedException("Invalid or expired token"); + try { + // Access Token 검증 + if (accessToken == null || !jwtTokenProvider.validateToken(accessToken)) { // 메시지 전송 전 유효한 토큰인지 검증 + throw new AccessDeniedException("Invalid or expired token"); + } + // 메시지 전송 로직 호출 + messageRoomService.handleMessage(messageDto.getRoomId(), messageDto.getSender(), messageDto); + } catch (Exception e) { + log.error("Failed to send message: {}", e.getMessage()); + throw e; } - - // 메시지 전송 로직 호출 - messageRoomService.handleMessage(messageDto.getRoomId(), messageDto.getSender(), messageDto); } // 대화 내역 조회 diff --git a/src/main/java/com/SafeNet/Backend/global/config/AuthChannelInterceptor.java b/src/main/java/com/SafeNet/Backend/global/config/AuthChannelInterceptor.java index 502b3c4..73aa6f7 100644 --- a/src/main/java/com/SafeNet/Backend/global/config/AuthChannelInterceptor.java +++ b/src/main/java/com/SafeNet/Backend/global/config/AuthChannelInterceptor.java @@ -1,6 +1,7 @@ package com.SafeNet.Backend.global.config; import com.SafeNet.Backend.global.auth.JwtTokenProvider; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; @@ -15,6 +16,7 @@ // WebSocket 메시지의 헤더에서 ACCESS_TOKEN을 추출하고 검증 // 유효한 토큰이 있는 경우 사용자 인증 정보를 설정하고, 유효하지 않은 경우 연결을 차단 @Component +@Slf4j public class AuthChannelInterceptor implements ChannelInterceptor { private final JwtTokenProvider jwtTokenProvider; @@ -27,18 +29,23 @@ public AuthChannelInterceptor(JwtTokenProvider jwtTokenProvider) { @Override public Message preSend(Message message, MessageChannel channel) { StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); - if (StompCommand.CONNECT.equals(accessor.getCommand())) { // CONNECT 프레임은 서버에 대한 인증 및 기타 설정과 관련된 정보를 전송하기 위해 사용 + if (StompCommand.CONNECT.equals(accessor.getCommand())) { String token = accessor.getFirstNativeHeader("ACCESS_TOKEN"); - if (token != null && token.startsWith("Bearer ")) { - token = token.substring(7); - if (jwtTokenProvider.validateToken(token)) { - String username = jwtTokenProvider.getAuthentication(token).getName(); - accessor.setUser(new UsernamePasswordAuthenticationToken(username, null, Collections.emptyList())); + try { + if (token != null && token.startsWith("Bearer ")) { + token = token.substring(7); + if (jwtTokenProvider.validateToken(token)) { + String username = jwtTokenProvider.getAuthentication(token).getName(); + accessor.setUser(new UsernamePasswordAuthenticationToken(username, null, Collections.emptyList())); + } else { + throw new IllegalArgumentException("Invalid or expired token"); + } } else { - throw new IllegalArgumentException("Invalid or expired token"); + throw new IllegalArgumentException("Missing or invalid ACCESS_TOKEN header"); } - } else { - throw new IllegalArgumentException("Missing or invalid ACCESS_TOKEN header"); + } catch (Exception e) { + log.error("Token validation failed: {}", e.getMessage()); + throw e; } } return message;