Skip to content

Commit 684e695

Browse files
committed
Expose allowedOriginPatterns in SocketJS XML config
Closes gh-26108
1 parent 8130bf5 commit 684e695

File tree

8 files changed

+57
-12
lines changed

8 files changed

+57
-12
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/config/HandlersBeanDefinitionParser.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.beans.factory.xml.BeanDefinitionParser;
3333
import org.springframework.beans.factory.xml.ParserContext;
3434
import org.springframework.lang.Nullable;
35+
import org.springframework.util.ObjectUtils;
3536
import org.springframework.util.StringUtils;
3637
import org.springframework.util.xml.DomUtils;
3738
import org.springframework.web.socket.server.support.OriginHandshakeInterceptor;
@@ -85,7 +86,13 @@ public BeanDefinition parse(Element element, ParserContext context) {
8586
ManagedList<Object> interceptors = WebSocketNamespaceUtils.parseBeanSubElements(interceptElem, context);
8687
String allowedOrigins = element.getAttribute("allowed-origins");
8788
List<String> origins = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOrigins, ","));
88-
interceptors.add(new OriginHandshakeInterceptor(origins));
89+
String allowedOriginPatterns = element.getAttribute("allowed-origin-patterns");
90+
List<String> originPatterns = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOriginPatterns, ","));
91+
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(origins);
92+
if (!ObjectUtils.isEmpty(originPatterns)) {
93+
interceptor.setAllowedOriginPatterns(originPatterns);
94+
}
95+
interceptors.add(interceptor);
8996
strategy = new WebSocketHandlerMappingStrategy(handler, interceptors);
9097
}
9198

spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.springframework.util.Assert;
6161
import org.springframework.util.ClassUtils;
6262
import org.springframework.util.MimeTypeUtils;
63+
import org.springframework.util.ObjectUtils;
6364
import org.springframework.util.StringUtils;
6465
import org.springframework.util.xml.DomUtils;
6566
import org.springframework.web.socket.WebSocketHandler;
@@ -358,7 +359,13 @@ private RuntimeBeanReference registerRequestHandler(
358359
ManagedList<Object> interceptors = WebSocketNamespaceUtils.parseBeanSubElements(interceptElem, ctx);
359360
String allowedOrigins = element.getAttribute("allowed-origins");
360361
List<String> origins = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOrigins, ","));
361-
interceptors.add(new OriginHandshakeInterceptor(origins));
362+
String allowedOriginPatterns = element.getAttribute("allowed-origin-patterns");
363+
List<String> originPatterns = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOriginPatterns, ","));
364+
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(origins);
365+
if (!ObjectUtils.isEmpty(originPatterns)) {
366+
interceptor.setAllowedOriginPatterns(originPatterns);
367+
}
368+
interceptors.add(interceptor);
362369
ConstructorArgumentValues cargs = new ConstructorArgumentValues();
363370
cargs.addIndexedArgumentValue(0, subProtoHandler);
364371
cargs.addIndexedArgumentValue(1, handler);

spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketNamespaceUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -105,11 +105,18 @@ else if (handshakeHandler != null) {
105105

106106
Element interceptElem = DomUtils.getChildElementByTagName(element, "handshake-interceptors");
107107
ManagedList<Object> interceptors = WebSocketNamespaceUtils.parseBeanSubElements(interceptElem, context);
108+
108109
String allowedOrigins = element.getAttribute("allowed-origins");
109110
List<String> origins = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOrigins, ","));
110111
sockJsServiceDef.getPropertyValues().add("allowedOrigins", origins);
112+
113+
String allowedOriginPatterns = element.getAttribute("allowed-origin-patterns");
114+
List<String> originPatterns = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOriginPatterns, ","));
115+
sockJsServiceDef.getPropertyValues().add("allowedOriginPatterns", originPatterns);
116+
111117
RootBeanDefinition originHandshakeInterceptor = new RootBeanDefinition(OriginHandshakeInterceptor.class);
112118
originHandshakeInterceptor.getPropertyValues().add("allowedOrigins", origins);
119+
originHandshakeInterceptor.getPropertyValues().add("allowedOriginPatterns", originPatterns);
113120
interceptors.add(originHandshakeInterceptor);
114121
sockJsServiceDef.getPropertyValues().add("handshakeInterceptors", interceptors);
115122

spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket.xsd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,16 @@
541541
]]></xsd:documentation>
542542
</xsd:annotation>
543543
</xsd:attribute>
544+
<xsd:attribute name="allowed-origin-patterns" type="xsd:string">
545+
<xsd:annotation>
546+
<xsd:documentation><![CDATA[
547+
Alternative to allowed-origins that supports origins declared via patterns,
548+
e.g. "*.domain1.com".
549+
550+
By default this is not set.
551+
]]></xsd:documentation>
552+
</xsd:annotation>
553+
</xsd:attribute>
544554
</xsd:complexType>
545555
</xsd:element>
546556

@@ -739,6 +749,16 @@
739749
]]></xsd:documentation>
740750
</xsd:annotation>
741751
</xsd:attribute>
752+
<xsd:attribute name="allowed-origin-patterns" type="xsd:string">
753+
<xsd:annotation>
754+
<xsd:documentation><![CDATA[
755+
Alternative to allowed-origins that supports origins declared via patterns,
756+
e.g. "*.domain1.com".
757+
758+
By default this is not set.
759+
]]></xsd:documentation>
760+
</xsd:annotation>
761+
</xsd:attribute>
742762
</xsd:complexType>
743763
</xsd:element>
744764
<xsd:element name="stomp-error-handler" minOccurs="0">

spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -226,8 +226,8 @@ public void sockJsAttributes() {
226226
List<HandshakeInterceptor> interceptors = transportService.getHandshakeInterceptors();
227227
assertThat(interceptors).extracting("class").containsExactly(OriginHandshakeInterceptor.class);
228228
assertThat(transportService.shouldSuppressCors()).isTrue();
229-
assertThat(transportService.getAllowedOrigins().contains("https://mydomain1.example")).isTrue();
230-
assertThat(transportService.getAllowedOrigins().contains("https://mydomain2.example")).isTrue();
229+
assertThat(transportService.getAllowedOrigins()).containsExactly("https://mydomain1.example", "https://mydomain2.example");
230+
assertThat(transportService.getAllowedOriginPatterns()).containsExactly("https://*.mydomain.example");
231231
}
232232

233233

spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ public void simpleBroker() throws Exception {
181181
interceptors = defaultSockJsService.getHandshakeInterceptors();
182182
assertThat(interceptors).extracting("class").containsExactly(FooTestInterceptor.class,
183183
BarTestInterceptor.class, OriginHandshakeInterceptor.class);
184-
assertThat(defaultSockJsService.getAllowedOrigins().contains("https://mydomain3.com")).isTrue();
185-
assertThat(defaultSockJsService.getAllowedOrigins().contains("https://mydomain4.com")).isTrue();
184+
assertThat(defaultSockJsService.getAllowedOrigins()).containsExactly("https://mydomain3.com", "https://mydomain4.com");
185+
assertThat(defaultSockJsService.getAllowedOriginPatterns()).containsExactly("https://*.mydomain.com");
186186

187187
SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
188188
assertThat(userRegistry).isNotNull();

spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-broker-simple.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
</websocket:decorator-factories>
1818
</websocket:transport>
1919

20-
<websocket:stomp-endpoint path=" /foo,/bar" allowed-origins="https://mydomain1.example,https://mydomain2.example">
20+
<websocket:stomp-endpoint path=" /foo,/bar"
21+
allowed-origins="https://mydomain1.example,https://mydomain2.example"
22+
allowed-origin-patterns="https://*.mydomain.com">
2123
<websocket:handshake-handler ref="myHandler"/>
2224
<websocket:handshake-interceptors>
2325
<bean class="org.springframework.web.socket.config.FooTestInterceptor"/>
2426
<ref bean="barTestInterceptor"/>
2527
</websocket:handshake-interceptors>
2628
</websocket:stomp-endpoint>
2729

28-
<websocket:stomp-endpoint path="/test,/sockjs" allowed-origins="https://mydomain3.com,https://mydomain4.com">
30+
<websocket:stomp-endpoint path="/test,/sockjs"
31+
allowed-origins="https://mydomain3.com,https://mydomain4.com"
32+
allowed-origin-patterns="https://*.mydomain.com">
2933
<websocket:handshake-handler ref="myHandler"/>
3034
<websocket:handshake-interceptors>
3135
<bean class="org.springframework.web.socket.config.FooTestInterceptor"/>

spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-handlers-sockjs-attributes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
66
http://www.springframework.org/schema/websocket https://www.springframework.org/schema/websocket/spring-websocket.xsd">
77

8-
<websocket:handlers allowed-origins="https://mydomain1.example, https://mydomain2.example">
8+
<websocket:handlers allowed-origins="https://mydomain1.example, https://mydomain2.example" allowed-origin-patterns="https://*.mydomain.example">
99
<websocket:mapping path="/test" handler="testHandler"/>
1010
<websocket:sockjs name="testSockJsService" scheduler="testTaskScheduler" websocket-enabled="false"
1111
session-cookie-needed="false" stream-bytes-limit="2048" disconnect-delay="256"

0 commit comments

Comments
 (0)