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
Expand Up @@ -5,6 +5,7 @@
import com.example.solidconnection.chat.config.StompProperties.OutboundProperties;
import com.example.solidconnection.security.config.CorsProperties;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
Expand All @@ -24,6 +25,7 @@ public class StompWebSocketConfig implements WebSocketMessageBrokerConfigurer {
private final CorsProperties corsProperties;
private final WebSocketHandshakeInterceptor webSocketHandshakeInterceptor;
private final CustomHandshakeHandler customHandshakeHandler;
private final Optional<WebSocketLoggingInterceptor> webSocketLoggingInterceptor;

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
Expand All @@ -39,7 +41,12 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
InboundProperties inboundProperties = stompProperties.threadPool().inbound();
registration.interceptors(stompHandler).taskExecutor().corePoolSize(inboundProperties.corePoolSize()).maxPoolSize(inboundProperties.maxPoolSize()).queueCapacity(inboundProperties.queueCapacity());
webSocketLoggingInterceptor.ifPresent(registration::interceptors);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 인터셉터 등록 순서 문제는 발생하지 않나요?
StompHandler 보다 이전에 로깅 인터셉터가 등록 되었을 때, User Principal 을 잘 가져올 수 있는지 궁금합니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

둘 다 Principal을 읽기만 하기에 문제없을 것 같습니다 !

registration.interceptors(stompHandler)
.taskExecutor()
.corePoolSize(inboundProperties.corePoolSize())
.maxPoolSize(inboundProperties.maxPoolSize())
.queueCapacity(inboundProperties.queueCapacity());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.solidconnection.chat.config;

import com.example.solidconnection.security.authentication.TokenAuthentication;
import com.example.solidconnection.security.userdetails.SiteUserDetails;
import java.security.Principal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Profile("dev")
public class WebSocketLoggingInterceptor implements ChannelInterceptor {

@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
StompCommand command = accessor.getCommand();

if (command != null) {
Long userId = extractUserId(accessor);
String destination = accessor.getDestination();
logStompMessage(command, destination, userId);
}

return message;
}

private void logStompMessage(StompCommand command, String destination, Long userId) {
switch (command) {
case CONNECT -> log.info("[WEBSOCKET] CONNECT userId = {}", userId);
case SUBSCRIBE -> log.info("[WEBSOCKET] SUBSCRIBE {} userId = {}", destination, userId);
case SEND -> log.info("[WEBSOCKET] SEND {} userId = {}", destination, userId);
case DISCONNECT -> log.info("[WEBSOCKET] DISCONNECT userId = {}", userId);
default -> {
}
}
}

private Long extractUserId(StompHeaderAccessor accessor) {
Principal user = accessor.getUser();
if (user instanceof TokenAuthentication tokenAuth) {
Object principal = tokenAuth.getPrincipal();
if (principal instanceof SiteUserDetails details) {
return details.getSiteUser().getId();
}
}
return null;
}
}
Loading