Give STOMP sessions a principal so /user/queue/errors is delivered

ChatWebSocketController reports send errors with convertAndSendToUser, but
CONNECT never set a user on the session, so those messages had no recipient.
The interceptor now sets a principal named after the user id on the message's
own accessor and allows subscriptions to /user/queue/** (resolved per session
by Spring, so it cannot reach another user's queue).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-20 01:09:57 +03:00
parent 6b647e0bbf
commit 7b81c18614
2 changed files with 115 additions and 1 deletions

View File

@@ -0,0 +1,91 @@
package com.krylov.refound.util;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.krylov.refound.security.JwtService;
import com.krylov.refound.service.ChatService;
import com.krylov.refound.service.UserService;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.test.util.ReflectionTestUtils;
class WsStompInterceptorTest {
private JwtService jwtService;
private WsStompInterceptor interceptor;
@BeforeEach
void setUp() {
jwtService = new JwtService();
ReflectionTestUtils.setField(jwtService, "secret", "test-secret-test-secret-test-secret-0123456789");
ReflectionTestUtils.setField(jwtService, "accessExpiration", 60_000L);
ReflectionTestUtils.setField(jwtService, "refreshExpiration", 600_000L);
UserService userService = mock(UserService.class);
when(userService.getUserIdByLogin("alice")).thenReturn(5L);
interceptor = new WsStompInterceptor(jwtService, userService, mock(ChatService.class));
}
@Test
void connectSetsPrincipalSoUserQueuesWork() {
Map<String, Object> session = new HashMap<>();
Message<?> connect = connect("Bearer " + jwtService.generateAccessToken("alice", "USER"), session);
interceptor.preSend(connect, mock(MessageChannel.class));
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(connect, StompHeaderAccessor.class);
assertThat(accessor.getUser()).isNotNull();
assertThat(accessor.getUser().getName()).isEqualTo("5");
assertThat(session).containsEntry("userId", 5L);
}
@Test
void refreshTokenIsRejectedOnConnect() {
Message<?> connect = connect("Bearer " + jwtService.generateRefreshToken("alice"), new HashMap<>());
assertThatThrownBy(() -> interceptor.preSend(connect, mock(MessageChannel.class)))
.isInstanceOf(IllegalArgumentException.class);
}
@Test
void ownUserQueueIsAllowedAndForeignQueueIsNot() {
Map<String, Object> session = new HashMap<>();
session.put("userId", 5L);
session.put("role", "USER");
interceptor.preSend(subscribe("/user/queue/errors", session), mock(MessageChannel.class));
interceptor.preSend(subscribe("/user/5/queue/errors", session), mock(MessageChannel.class));
assertThatThrownBy(() -> interceptor.preSend(subscribe("/user/6/queue/errors", session), mock(MessageChannel.class)))
.isInstanceOf(MessagingException.class);
assertThatThrownBy(() -> interceptor.preSend(subscribe("/topic/ads/1/status", session), mock(MessageChannel.class)))
.isInstanceOf(MessagingException.class);
}
private static Message<?> connect(String authorization, Map<String, Object> session) {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
accessor.setNativeHeader("Authorization", authorization);
accessor.setSessionAttributes(session);
accessor.setLeaveMutable(true);
return MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
}
private static Message<?> subscribe(String destination, Map<String, Object> session) {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
accessor.setDestination(destination);
accessor.setSessionAttributes(session);
accessor.setLeaveMutable(true);
return MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
}
}