Add CORS support for Private Network Access
This commit adds CORS support for Private Network Access
by adding an Access-Control-Allow-Private-Network response
header when the preflight request is sent with an
Access-Control-Request-Private-Network header and that
Private Network Access has been enabled in the CORS
configuration.
See https://developer.chrome.com/blog/private-network-access-preflight/
for more details.
Closes gh-31975
(cherry picked from commit 318d460256)
This commit is contained in:
@@ -84,6 +84,7 @@ public class CorsBeanDefinitionParser implements BeanDefinitionParser {
|
||||
}
|
||||
config.applyPermitDefaultValues();
|
||||
config.validateAllowCredentials();
|
||||
config.validateAllowPrivateNetwork();
|
||||
corsConfigurations.put(mapping.getAttribute("path"), config);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +132,17 @@ public class CorsRegistration {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether private network access is supported.
|
||||
* <p>By default this is not set (i.e. private network access is not supported).
|
||||
* @since 6.1.3
|
||||
* @see <a href="https://wicg.github.io/private-network-access/">Private network access specifications</a>
|
||||
*/
|
||||
public CorsRegistration allowPrivateNetwork(boolean allowPrivateNetwork) {
|
||||
this.config.setAllowPrivateNetwork(allowPrivateNetwork);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure how long in seconds the response from a pre-flight request
|
||||
* can be cached by clients.
|
||||
|
||||
@@ -536,6 +536,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||
}
|
||||
if (config != null) {
|
||||
config.validateAllowCredentials();
|
||||
config.validateAllowPrivateNetwork();
|
||||
}
|
||||
executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
|
||||
}
|
||||
|
||||
@@ -646,6 +646,7 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
CorsConfiguration corsConfig = initCorsConfiguration(handler, method, mapping);
|
||||
if (corsConfig != null) {
|
||||
corsConfig.validateAllowCredentials();
|
||||
corsConfig.validateAllowPrivateNetwork();
|
||||
this.corsLookup.put(handlerMethod, corsConfig);
|
||||
}
|
||||
|
||||
|
||||
@@ -522,6 +522,18 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
|
||||
"or an empty string (\"\"): current value is [" + allowCredentials + "]");
|
||||
}
|
||||
|
||||
String allowPrivateNetwork = resolveCorsAnnotationValue(annotation.allowPrivateNetwork());
|
||||
if ("true".equalsIgnoreCase(allowPrivateNetwork)) {
|
||||
config.setAllowPrivateNetwork(true);
|
||||
}
|
||||
else if ("false".equalsIgnoreCase(allowPrivateNetwork)) {
|
||||
config.setAllowPrivateNetwork(false);
|
||||
}
|
||||
else if (!allowPrivateNetwork.isEmpty()) {
|
||||
throw new IllegalStateException("@CrossOrigin's allowPrivateNetwork value must be \"true\", \"false\", " +
|
||||
"or an empty string (\"\"): current value is [" + allowPrivateNetwork + "]");
|
||||
}
|
||||
|
||||
if (annotation.maxAge() >= 0 ) {
|
||||
config.setMaxAge(annotation.maxAge());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user