类 CorsConfiguration
By default a newly created CorsConfiguration does not permit any
cross-origin requests and must be configured explicitly to indicate what
should be allowed. Use applyPermitDefaultValues() to flip the
initialization model to start with open defaults that permit all cross-origin
requests for GET, HEAD, and POST requests.
- 从以下版本开始:
- 2.3.7
- 作者:
- Sebastien Deleuze, Rossen Stoyanchev, Juergen Hoeller, Sam Brannen, TODAY
2019-12-08 16:39 - 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private static classContains both the user-declared pattern (e.g. -
字段概要
字段修饰符和类型字段说明static final StringWildcard representing all origins, methods, or headers.private static final CorsConfiguration.OriginPatternprivate static final List<CorsConfiguration.OriginPattern>private Booleanprivate List<CorsConfiguration.OriginPattern>private static final List<HttpMethod>private Longprivate List<HttpMethod> -
构造器概要
构造器构造器说明Construct a newCorsConfigurationinstance with no cross-origin requests allowed for any origin by default.Construct a newCorsConfigurationinstance by copying all values from the suppliedCorsConfiguration. -
方法概要
修饰符和类型方法说明voidaddAllowedHeader(String allowedHeader) Variant ofsetAllowedHeaders(List)for adding one allowed header at a time.voidaddAllowedMethod(HttpMethod method) Variant ofsetAllowedMethods(java.util.List<java.lang.String>)for adding one allowed method at a time.voidaddAllowedMethod(String method) Variant ofsetAllowedMethods(java.util.List<java.lang.String>)for adding one allowed method at a time.voidaddAllowedOrigin(String origin) Variant ofsetAllowedOrigins(java.util.List<java.lang.String>)for adding one origin at a time.voidaddAllowedOriginPattern(String originPattern) Variant ofsetAllowedOriginPatterns(java.util.List<java.lang.String>)for adding one origin at a time.voidaddExposedHeader(String exposedHeader) Variant ofsetExposedHeaders(java.util.List<java.lang.String>)for adding one exposed header at a time.By default a newly createdCorsConfigurationdoes not permit any cross-origin requests and must be configured explicitly to indicate what should be allowed.checkHeaders(List<String> requestHeaders) Check the supplied request headers (or the headers listed in theAccess-Control-Request-Headersof a pre-flight request) against the configured allowed headers.checkHttpMethod(HttpMethod method) Check the HTTP request method (or the method from theAccess-Control-Request-Methodheader on a pre-flight request) against the configured allowed methods.checkOrigin(String requestOrigin) Check the origin of the request against the configured allowed origins.combine(CorsConfiguration other) Combine the non-null properties of the suppliedCorsConfigurationwith this one.private List<CorsConfiguration.OriginPattern>combinePatterns(List<CorsConfiguration.OriginPattern> source, List<CorsConfiguration.OriginPattern> other) Return the configuredallowCredentialsflag, ornullif none.Return the allowed actual request headers, ornullif none.Return the allowed HTTP methods, ornullin which case only"GET"and"HEAD"allowed.Return the configured origins patterns to allow, ornullif none.Return the configured origins to allow, ornullif none.Return the configured response headers to expose, ornullif none.Return the configuredmaxAgevalue, ornullif none.private static voidparseCommaDelimitedOrigin(String rawValue, Consumer<String> valueConsumer) voidsetAllowCredentials(Boolean allowCredentials) Whether user credentials are supported.voidsetAllowedHeaders(List<String> allowedHeaders) Set the list of headers that a pre-flight request can list as allowed for use during an actual request.voidsetAllowedMethods(List<String> allowedMethods) Set the HTTP methods to allow, e.g.setAllowedOriginPatterns(List<String> allowedOriginPatterns) Alternative tosetAllowedOrigins(java.util.List<java.lang.String>)that supports more flexible origins patterns with "*" anywhere in the host name in addition to port lists.voidsetAllowedOrigins(List<String> origins) A list of origins for which cross-origin requests are allowed where each value may be one of the following: a specific domain, e.g.voidsetExposedHeaders(List<String> exposedHeaders) Set the list of response headers that an actual response might have and can be exposed to the client.voidConfigure how long, in seconds, the response from a pre-flight request can be cached by clients.voidConfigure how long, as a duration, the response from a pre-flight request can be cached by clients.private StringtrimTrailingSlash(String origin) voidValidate that whenallowCredentialsis true,allowedOriginsdoes not contain the special value"*"since in that case the "Access-Control-Allow-Origin" cannot be set to"*".
-
字段详细资料
-
ALL
Wildcard representing all origins, methods, or headers.- 另请参阅:
-
DEFAULT_METHODS
-
DEFAULT_PERMIT_ALL
-
DEFAULT_PERMIT_METHODS
-
ALL_PATTERN
- 从以下版本开始:
- 3.0
-
ALL_PATTERN_LIST
- 从以下版本开始:
- 3.0
-
ALL_LIST
- 从以下版本开始:
- 3.0
-
maxAge
-
allowCredentials
-
allowedOrigins
-
allowedMethods
-
allowedHeaders
-
exposedHeaders
-
resolvedMethods
-
allowedOriginPatterns
- 从以下版本开始:
- 3.0
-
-
构造器详细资料
-
CorsConfiguration
public CorsConfiguration()Construct a newCorsConfigurationinstance with no cross-origin requests allowed for any origin by default. -
CorsConfiguration
Construct a newCorsConfigurationinstance by copying all values from the suppliedCorsConfiguration.
-
-
方法详细资料
-
setAllowedOrigins
A list of origins for which cross-origin requests are allowed where each value may be one of the following:- a specific domain, e.g.
"https://domain1.com" - comma-delimited list of specific domains, e.g.
"https://a1.com,https://a2.com"; this is convenient when a value is resolved through a property placeholder, e.g."${origin}"; note that such placeholders must be resolved externally. - the CORS defined special value
"*"for all origins
For matched pre-flight and actual requests the
Access-Control-Allow-Originresponse header is set either to the matched domain value or to"*". Keep in mind however that the CORS spec does not allow"*"whenallowCredentialsis set totrueand as of 5.3 that combination is rejected in favor of usingallowedOriginPatternsinstead.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
@CrossOrigin, viaapplyPermitDefaultValues(). - a specific domain, e.g.
-
trimTrailingSlash
-
getAllowedOrigins
Return the configured origins to allow, ornullif none. -
addAllowedOrigin
Variant ofsetAllowedOrigins(java.util.List<java.lang.String>)for adding one origin at a time. -
setAllowedOriginPatterns
Alternative tosetAllowedOrigins(java.util.List<java.lang.String>)that supports more flexible origins patterns with "*" anywhere in the host name in addition to port lists. Examples:- https://*.domain1.com -- domains ending with domain1.com
- https://*.domain1.com:[8080,8081] -- domains ending with domain1.com on port 8080 or port 8081
- https://*.domain1.com:[*] -- domains ending with domain1.com on any port, including the default port
- comma-delimited list of patters, e.g.
"https://*.a1.com,https://*.a2.com"; this is convenient when a value is resolved through a property placeholder, e.g."${origin}"; note that such placeholders must be resolved externally.
In contrast to
allowedOriginswhich only supports "*" and cannot be used withallowCredentials, when an allowedOriginPattern is matched, theAccess-Control-Allow-Originresponse header is set to the matched origin and not to"*"nor to the pattern. Therefore, allowedOriginPatterns can be used in combination withsetAllowCredentials(java.lang.Boolean)set totrue.By default this is not set.
- 从以下版本开始:
- 3.0
-
getAllowedOriginPatterns
Return the configured origins patterns to allow, ornullif none.- 从以下版本开始:
- 3.0
-
addAllowedOriginPattern
Variant ofsetAllowedOriginPatterns(java.util.List<java.lang.String>)for adding one origin at a time.- 从以下版本开始:
- 3.0
-
parseCommaDelimitedOrigin
-
setAllowedMethods
Set the HTTP methods to allow, e.g."GET","POST","PUT", etc. The special value"*"allows all methods.Access-Control-Allow-Methodsresponse header is set either to the configured method or to"*". Keep in mind however that the CORS spec does not allow"*"whenallowCredentialsis set totrue, that combination is handled by copying the method specified in the CORS preflight request.If not set, only
"GET"and"HEAD"are allowed.By default this is not set.
Note: CORS checks use values from "Forwarded" (RFC 7239), "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers, if present, in order to reflect the client-originated address. Consider using the
ForwardedHeaderFilterin order to choose from a central place whether to extract and use, or to discard such headers. -
getAllowedMethods
Return the allowed HTTP methods, ornullin which case only"GET"and"HEAD"allowed. -
addAllowedMethod
Variant ofsetAllowedMethods(java.util.List<java.lang.String>)for adding one allowed method at a time. -
addAllowedMethod
Variant ofsetAllowedMethods(java.util.List<java.lang.String>)for adding one allowed method at a time. -
setAllowedHeaders
Set the list of headers that a pre-flight request can list as allowed for use during an actual request. The special value"*"allows actual requests to send any header.Access-Control-Allow-Headersresponse header is set either to the configured list of headers or to"*". Keep in mind however that the CORS spec does not allow"*"whenallowCredentialsis set totrue, that combination is handled by copying the headers specified in the CORS preflight request.A header name is not required to be listed if it is one of:
Cache-Control,Content-Language,Expires,Last-Modified, orPragma.By default this is not set.
-
getAllowedHeaders
Return the allowed actual request headers, ornullif none. -
addAllowedHeader
Variant ofsetAllowedHeaders(List)for adding one allowed header at a time. -
setExposedHeaders
Set the list of response headers that an actual response might have and can be exposed to the client. The special value"*"allows all headers to be exposed.Access-Control-Expose-Headersresponse header is set either to the configured list of headers or to"*". While the CORS spec does not allow"*"whenAccess-Control-Allow-Credentialsis set totrue, most browsers support it and the response headers are not all available during the CORS processing, so as a consequence"*"is the header value used when specified regardless of the value of the `allowCredentials` property.A header name is not required to be listed if it is one of:
Cache-Control,Content-Language,Expires,Last-Modified, orPragma.By default this is not set.
-
getExposedHeaders
Return the configured response headers to expose, ornullif none. -
addExposedHeader
Variant ofsetExposedHeaders(java.util.List<java.lang.String>)for adding one exposed header at a time. -
setAllowCredentials
Whether user credentials are supported.Setting this property has an impact on how
origins,originPatterns,allowedMethodsandallowedHeadersare processed, see related API documentation for more details.NOTE: Be aware that this option establishes a high level of trust with the configured domains and also increases the surface attack of the web application by exposing sensitive user-specific information such as cookies and CSRF tokens.
By default this is not set (i.e. user credentials are not supported).
-
getAllowCredentials
Return the configuredallowCredentialsflag, ornullif none. -
setMaxAge
Configure how long, as a duration, the response from a pre-flight request can be cached by clients.- 另请参阅:
-
setMaxAge
Configure how long, in seconds, the response from a pre-flight request can be cached by clients.By default this is not set.
-
getMaxAge
Return the configuredmaxAgevalue, ornullif none.- 另请参阅:
-
applyPermitDefaultValues
By default a newly createdCorsConfigurationdoes not permit any cross-origin requests and must be configured explicitly to indicate what should be allowed.Use this method to flip the initialization model to start with open defaults that permit all cross-origin requests for GET, HEAD, and POST requests. Note however that this method will not override any existing values already set.
The following defaults are applied if not already set:
- Allow all origins.
- Allow "simple" methods
GET,HEADandPOST. - Allow all headers.
- Set max age to 1800 seconds (30 minutes).
-
combine
Combine the non-null properties of the suppliedCorsConfigurationwith this one.When combining single values like
allowCredentialsormaxAge,thisproperties are overridden by non-nullotherproperties if any.Combining lists like
allowedOrigins,allowedMethods,allowedHeadersorexposedHeadersis done in an additive way. For example, combining["GET", "POST"]with["PATCH"]results in["GET", "POST", "PATCH"], but keep in mind that combining["GET", "POST"]with["*"]results in["*"].Notice that default permit values set by
applyPermitDefaultValues()are overridden by any value explicitly defined.- 返回:
- the combined
CorsConfiguration, orthisconfiguration if the supplied configuration isnull
-
combine
-
combinePatterns
private List<CorsConfiguration.OriginPattern> combinePatterns(@Nullable List<CorsConfiguration.OriginPattern> source, @Nullable List<CorsConfiguration.OriginPattern> other) -
checkOrigin
Check the origin of the request against the configured allowed origins.- 参数:
requestOrigin- the origin to check- 返回:
- the origin to use for the response, or
nullwhich means the request origin is not allowed
-
validateAllowCredentials
public void validateAllowCredentials()Validate that whenallowCredentialsis true,allowedOriginsdoes not contain the special value"*"since in that case the "Access-Control-Allow-Origin" cannot be set to"*".- 抛出:
IllegalArgumentException- if the validation fails- 从以下版本开始:
- 3.0
-
checkHeaders
Check the supplied request headers (or the headers listed in theAccess-Control-Request-Headersof a pre-flight request) against the configured allowed headers.- 参数:
requestHeaders- the request headers to check- 返回:
- the list of allowed headers to list in the response of a pre-flight
request, or
nullif none of the supplied request headers is allowed
-
checkHttpMethod
Check the HTTP request method (or the method from theAccess-Control-Request-Methodheader on a pre-flight request) against the configured allowed methods.- 参数:
method- the HTTP request method to check- 返回:
- the list of HTTP methods to list in the response of a pre-flight
request, or
nullif the suppliedrequestMethodis not allowed
-