Search in sources :

Example 21 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework by spring-projects.

the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpDisconnectAck.

@Test
public void handleMessageToClientWithSimpDisconnectAck() {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.DISCONNECT);
    Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER, connectMessage);
    Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, ackMessage);
    assertEquals(1, this.session.getSentMessages().size());
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertEquals("ERROR\n" + "message:Session closed.\n" + "content-length:0\n" + "\n", actual.getPayload());
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 22 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework by spring-projects.

the class DefaultSimpUserRegistryTests method createMessage.

private Message<byte[]> createMessage(SimpMessageType type, String sessionId, String subscriptionId, String destination) {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(type);
    accessor.setSessionId(sessionId);
    if (destination != null) {
        accessor.setDestination(destination);
    }
    if (subscriptionId != null) {
        accessor.setSubscriptionId(subscriptionId);
    }
    return MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 23 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurerTests method csrfProtectionForConnect.

@Test
public void csrfProtectionForConnect() throws InterruptedException {
    loadConfig(SockJsSecurityConfig.class);
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
    Message<?> message = message(headers, "/authentication");
    MessageChannel messageChannel = clientInboundChannel();
    try {
        messageChannel.send(message);
        fail("Expected Exception");
    } catch (MessageDeliveryException success) {
        assertThat(success.getCause()).isInstanceOf(MissingCsrfTokenException.class);
    }
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) MissingCsrfTokenException(org.springframework.security.web.csrf.MissingCsrfTokenException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 24 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurerTests method messagesConnectUseCsrfTokenHandshakeInterceptorMultipleMappings.

@Test
public void messagesConnectUseCsrfTokenHandshakeInterceptorMultipleMappings() throws Exception {
    loadConfig(SockJsSecurityConfig.class);
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
    Message<?> message = message(headers, "/authentication");
    MockHttpServletRequest request = sockjsHttpRequest("/other");
    HttpRequestHandler handler = handler(request);
    handler.handleRequest(request, new MockHttpServletResponse());
    assertHandshake(request);
}
Also used : HttpRequestHandler(org.springframework.web.HttpRequestHandler) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 25 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurerTests method messagesConnectUseCsrfTokenHandshakeInterceptor.

@Test
public void messagesConnectUseCsrfTokenHandshakeInterceptor() throws Exception {
    loadConfig(SockJsSecurityConfig.class);
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
    Message<?> message = message(headers, "/authentication");
    MockHttpServletRequest request = sockjsHttpRequest("/chat");
    HttpRequestHandler handler = handler(request);
    handler.handleRequest(request, new MockHttpServletResponse());
    assertHandshake(request);
}
Also used : HttpRequestHandler(org.springframework.web.HttpRequestHandler) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Aggregations

SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)54 Test (org.junit.Test)34 Message (org.springframework.messaging.Message)14 MessageHeaders (org.springframework.messaging.MessageHeaders)5 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)5 TextMessage (org.springframework.web.socket.TextMessage)5 MessageChannel (org.springframework.messaging.MessageChannel)3 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)3 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 HttpRequestHandler (org.springframework.web.HttpRequestHandler)3 Principal (java.security.Principal)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 MissingCsrfTokenException (org.springframework.security.web.csrf.MissingCsrfTokenException)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 MessageSendingOperations (org.springframework.messaging.core.MessageSendingOperations)1