Search in sources :

Example 11 with WebSocketHttpRequestHandler

use of org.springframework.web.socket.server.support.WebSocketHttpRequestHandler 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

WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)11 Test (org.junit.Test)8 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)7 HttpRequestHandler (org.springframework.web.HttpRequestHandler)6 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)5 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)4 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)4 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)4 List (java.util.List)3 HandlerMapping (org.springframework.web.servlet.HandlerMapping)3 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)3 Map (java.util.Map)2 MultiValueMap (org.springframework.util.MultiValueMap)2 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)2 WebSocketSession (org.springframework.web.socket.WebSocketSession)2 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)2 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)2 SockJsService (org.springframework.web.socket.sockjs.SockJsService)2 ArrayList (java.util.ArrayList)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1