@ -93,6 +93,9 @@ public class CorsConfiguration {
@Nullable
@Nullable
private Boolean allowCredentials ;
private Boolean allowCredentials ;
@Nullable
private Boolean allowPrivateNetwork ;
@Nullable
@Nullable
private Long maxAge ;
private Long maxAge ;
@ -117,6 +120,7 @@ public class CorsConfiguration {
this . allowedHeaders = other . allowedHeaders ;
this . allowedHeaders = other . allowedHeaders ;
this . exposedHeaders = other . exposedHeaders ;
this . exposedHeaders = other . exposedHeaders ;
this . allowCredentials = other . allowCredentials ;
this . allowCredentials = other . allowCredentials ;
this . allowPrivateNetwork = other . allowPrivateNetwork ;
this . maxAge = other . maxAge ;
this . maxAge = other . maxAge ;
}
}
@ -129,9 +133,10 @@ public class CorsConfiguration {
* { @code Access - Control - Allow - Origin } response header is set either to the
* { @code Access - Control - Allow - Origin } response header is set either to the
* matched domain value or to { @code "*" } . Keep in mind however that the
* matched domain value or to { @code "*" } . Keep in mind however that the
* CORS spec does not allow { @code "*" } when { @link # setAllowCredentials
* CORS spec does not allow { @code "*" } when { @link # setAllowCredentials
* allowCredentials } is set to { @code true } and as of 5 . 3 that combination
* allowCredentials } is set to { @code true } , and does not recommend { @code "*" }
* is rejected in favor of using { @link # setAllowedOriginPatterns
* when { @link # setAllowPrivateNetwork allowPrivateNetwork } is set to { @code true } .
* allowedOriginPatterns } instead .
* As a consequence , those combinations are rejected in favor of using
* { @link # setAllowedOriginPatterns allowedOriginPatterns } instead .
* < p > By default this is not set which means that no origins are allowed .
* < p > By default this is not set which means that no origins are allowed .
* However , an instance of this class is often initialized further , e . g . for
* However , an instance of this class is often initialized further , e . g . for
* { @code @CrossOrigin } , via { @link # applyPermitDefaultValues ( ) } .
* { @code @CrossOrigin } , via { @link # applyPermitDefaultValues ( ) } .
@ -182,11 +187,13 @@ public class CorsConfiguration {
* domain1 . com on any port , including the default port
* domain1 . com on any port , including the default port
* < / ul >
* < / ul >
* < p > In contrast to { @link # setAllowedOrigins ( List ) allowedOrigins } which
* < p > In contrast to { @link # setAllowedOrigins ( List ) allowedOrigins } which
* only supports "*" and cannot be used with { @code allowCredentials } , when
* only supports "*" and cannot be used with { @code allowCredentials } or
* an allowedOriginPattern is matched , the { @code Access - Control - Allow - Origin }
* { @code allowPrivateNetwork } , when an { @code allowedOriginPattern } is matched ,
* response header is set to the matched origin and not to { @code "*" } nor
* the { @code Access - Control - Allow - Origin } response header is set to the
* to the pattern . Therefore allowedOriginPatterns can be used in combination
* matched origin and not to { @code "*" } nor to the pattern .
* with { @link # setAllowCredentials } set to { @code true } .
* Therefore , { @code allowedOriginPatterns } can be used in combination with
* { @link # setAllowCredentials } and { @link # setAllowPrivateNetwork } set to
* { @code true }
* < p > By default this is not set .
* < p > By default this is not set .
* @since 5 . 3
* @since 5 . 3
* /
* /
@ -418,6 +425,33 @@ public class CorsConfiguration {
return this . allowCredentials ;
return this . allowCredentials ;
}
}
/ * *
* Whether private network access is supported for user - agents restricting such access by default .
* < p > Private network requests are requests whose target server ' s IP address is more private than
* that from which the request initiator was fetched . For example , a request from a public website
* ( https : //example.com) to a private website (https://router.local), or a request from a private
* website to localhost .
* < p > Setting this property has an impact on how { @link # setAllowedOrigins ( List )
* origins } and { @link # setAllowedOriginPatterns ( List ) originPatterns } are processed ,
* see related API documentation for more details .
* < 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 void setAllowPrivateNetwork ( @Nullable Boolean allowPrivateNetwork ) {
this . allowPrivateNetwork = allowPrivateNetwork ;
}
/ * *
* Return the configured { @code allowPrivateNetwork } flag , or { @code null } if none .
* @since 6 . 1 . 3
* @see # setAllowPrivateNetwork ( Boolean )
* /
@Nullable
public Boolean getAllowPrivateNetwork ( ) {
return this . allowPrivateNetwork ;
}
/ * *
/ * *
* Configure how long , as a duration , the response from a pre - flight request
* Configure how long , as a duration , the response from a pre - flight request
* can be cached by clients .
* can be cached by clients .
@ -500,6 +534,25 @@ public class CorsConfiguration {
}
}
}
}
/ * *
* Validate that when { @link # setAllowPrivateNetwork allowPrivateNetwork } is { @code true } ,
* { @link # setAllowedOrigins allowedOrigins } does not contain the special
* value { @code "*" } since this is insecure .
* @throws IllegalArgumentException if the validation fails
* @since 6 . 1 . 3
* /
public void validateAllowPrivateNetwork ( ) {
if ( this . allowPrivateNetwork = = Boolean . TRUE & &
this . allowedOrigins ! = null & & this . allowedOrigins . contains ( ALL ) ) {
throw new IllegalArgumentException (
"When allowPrivateNetwork is true, allowedOrigins cannot contain the special value \"*\" " +
"as it is not recommended from a security perspective. " +
"To allow private network access to a set of origins, list them explicitly " +
"or consider using \"allowedOriginPatterns\" instead." ) ;
}
}
/ * *
/ * *
* Combine the non - null properties of the supplied
* Combine the non - null properties of the supplied
* { @code CorsConfiguration } with this one .
* { @code CorsConfiguration } with this one .
@ -534,6 +587,10 @@ public class CorsConfiguration {
if ( allowCredentials ! = null ) {
if ( allowCredentials ! = null ) {
config . setAllowCredentials ( allowCredentials ) ;
config . setAllowCredentials ( allowCredentials ) ;
}
}
Boolean allowPrivateNetwork = other . getAllowPrivateNetwork ( ) ;
if ( allowPrivateNetwork ! = null ) {
config . setAllowPrivateNetwork ( allowPrivateNetwork ) ;
}
Long maxAge = other . getMaxAge ( ) ;
Long maxAge = other . getMaxAge ( ) ;
if ( maxAge ! = null ) {
if ( maxAge ! = null ) {
config . setMaxAge ( maxAge ) ;
config . setMaxAge ( maxAge ) ;
@ -597,6 +654,7 @@ public class CorsConfiguration {
if ( ! ObjectUtils . isEmpty ( this . allowedOrigins ) ) {
if ( ! ObjectUtils . isEmpty ( this . allowedOrigins ) ) {
if ( this . allowedOrigins . contains ( ALL ) ) {
if ( this . allowedOrigins . contains ( ALL ) ) {
validateAllowCredentials ( ) ;
validateAllowCredentials ( ) ;
validateAllowPrivateNetwork ( ) ;
return ALL ;
return ALL ;
}
}
for ( String allowedOrigin : this . allowedOrigins ) {
for ( String allowedOrigin : this . allowedOrigins ) {