org.springframework.web.socket.server.support.OriginHandshakeInterceptor Java Examples

The following examples show how to use org.springframework.web.socket.server.support.OriginHandshakeInterceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void handshakeHandlerAndInterceptor() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor);

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo"), entry.getValue());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #2
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerAndInterceptor() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor);

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo"), entry.getValue());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #3
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void interceptorsPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo")
			.addInterceptors(interceptor)
			.setAllowedOrigins("http://mydomain1.com")
			.withSockJS();

	this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);
	assertTrue(mapping.sockJsService.getAllowedOrigins().contains("http://mydomain1.com"));
	List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
	assertEquals(interceptor, interceptors.get(0));
	assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}
 
Example #4
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void interceptorsWithAllowedOrigins() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #5
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void emptyAllowedOrigin() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #6
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void interceptors() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #7
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void minimal() {
	WebSocketHandler handler = new TextWebSocketHandler();
	this.registration.addHandler(handler, "/foo", "/bar");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(2, mappings.size());

	Mapping m1 = mappings.get(0);
	assertEquals(handler, m1.webSocketHandler);
	assertEquals("/foo", m1.path);
	assertNotNull(m1.interceptors);
	assertEquals(1, m1.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m1.interceptors[0].getClass());

	Mapping m2 = mappings.get(1);
	assertEquals(handler, m2.webSocketHandler);
	assertEquals("/bar", m2.path);
	assertNotNull(m2.interceptors);
	assertEquals(1, m2.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m2.interceptors[0].getClass());
}
 
Example #8
Source File: DefaultSockJsServiceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
}
 
Example #9
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void emptyAllowedOrigin() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #10
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptorsWithAllowedOrigins() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #11
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptorsPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor)
			.setAllowedOrigins("http://mydomain1.com").withSockJS();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);
	assertTrue(mapping.sockJsService.getAllowedOrigins().contains("http://mydomain1.com"));
	List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
	assertEquals(interceptor, interceptors.get(0));
	assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}
 
Example #12
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 = "http://mydomain.com";
	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin);

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo"), entry.getValue());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #13
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptors() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #14
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 = "http://mydomain.com";
	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin);

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo"), entry.getValue());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #15
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo"), entry.getValue());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #16
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerAndInterceptor() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor);

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo"), entry.getValue());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #17
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void interceptorsPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo")
			.addInterceptors(interceptor)
			.setAllowedOrigins("https://mydomain1.com")
			.withSockJS();

	this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);
	assertTrue(mapping.sockJsService.getAllowedOrigins().contains("https://mydomain1.com"));
	List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
	assertEquals(interceptor, interceptors.get(0));
	assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}
 
Example #18
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void interceptorsWithAllowedOrigins() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("https://mydomain1.com");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #19
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void emptyAllowedOrigin() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #20
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void interceptors() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #21
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void minimal() {
	WebSocketHandler handler = new TextWebSocketHandler();
	this.registration.addHandler(handler, "/foo", "/bar");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(2, mappings.size());

	Mapping m1 = mappings.get(0);
	assertEquals(handler, m1.webSocketHandler);
	assertEquals("/foo", m1.path);
	assertNotNull(m1.interceptors);
	assertEquals(1, m1.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m1.interceptors[0].getClass());

	Mapping m2 = mappings.get(1);
	assertEquals(handler, m2.webSocketHandler);
	assertEquals("/bar", m2.path);
	assertNotNull(m2.interceptors);
	assertEquals(1, m2.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m2.interceptors[0].getClass());
}
 
Example #22
Source File: DefaultSockJsServiceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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("https://mydomain1.com");
	OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(allowed);
	wsService.setHandshakeInterceptors(Collections.singletonList(interceptor));
	setRequest("GET", sockJsPrefix + sockJsPath);
	this.servletRequest.addHeader(HttpHeaders.ORIGIN, "https://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, "https://mydomain2.com");
	wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
	assertEquals(403, this.servletResponse.getStatus());
}
 
Example #23
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void minimal() {
	WebSocketHandler handler = new TextWebSocketHandler();
	this.registration.addHandler(handler, "/foo", "/bar");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(2, mappings.size());

	Mapping m1 = mappings.get(0);
	assertEquals(handler, m1.webSocketHandler);
	assertEquals("/foo", m1.path);
	assertEquals(1, m1.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m1.interceptors[0].getClass());

	Mapping m2 = mappings.get(1);
	assertEquals(handler, m2.webSocketHandler);
	assertEquals("/bar", m2.path);
	assertEquals(1, m2.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m2.interceptors[0].getClass());
}
 
Example #24
Source File: DefaultSockJsServiceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #25
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void allowedOrigins() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	registration.setAllowedOrigins();

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());
	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler)mappings.entrySet().iterator().next().getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertEquals(1, requestHandler.getHandshakeInterceptors().size());
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(0).getClass());
}
 
Example #26
Source File: HandlersBeanDefinitionParserTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #27
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void sameOrigin() {
	WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	registration.setAllowedOrigins();

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());
	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler)mappings.entrySet().iterator().next().getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertEquals(1, requestHandler.getHandshakeInterceptors().size());
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(0).getClass());
}
 
Example #28
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void handshakeHandlerInterceptorWithSockJsService() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).withSockJS();

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo/**"), entry.getValue());

	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());

	DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
	assertNotNull(sockJsService);

	Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
	WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
	assertEquals(2, sockJsService.getHandshakeInterceptors().size());
	assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, sockJsService.getHandshakeInterceptors().get(1).getClass());
}
 
Example #29
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void handshakeHandlerInterceptorWithSockJsServiceAndAllowedOrigins() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
	String origin = "http://mydomain.com";

	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin).withSockJS();

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo/**"), entry.getValue());

	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());

	DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
	assertNotNull(sockJsService);

	Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
	WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
	assertEquals(2, sockJsService.getHandshakeInterceptors().size());
	assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class,
			sockJsService.getHandshakeInterceptors().get(1).getClass());
	assertTrue(sockJsService.getAllowedOrigins().contains(origin));
}
 
Example #30
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void handshakeHandlerInterceptorWithSockJsServiceAndAllowedOrigins() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
	String origin = "http://mydomain.com";

	registration.setHandshakeHandler(handshakeHandler)
			.addInterceptors(interceptor).setAllowedOrigins(origin).withSockJS();

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo/**"), entry.getValue());

	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());

	DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
	assertNotNull(sockJsService);

	Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
	WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
	assertEquals(2, sockJsService.getHandshakeInterceptors().size());
	assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class,
			sockJsService.getHandshakeInterceptors().get(1).getClass());
	assertTrue(sockJsService.getAllowedOrigins().contains(origin));
}