From ffefed3678e753e8b58e0f493a54e31cf4436128 Mon Sep 17 00:00:00 2001 From: kimnamgyu Date: Mon, 6 May 2024 14:44:13 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=ED=97=A4=EB=8D=94=EC=97=90=20?= =?UTF-8?q?=EC=9D=B8=EC=A6=9D=20=ED=86=A0=ED=81=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: hyoguoo --- .../chat/controller/SignallingController.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/ei/algobaroapi/domain/chat/controller/SignallingController.java b/src/main/java/ei/algobaroapi/domain/chat/controller/SignallingController.java index a65c92d..8b5dde6 100644 --- a/src/main/java/ei/algobaroapi/domain/chat/controller/SignallingController.java +++ b/src/main/java/ei/algobaroapi/domain/chat/controller/SignallingController.java @@ -3,6 +3,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.messaging.handler.annotation.DestinationVariable; +import org.springframework.messaging.handler.annotation.Header; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.Payload; import org.springframework.messaging.handler.annotation.SendTo; @@ -17,6 +18,7 @@ public class SignallingController { @SendTo("/topic/peer/offer/{camKey}/{roomShortUuid}") public String peerHandleOffer( @Payload String offer, + @Header("Authorization") String authorization, @DestinationVariable(value = "roomShortUuid") String roomShortUuid, @DestinationVariable(value = "camKey") String camKey ) { @@ -30,6 +32,7 @@ public String peerHandleOffer( @SendTo("/topic/peer/iceCandidate/{camKey}/{roomShortUuid}") public String peerHandleIceCandidate( @Payload String candidate, + @Header("Authorization") String authorization, @DestinationVariable(value = "roomShortUuid") String roomShortUuid, @DestinationVariable(value = "camKey") String camKey ) { @@ -42,6 +45,7 @@ public String peerHandleIceCandidate( @SendTo("/topic/peer/answer/{camKey}/{roomShortUuid}") public String peerHandleAnswer( @Payload String answer, + @Header("Authorization") String authorization, @DestinationVariable(value = "roomShortUuid") String roomShortUuid, @DestinationVariable(value = "camKey") String camKey ) { @@ -52,7 +56,10 @@ public String peerHandleAnswer( //camKey 를 받기위해 신호를 보내는 webSocket @MessageMapping("/call/key") @SendTo("/topic/call/key") - public String callKey(@Payload String message) { + public String callKey( + @Payload String message, + @Header("Authorization") String authorization + ) { log.info("[Key] : {}", message); return message; } @@ -60,7 +67,10 @@ public String callKey(@Payload String message) { //자신의 camKey 를 모든 연결된 세션에 보내는 webSocket @MessageMapping("/send/key") @SendTo("/topic/send/key") - public String sendKey(@Payload String message) { + public String sendKey( + @Payload String message, + @Header("Authorization") String authorization + ) { return message; } } From 7a8c98d656e46b8d943809494984429fd166de6c Mon Sep 17 00:00:00 2001 From: kimnamgyu Date: Mon, 6 May 2024 14:44:26 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EC=97=90=EB=9F=AC=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=20=EC=9C=84=ED=95=9C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: hyoguoo --- .../socket/WebSocketChannelInterceptor.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/ei/algobaroapi/global/socket/WebSocketChannelInterceptor.java b/src/main/java/ei/algobaroapi/global/socket/WebSocketChannelInterceptor.java index c4f048e..7237cae 100644 --- a/src/main/java/ei/algobaroapi/global/socket/WebSocketChannelInterceptor.java +++ b/src/main/java/ei/algobaroapi/global/socket/WebSocketChannelInterceptor.java @@ -2,6 +2,7 @@ import ei.algobaroapi.global.jwt.JwtProvider; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.simp.stomp.StompCommand; @@ -9,6 +10,7 @@ import org.springframework.messaging.support.ChannelInterceptor; import org.springframework.stereotype.Component; +@Slf4j @Component @RequiredArgsConstructor public class WebSocketChannelInterceptor implements ChannelInterceptor { @@ -24,12 +26,17 @@ public Message preSend(Message message, MessageChannel channel) { if (accessor.getCommand() == StompCommand.CONNECT || accessor.getCommand() == StompCommand.SUBSCRIBE || accessor.getCommand() == StompCommand.SEND) { - String token = extractToken(accessor); - if (!jwtProvider.validateToken(token)) { - throw new IllegalArgumentException("Invalid token"); + try { + String token = extractToken(accessor); + if (token == null || !jwtProvider.validateToken(token)) { + log.warn("토큰 null or 유효하지 않은 토큰"); + throw new IllegalArgumentException("Invalid token"); + } + } catch (Exception e) { + log.error("Authentication error: " + e.getMessage(), e); + return null; } } - return message; }