Add allowedOriginPatterns to WebSocketHandlerRegistration

Closes gh-26593
This commit is contained in:
Rossen Stoyanchev
2021-02-23 18:03:18 +00:00
parent ec5774e748
commit 0fd774e69f
3 changed files with 45 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,6 +54,8 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
private final List<String> allowedOrigins = new ArrayList<>();
private final List<String> allowedOriginPatterns = new ArrayList<>();
@Nullable
private SockJsServiceRegistration sockJsServiceRegistration;
@@ -94,6 +96,15 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
return this;
}
@Override
public WebSocketHandlerRegistration setAllowedOriginPatterns(String... allowedOriginPatterns) {
this.allowedOriginPatterns.clear();
if (!ObjectUtils.isEmpty(allowedOriginPatterns)) {
this.allowedOriginPatterns.addAll(Arrays.asList(allowedOriginPatterns));
}
return this;
}
@Override
public SockJsServiceRegistration withSockJS() {
this.sockJsServiceRegistration = new SockJsServiceRegistration();
@@ -108,13 +119,21 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
if (!this.allowedOrigins.isEmpty()) {
this.sockJsServiceRegistration.setAllowedOrigins(StringUtils.toStringArray(this.allowedOrigins));
}
if (!this.allowedOriginPatterns.isEmpty()) {
this.sockJsServiceRegistration.setAllowedOriginPatterns(
StringUtils.toStringArray(this.allowedOriginPatterns));
}
return this.sockJsServiceRegistration;
}
protected HandshakeInterceptor[] getInterceptors() {
List<HandshakeInterceptor> interceptors = new ArrayList<>(this.interceptors.size() + 1);
interceptors.addAll(this.interceptors);
interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins));
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(this.allowedOrigins);
if (!ObjectUtils.isEmpty(this.allowedOriginPatterns)) {
interceptor.setAllowedOriginPatterns(this.allowedOriginPatterns);
}
interceptors.add(interceptor);
return interceptors.toArray(new HandshakeInterceptor[0]);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -63,6 +63,15 @@ public interface WebSocketHandlerRegistration {
*/
WebSocketHandlerRegistration setAllowedOrigins(String... origins);
/**
* A variant of {@link #setAllowedOrigins(String...)} that accepts flexible
* domain patterns, e.g. {@code "https://*.domain1.com"}. Furthermore it
* always sets the {@code Access-Control-Allow-Origin} response header to
* the matched origin and never to {@code "*"}, nor to any other pattern.
* @since 5.3.5
*/
WebSocketHandlerRegistration setAllowedOriginPatterns(String... originPatterns);
/**
* Enable SockJS fallback options.
*/