Search in sources :

Example 6 with TransportHandlingSockJsService

use of org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService in project spring-framework by spring-projects.

the class TestMessageCodec method sockJsAttributes.

@Test
@SuppressWarnings("unchecked")
public void sockJsAttributes() {
    loadBeanDefinitions("websocket-config-handlers-sockjs-attributes.xml");
    SimpleUrlHandlerMapping handlerMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(handlerMapping);
    SockJsHttpRequestHandler handler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/test/**");
    assertNotNull(handler);
    unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
    SockJsService sockJsService = handler.getSockJsService();
    assertNotNull(sockJsService);
    assertThat(sockJsService, instanceOf(TransportHandlingSockJsService.class));
    TransportHandlingSockJsService transportService = (TransportHandlingSockJsService) sockJsService;
    assertThat(transportService.getTaskScheduler(), instanceOf(TestTaskScheduler.class));
    assertThat(transportService.getTransportHandlers().values(), containsInAnyOrder(instanceOf(XhrPollingTransportHandler.class), instanceOf(XhrStreamingTransportHandler.class)));
    assertEquals("testSockJsService", transportService.getName());
    assertFalse(transportService.isWebSocketEnabled());
    assertFalse(transportService.isSessionCookieNeeded());
    assertEquals(2048, transportService.getStreamBytesLimit());
    assertEquals(256, transportService.getDisconnectDelay());
    assertEquals(1024, transportService.getHttpMessageCacheSize());
    assertEquals(20, transportService.getHeartbeatTime());
    assertEquals("/js/sockjs.min.js", transportService.getSockJsClientLibraryUrl());
    assertEquals(TestMessageCodec.class, transportService.getMessageCodec().getClass());
    List<HandshakeInterceptor> interceptors = transportService.getHandshakeInterceptors();
    assertThat(interceptors, contains(instanceOf(OriginHandshakeInterceptor.class)));
    assertTrue(transportService.shouldSuppressCors());
    assertTrue(transportService.getAllowedOrigins().contains("http://mydomain1.com"));
    assertTrue(transportService.getAllowedOrigins().contains("http://mydomain2.com"));
}
Also used : SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) SockJsService(org.springframework.web.socket.sockjs.SockJsService) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 7 with TransportHandlingSockJsService

use of org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurer method afterSingletonsInstantiated.

public void afterSingletonsInstantiated() {
    if (sameOriginDisabled()) {
        return;
    }
    String beanName = "stompWebSocketHandlerMapping";
    SimpleUrlHandlerMapping mapping = context.getBean(beanName, SimpleUrlHandlerMapping.class);
    Map<String, Object> mappings = mapping.getHandlerMap();
    for (Object object : mappings.values()) {
        if (object instanceof SockJsHttpRequestHandler) {
            SockJsHttpRequestHandler sockjsHandler = (SockJsHttpRequestHandler) object;
            SockJsService sockJsService = sockjsHandler.getSockJsService();
            if (!(sockJsService instanceof TransportHandlingSockJsService)) {
                throw new IllegalStateException("sockJsService must be instance of TransportHandlingSockJsService got " + sockJsService);
            }
            TransportHandlingSockJsService transportHandlingSockJsService = (TransportHandlingSockJsService) sockJsService;
            List<HandshakeInterceptor> handshakeInterceptors = transportHandlingSockJsService.getHandshakeInterceptors();
            List<HandshakeInterceptor> interceptorsToSet = new ArrayList<HandshakeInterceptor>(handshakeInterceptors.size() + 1);
            interceptorsToSet.add(new CsrfTokenHandshakeInterceptor());
            interceptorsToSet.addAll(handshakeInterceptors);
            transportHandlingSockJsService.setHandshakeInterceptors(interceptorsToSet);
        } else if (object instanceof WebSocketHttpRequestHandler) {
            WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) object;
            List<HandshakeInterceptor> handshakeInterceptors = handler.getHandshakeInterceptors();
            List<HandshakeInterceptor> interceptorsToSet = new ArrayList<HandshakeInterceptor>(handshakeInterceptors.size() + 1);
            interceptorsToSet.add(new CsrfTokenHandshakeInterceptor());
            interceptorsToSet.addAll(handshakeInterceptors);
            handler.setHandshakeInterceptors(interceptorsToSet);
        } else {
            throw new IllegalStateException("Bean " + beanName + " is expected to contain mappings to either a SockJsHttpRequestHandler or a WebSocketHttpRequestHandler but got " + object);
        }
    }
    if (inboundRegistry.containsMapping() && !inboundRegistry.isSimpDestPathMatcherConfigured()) {
        PathMatcher pathMatcher = getDefaultPathMatcher();
        inboundRegistry.simpDestPathMatcher(pathMatcher);
    }
}
Also used : CsrfTokenHandshakeInterceptor(org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) ArrayList(java.util.ArrayList) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) CsrfTokenHandshakeInterceptor(org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) AntPathMatcher(org.springframework.util.AntPathMatcher) PathMatcher(org.springframework.util.PathMatcher) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) SockJsService(org.springframework.web.socket.sockjs.SockJsService) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TransportHandlingSockJsService (org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)7 Test (org.junit.Test)4 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)2 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)2 SockJsService (org.springframework.web.socket.sockjs.SockJsService)2 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Before (org.junit.Before)1 TaskScheduler (org.springframework.scheduling.TaskScheduler)1 CsrfTokenHandshakeInterceptor (org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor)1 AntPathMatcher (org.springframework.util.AntPathMatcher)1 PathMatcher (org.springframework.util.PathMatcher)1 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)1 TransportHandler (org.springframework.web.socket.sockjs.transport.TransportHandler)1 TransportType (org.springframework.web.socket.sockjs.transport.TransportType)1 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)1 StubSockJsServiceConfig (org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig)1 TestSockJsSession (org.springframework.web.socket.sockjs.transport.session.TestSockJsSession)1