Skip to content

Commit 9beca06

Browse files
committed
Polishing contribution
See gh-26108
1 parent ae75db2 commit 9beca06

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

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

Lines changed: 1 addition & 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.

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

Lines changed: 6 additions & 4 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.
@@ -62,9 +62,11 @@ public interface StompWebSocketEndpointRegistration {
6262
StompWebSocketEndpointRegistration setAllowedOrigins(String... origins);
6363

6464
/**
65-
* Configure allowed {@code Origin} header values.
66-
*
67-
* @see org.springframework.web.cors.CorsConfiguration#setAllowedOriginPatterns(java.util.List)
65+
* A variant of {@link #setAllowedOrigins(String...)} that accepts flexible
66+
* domain patterns, e.g. {@code "https://*.domain1.com"}. Furthermore it
67+
* always sets the {@code Access-Control-Allow-Origin} response header to
68+
* the matched origin and never to {@code "*"}, nor to any other pattern.
69+
* @since 5.3.2
6870
*/
6971
StompWebSocketEndpointRegistration setAllowedOriginPatterns(String... originPatterns);
7072

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@ public SockJsServiceRegistration withSockJS() {
132132
protected HandshakeInterceptor[] getInterceptors() {
133133
List<HandshakeInterceptor> interceptors = new ArrayList<>(this.interceptors.size() + 1);
134134
interceptors.addAll(this.interceptors);
135-
OriginHandshakeInterceptor originHandshakeInterceptor = new OriginHandshakeInterceptor(this.allowedOrigins);
136-
interceptors.add(originHandshakeInterceptor);
137-
135+
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(this.allowedOrigins);
136+
interceptors.add(interceptor);
138137
if (!ObjectUtils.isEmpty(this.allowedOriginPatterns)) {
139-
originHandshakeInterceptor.setAllowedOriginPatterns(this.allowedOriginPatterns);
138+
interceptor.setAllowedOriginPatterns(this.allowedOriginPatterns);
140139
}
141-
142140
return interceptors.toArray(new HandshakeInterceptor[0]);
143141
}
144142

spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java

Lines changed: 14 additions & 13 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.
@@ -82,18 +82,19 @@ public void setAllowedOrigins(Collection<String> allowedOrigins) {
8282
/**
8383
* Return the allowed {@code Origin} header values.
8484
* @since 4.1.5
85-
* @see #setAllowedOrigins
8685
*/
8786
public Collection<String> getAllowedOrigins() {
88-
if (this.corsConfiguration.getAllowedOrigins() == null) {
89-
return Collections.emptyList();
90-
}
91-
return Collections.unmodifiableSet(new HashSet<>(this.corsConfiguration.getAllowedOrigins()));
87+
return (this.corsConfiguration.getAllowedOrigins() != null ?
88+
Collections.unmodifiableSet(new HashSet<>(this.corsConfiguration.getAllowedOrigins())) :
89+
Collections.emptyList());
9290
}
9391

9492
/**
95-
* Configure allowed {@code Origin} pattern header values.
96-
*
93+
* A variant of {@link #setAllowedOrigins(Collection)} that accepts flexible
94+
* domain patterns, e.g. {@code "https://*.domain1.com"}. Furthermore it
95+
* always sets the {@code Access-Control-Allow-Origin} response header to
96+
* the matched origin and never to {@code "*"}, nor to any other pattern.
97+
* @since 5.3.2
9798
* @see CorsConfiguration#setAllowedOriginPatterns(List)
9899
*/
99100
public void setAllowedOriginPatterns(Collection<String> allowedOriginPatterns) {
@@ -108,18 +109,18 @@ public void setAllowedOriginPatterns(Collection<String> allowedOriginPatterns) {
108109
* @see CorsConfiguration#getAllowedOriginPatterns()
109110
*/
110111
public Collection<String> getAllowedOriginPatterns() {
111-
if (this.corsConfiguration.getAllowedOriginPatterns() == null) {
112-
return Collections.emptyList();
113-
}
114-
return Collections.unmodifiableSet(new HashSet<>(this.corsConfiguration.getAllowedOriginPatterns()));
112+
return (this.corsConfiguration.getAllowedOriginPatterns() != null ?
113+
Collections.unmodifiableSet(new HashSet<>(this.corsConfiguration.getAllowedOriginPatterns())) :
114+
Collections.emptyList());
115115
}
116116

117117

118118
@Override
119119
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
120120
WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
121121

122-
if (!WebUtils.isSameOrigin(request) && this.corsConfiguration.checkOrigin(request.getHeaders().getOrigin()) == null) {
122+
if (!WebUtils.isSameOrigin(request) &&
123+
this.corsConfiguration.checkOrigin(request.getHeaders().getOrigin()) == null) {
123124
response.setStatusCode(HttpStatus.FORBIDDEN);
124125
if (logger.isDebugEnabled()) {
125126
logger.debug("Handshake request rejected, Origin header value " +

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,12 @@ public void setAllowedOrigins(Collection<String> allowedOrigins) {
322322
}
323323

324324
/**
325-
* Configure allowed {@code Origin} header values.
326-
*
327-
* @see org.springframework.web.cors.CorsConfiguration#setAllowedOriginPatterns(java.util.List)
325+
* A variant of {@link #setAllowedOrigins(Collection)} that accepts flexible
326+
* domain patterns, e.g. {@code "https://*.domain1.com"}. Furthermore it
327+
* always sets the {@code Access-Control-Allow-Origin} response header to
328+
* the matched origin and never to {@code "*"}, nor to any other pattern.
329+
* <p>By default this is not set.
330+
* @since 5.2.3
328331
*/
329332
public void setAllowedOriginPatterns(Collection<String> allowedOriginPatterns) {
330333
Assert.notNull(allowedOriginPatterns, "Allowed origin patterns Collection must not be null");

0 commit comments

Comments
 (0)