diff --git a/backend/src/main/java/io/f1/backend/global/config/CustomHandshakeInterceptor.java b/backend/src/main/java/io/f1/backend/global/config/CustomHandshakeInterceptor.java new file mode 100644 index 00000000..314b37df --- /dev/null +++ b/backend/src/main/java/io/f1/backend/global/config/CustomHandshakeInterceptor.java @@ -0,0 +1,46 @@ +package io.f1.backend.global.config; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.http.HttpStatus; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.server.HandshakeInterceptor; + +import java.util.Map; + +@Slf4j +@Component +public class CustomHandshakeInterceptor implements HandshakeInterceptor { + + @Override + public boolean beforeHandshake( + ServerHttpRequest request, + ServerHttpResponse response, + WebSocketHandler wsHandler, + Map attributes) + throws Exception { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || !authentication.isAuthenticated()) { + response.setStatusCode(HttpStatus.UNAUTHORIZED); // 서버 로그에만 적용되는 StatusCode + return false; + } + + attributes.put("auth", authentication); + return true; + } + + @Override + public void afterHandshake( + ServerHttpRequest request, + ServerHttpResponse response, + WebSocketHandler wsHandler, + Exception exception) { + // TODO : 연결 이후, 사용자 웹소켓 세션 로그 및 IP 등 추적 및 메트릭 수집 로직 추가 + + } +} diff --git a/backend/src/main/java/io/f1/backend/global/config/StompChannelInterceptor.java b/backend/src/main/java/io/f1/backend/global/config/StompChannelInterceptor.java index dea375a8..fd3d02a0 100644 --- a/backend/src/main/java/io/f1/backend/global/config/StompChannelInterceptor.java +++ b/backend/src/main/java/io/f1/backend/global/config/StompChannelInterceptor.java @@ -27,16 +27,21 @@ public Message preSend(Message message, MessageChannel channel) { throw new IllegalArgumentException("Stomp command required"); } + String username = "알수없는 사용자"; + if (accessor.getUser() != null) { + username = accessor.getUser().getName(); + } + if (command.equals(StompCommand.CONNECT)) { - log.info("CONNECT : 세션 연결 - sessionId = {}", sessionId); + log.info("user : {} | CONNECT : 세션 연결 - sessionId = {}", username, sessionId); } else if (command.equals(StompCommand.SUBSCRIBE)) { if (destination != null && sessionId != null) { - log.info("SUBSCRIBE : 구독 시작 destination = {}", destination); + log.info("user : {} | SUBSCRIBE : 구독 시작 destination = {}", username, destination); } } else if (command.equals(StompCommand.SEND)) { - log.info("SEND : 요청 destination = {}", destination); + log.info("user : {} | SEND : 요청 destination = {}", username, destination); } else if (command.equals(StompCommand.DISCONNECT)) { - log.info("DISCONNECT : 연결 해제 sessionId = {}", sessionId); + log.info("user : {} | DISCONNECT : 연결 해제 sessionId = {}", username, sessionId); } return message; diff --git a/backend/src/main/java/io/f1/backend/global/config/WebSocketConfig.java b/backend/src/main/java/io/f1/backend/global/config/WebSocketConfig.java index cb229ed4..7c3ce063 100644 --- a/backend/src/main/java/io/f1/backend/global/config/WebSocketConfig.java +++ b/backend/src/main/java/io/f1/backend/global/config/WebSocketConfig.java @@ -8,7 +8,6 @@ import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; -import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor; @Configuration @RequiredArgsConstructor @@ -16,11 +15,12 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { private final StompChannelInterceptor stompChannelInterceptor; + private final CustomHandshakeInterceptor customHandshakeInterceptor; @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws/game-room") - .addInterceptors(new HttpSessionHandshakeInterceptor()) + .addInterceptors(customHandshakeInterceptor) .setAllowedOriginPatterns("*"); }