Search in sources :

Example 1 with OriginHandshakeInterceptor

use of org.springframework.web.socket.server.support.OriginHandshakeInterceptor in project spring-framework by spring-projects.

the class MessageBrokerBeanDefinitionParser method registerRequestHandler.

private RuntimeBeanReference registerRequestHandler(Element element, RuntimeBeanReference subProtoHandler, ParserContext context, Object source) {
    RootBeanDefinition beanDef;
    RuntimeBeanReference sockJsService = WebSocketNamespaceUtils.registerSockJsService(element, SCHEDULER_BEAN_NAME, context, source);
    if (sockJsService != null) {
        ConstructorArgumentValues cavs = new ConstructorArgumentValues();
        cavs.addIndexedArgumentValue(0, sockJsService);
        cavs.addIndexedArgumentValue(1, subProtoHandler);
        beanDef = new RootBeanDefinition(SockJsHttpRequestHandler.class, cavs, null);
        // Register alias for backwards compatibility with 4.1
        context.getRegistry().registerAlias(SCHEDULER_BEAN_NAME, SOCKJS_SCHEDULER_BEAN_NAME);
    } else {
        RuntimeBeanReference handshakeHandler = WebSocketNamespaceUtils.registerHandshakeHandler(element, context, source);
        Element interceptorsElement = DomUtils.getChildElementByTagName(element, "handshake-interceptors");
        ManagedList<? super Object> interceptors = WebSocketNamespaceUtils.parseBeanSubElements(interceptorsElement, context);
        String allowedOriginsAttribute = element.getAttribute("allowed-origins");
        List<String> allowedOrigins = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOriginsAttribute, ","));
        interceptors.add(new OriginHandshakeInterceptor(allowedOrigins));
        ConstructorArgumentValues cavs = new ConstructorArgumentValues();
        cavs.addIndexedArgumentValue(0, subProtoHandler);
        if (handshakeHandler != null) {
            cavs.addIndexedArgumentValue(1, handshakeHandler);
        }
        beanDef = new RootBeanDefinition(WebSocketHttpRequestHandler.class, cavs, null);
        beanDef.getPropertyValues().add("handshakeInterceptors", interceptors);
    }
    return new RuntimeBeanReference(registerBeanDef(beanDef, context, source));
}
Also used : SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)

Example 2 with OriginHandshakeInterceptor

use of org.springframework.web.socket.server.support.OriginHandshakeInterceptor in project spring-framework by spring-projects.

the class AbstractWebSocketHandlerRegistration method getInterceptors.

protected HandshakeInterceptor[] getInterceptors() {
    List<HandshakeInterceptor> interceptors = new ArrayList<>(this.interceptors.size() + 1);
    interceptors.addAll(this.interceptors);
    interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins));
    return interceptors.toArray(new HandshakeInterceptor[interceptors.size()]);
}
Also used : OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) ArrayList(java.util.ArrayList) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor)

Example 3 with OriginHandshakeInterceptor

use of org.springframework.web.socket.server.support.OriginHandshakeInterceptor in project spring-framework by spring-projects.

the class DefaultSockJsServiceTests method handleTransportRequestWebsocket.

@Test
public void handleTransportRequestWebsocket() throws Exception {
    TransportHandlingSockJsService wsService = new TransportHandlingSockJsService(this.taskScheduler, this.wsTransportHandler);
    String sockJsPath = "/websocket";
    setRequest("GET", sockJsPrefix + sockJsPath);
    wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertNotEquals(403, this.servletResponse.getStatus());
    resetRequestAndResponse();
    List<String> allowed = Collections.singletonList("http://mydomain1.com");
    OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(allowed);
    wsService.setHandshakeInterceptors(Collections.singletonList(interceptor));
    setRequest("GET", sockJsPrefix + sockJsPath);
    this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain1.com");
    wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertNotEquals(403, this.servletResponse.getStatus());
    resetRequestAndResponse();
    setRequest("GET", sockJsPrefix + sockJsPath);
    this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
    wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertEquals(403, this.servletResponse.getStatus());
}
Also used : TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) Test(org.junit.Test)

Example 4 with OriginHandshakeInterceptor

use of org.springframework.web.socket.server.support.OriginHandshakeInterceptor in project spring-framework by spring-projects.

the class WebMvcStompWebSocketEndpointRegistration method getInterceptors.

protected HandshakeInterceptor[] getInterceptors() {
    List<HandshakeInterceptor> interceptors = new ArrayList<>();
    interceptors.addAll(this.interceptors);
    interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins));
    return interceptors.toArray(new HandshakeInterceptor[interceptors.size()]);
}
Also used : OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) ArrayList(java.util.ArrayList) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor)

Example 5 with OriginHandshakeInterceptor

use of org.springframework.web.socket.server.support.OriginHandshakeInterceptor in project spring-framework by spring-projects.

the class HandlersBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext context) {
    Object source = context.extractSource(element);
    CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
    context.pushContainingComponent(compDefinition);
    String orderAttribute = element.getAttribute("order");
    int order = orderAttribute.isEmpty() ? DEFAULT_MAPPING_ORDER : Integer.valueOf(orderAttribute);
    RootBeanDefinition handlerMappingDef = new RootBeanDefinition(WebSocketHandlerMapping.class);
    handlerMappingDef.setSource(source);
    handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    handlerMappingDef.getPropertyValues().add("order", order);
    String handlerMappingName = context.getReaderContext().registerWithGeneratedName(handlerMappingDef);
    RuntimeBeanReference sockJsService = WebSocketNamespaceUtils.registerSockJsService(element, SOCK_JS_SCHEDULER_NAME, context, source);
    HandlerMappingStrategy strategy;
    if (sockJsService != null) {
        strategy = new SockJsHandlerMappingStrategy(sockJsService);
    } else {
        RuntimeBeanReference handshakeHandler = WebSocketNamespaceUtils.registerHandshakeHandler(element, context, source);
        Element interceptorsElement = DomUtils.getChildElementByTagName(element, "handshake-interceptors");
        ManagedList<? super Object> interceptors = WebSocketNamespaceUtils.parseBeanSubElements(interceptorsElement, context);
        String allowedOriginsAttribute = element.getAttribute("allowed-origins");
        List<String> allowedOrigins = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOriginsAttribute, ","));
        interceptors.add(new OriginHandshakeInterceptor(allowedOrigins));
        strategy = new WebSocketHandlerMappingStrategy(handshakeHandler, interceptors);
    }
    ManagedMap<String, Object> urlMap = new ManagedMap<>();
    urlMap.setSource(source);
    for (Element mappingElement : DomUtils.getChildElementsByTagName(element, "mapping")) {
        strategy.addMapping(mappingElement, urlMap, context);
    }
    handlerMappingDef.getPropertyValues().add("urlMap", urlMap);
    context.registerComponent(new BeanComponentDefinition(handlerMappingDef, handlerMappingName));
    context.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Aggregations

OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)6 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)2 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)2 Element (org.w3c.dom.Element)2 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)1 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)1 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)1 ManagedMap (org.springframework.beans.factory.support.ManagedMap)1 HandlerMapping (org.springframework.web.servlet.HandlerMapping)1 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)1 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)1 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)1 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)1 TransportHandlingSockJsService (org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)1