Search in sources :

Example 1 with DefaultHandshakeHandler

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

the class DefaultHandshakeHandlerTests method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    this.handshakeHandler = new DefaultHandshakeHandler(this.upgradeStrategy);
}
Also used : DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) Before(org.junit.Before)

Example 2 with DefaultHandshakeHandler

use of org.springframework.web.socket.server.support.DefaultHandshakeHandler in project steve by RWTH-i5-IDSG.

the class WebSocketConfiguration method registerWebSocketHandlers.

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
    policy.setMaxTextMessageBufferSize(MAX_MSG_SIZE);
    policy.setMaxTextMessageSize(MAX_MSG_SIZE);
    policy.setIdleTimeout(IDLE_TIMEOUT);
    List<AbstractWebSocketEndpoint> endpoints = getEndpoints();
    String[] protocols = endpoints.stream().map(e -> e.getVersion().getValue()).toArray(String[]::new);
    OcppWebSocketUpgrader upgradeStrategy = new OcppWebSocketUpgrader(policy, endpoints, chargePointRepository, notificationService, chargePointHelperService);
    DefaultHandshakeHandler handler = new DefaultHandshakeHandler(upgradeStrategy);
    handler.setSupportedProtocols(protocols);
    for (AbstractWebSocketEndpoint endpoint : endpoints) {
        registry.addHandler(endpoint, "/websocket/CentralSystemService/*").setHandshakeHandler(handler).setAllowedOrigins("*");
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Ocpp15WebSocketEndpoint(de.rwth.idsg.steve.ocpp.ws.ocpp15.Ocpp15WebSocketEndpoint) NotificationService(de.rwth.idsg.steve.service.NotificationService) Ocpp16WebSocketEndpoint(de.rwth.idsg.steve.ocpp.ws.ocpp16.Ocpp16WebSocketEndpoint) WebSocketConfigurer(org.springframework.web.socket.config.annotation.WebSocketConfigurer) WebSocketHandlerRegistry(org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry) OcppWebSocketUpgrader(de.rwth.idsg.steve.ocpp.ws.OcppWebSocketUpgrader) EnableWebSocket(org.springframework.web.socket.config.annotation.EnableWebSocket) Ocpp12WebSocketEndpoint(de.rwth.idsg.steve.ocpp.ws.ocpp12.Ocpp12WebSocketEndpoint) Autowired(org.springframework.beans.factory.annotation.Autowired) Configuration(org.springframework.context.annotation.Configuration) TimeUnit(java.util.concurrent.TimeUnit) ChargePointHelperService(de.rwth.idsg.steve.service.ChargePointHelperService) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Lists(com.google.common.collect.Lists) AbstractWebSocketEndpoint(de.rwth.idsg.steve.ocpp.ws.AbstractWebSocketEndpoint) WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketBehavior(org.eclipse.jetty.websocket.api.WebSocketBehavior) ChargePointRepository(de.rwth.idsg.steve.repository.ChargePointRepository) AbstractWebSocketEndpoint(de.rwth.idsg.steve.ocpp.ws.AbstractWebSocketEndpoint) OcppWebSocketUpgrader(de.rwth.idsg.steve.ocpp.ws.OcppWebSocketUpgrader) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler)

Example 3 with DefaultHandshakeHandler

use of org.springframework.web.socket.server.support.DefaultHandshakeHandler in project cetc by DiscoverForever.

the class WebsocketConfiguration method defaultHandshakeHandler.

private DefaultHandshakeHandler defaultHandshakeHandler() {
    return new DefaultHandshakeHandler() {

        @Override
        protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
            Principal principal = request.getPrincipal();
            if (principal == null) {
                Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
                authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.ANONYMOUS));
                principal = new AnonymousAuthenticationToken("WebsocketConfiguration", "anonymous", authorities);
            }
            return principal;
        }
    };
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) ArrayList(java.util.ArrayList) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) Map(java.util.Map) Principal(java.security.Principal)

Example 4 with DefaultHandshakeHandler

use of org.springframework.web.socket.server.support.DefaultHandshakeHandler in project ArTEMiS by ls1intum.

the class WebsocketConfiguration method defaultHandshakeHandler.

private DefaultHandshakeHandler defaultHandshakeHandler() {
    return new DefaultHandshakeHandler() {

        @Override
        protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
            Principal principal = request.getPrincipal();
            if (principal == null) {
                Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
                authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.ANONYMOUS));
                principal = new AnonymousAuthenticationToken("WebsocketConfiguration", "anonymous", authorities);
            }
            return principal;
        }
    };
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) Principal(java.security.Principal)

Example 5 with DefaultHandshakeHandler

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

the class WebMvcStompWebSocketEndpointRegistrationTests method handshakeHandlerAndInterceptorWithAllowedOrigins.

@Test
public void handshakeHandlerAndInterceptorWithAllowedOrigins() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    String origin = "https://mydomain.com";
    registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin);
    MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
    assertThat(entry.getValue()).isEqualTo(Arrays.asList("/foo"));
    WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
    assertThat(requestHandler.getWebSocketHandler()).isNotNull();
    assertThat(requestHandler.getHandshakeHandler()).isSameAs(handshakeHandler);
    assertThat(requestHandler.getHandshakeInterceptors().size()).isEqualTo(2);
    assertThat(requestHandler.getHandshakeInterceptors().get(0)).isEqualTo(interceptor);
    assertThat(requestHandler.getHandshakeInterceptors().get(1).getClass()).isEqualTo(OriginHandshakeInterceptor.class);
}
Also used : HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) List(java.util.List) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)13 Test (org.junit.jupiter.api.Test)7 List (java.util.List)5 Map (java.util.Map)5 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)5 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)5 MultiValueMap (org.springframework.util.MultiValueMap)4 HttpRequestHandler (org.springframework.web.HttpRequestHandler)4 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)4 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)4 Principal (java.security.Principal)3 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)3 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)3 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)3 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)2 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)2 TransportHandler (org.springframework.web.socket.sockjs.transport.TransportHandler)2