Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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 등 추적 및 메트릭 수집 로직 추가

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
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
@EnableWebSocketMessageBroker
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("*");
}

Expand Down