Skip to content

Commit 4af9851

Browse files
committed
Add user to DISCONNECT in StompSubProtocolHandler
Issue: SPR-12215
1 parent b1e9577 commit 4af9851

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ private Message<?> createDisconnectMessage(WebSocketSession session) {
469469
}
470470
headerAccessor.setSessionId(session.getId());
471471
headerAccessor.setSessionAttributes(session.getAttributes());
472+
headerAccessor.setUser(session.getPrincipal());
472473
return MessageBuilder.createMessage(EMPTY_PAYLOAD, headerAccessor.getMessageHeaders());
473474
}
474475

spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,7 @@ public void eventPublication() {
207207
@Test
208208
public void eventPublicationWithExceptions() {
209209

210-
ApplicationEventPublisher publisher = new ApplicationEventPublisher() {
211-
212-
@Override
213-
public void publishEvent(ApplicationEvent event) {
214-
throw new IllegalStateException();
215-
}
216-
};
210+
ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
217211

218212
UserSessionRegistry registry = new DefaultUserSessionRegistry();
219213
this.protocolHandler.setUserSessionRegistry(registry);
@@ -244,7 +238,10 @@ public void publishEvent(ApplicationEvent event) {
244238
verify(this.channel).send(this.messageCaptor.capture());
245239
actual = this.messageCaptor.getValue();
246240
assertNotNull(actual);
247-
assertEquals(StompCommand.DISCONNECT, StompHeaderAccessor.wrap(actual).getCommand());
241+
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(actual);
242+
assertEquals(StompCommand.DISCONNECT, accessor.getCommand());
243+
assertEquals("s1", accessor.getSessionId());
244+
assertEquals("joe", accessor.getUser().getName());
248245
}
249246

250247
@Test

0 commit comments

Comments
 (0)