Interface CorsAuthorizer
Standard threadsafe implementations can be acquired via these factory methods:
withRejectAllPolicy()(don't permit CORS requests)withAcceptAllPolicy()(permit all CORS requests, not recommended for production)withWhitelistedOrigins(Set)(permit whitelisted origins only)withWhitelistedOrigins(Set, Function)(permit whitelisted origins only + control credentials behavior)withWhitelistAuthorizer(Function)(permit origins via function)withWhitelistAuthorizer(Function, Function)(permit origins via function + control credentials behavior)
- Author:
- Mark Allen
-
Method Summary
Modifier and TypeMethodDescriptionAuthorizes a non-preflight CORS request.authorizePreflight(Request request, CorsPreflight corsPreflight, Map<HttpMethod, ResourceMethod> availableResourceMethodsByHttpMethod) Authorizes a CORS preflight request.static CorsAuthorizerAcquires a threadsafeCorsAuthorizerconfigured to permit all cross-domain requests regardless ofOrigin.static CorsAuthorizerAcquires a threadsafeCorsAuthorizerconfigured to reject all cross-domain requests regardless ofOrigin.static CorsAuthorizerwithWhitelistAuthorizer(Function<String, Boolean> whitelistAuthorizer) Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginis allowed by the providedwhitelistAuthorizerfunction.static CorsAuthorizerwithWhitelistAuthorizer(Function<String, Boolean> whitelistAuthorizer, Function<String, Boolean> allowCredentialsResolver) Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginis allowed by the providedwhitelistAuthorizerfunction.static CorsAuthorizerwithWhitelistedOrigins(Set<String> whitelistedOrigins) Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginmatches a value in the provided set ofwhitelistedOrigins.static CorsAuthorizerwithWhitelistedOrigins(Set<String> whitelistedOrigins, Function<String, Boolean> allowCredentialsResolver) Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginmatches a value in the provided set ofwhitelistedOrigins.
-
Method Details
-
authorize
Authorizes a non-preflight CORS request.- Parameters:
request- the request to authorizecors- the CORS data provided in the request- Returns:
- a
CorsResponseif authorized, orOptional.empty()if not authorized
-
authorizePreflight
@Nonnull Optional<CorsPreflightResponse> authorizePreflight(@Nonnull Request request, @Nonnull CorsPreflight corsPreflight, @Nonnull Map<HttpMethod, ResourceMethod> availableResourceMethodsByHttpMethod) Authorizes a CORS preflight request.- Parameters:
request- the preflight request to authorizecorsPreflight- the CORS preflight data provided in the requestavailableResourceMethodsByHttpMethod- Resource Methods that are available to serve requests according to parameters specified by the preflight data- Returns:
- a
CorsPreflightResponseif authorized, orOptional.empty()if not authorized
-
withAcceptAllPolicy
Acquires a threadsafeCorsAuthorizerconfigured to permit all cross-domain requests regardless ofOrigin.The returned instance is guaranteed to be a JVM-wide singleton.
Note: the returned instance is generally unsafe for production - prefer
withWhitelistedOrigins(Set)orwithWhitelistAuthorizer(Function)for production systems.- Returns:
- a
CorsAuthorizerconfigured to permit all cross-domain requests
-
withRejectAllPolicy
Acquires a threadsafeCorsAuthorizerconfigured to reject all cross-domain requests regardless ofOrigin.The returned instance is guaranteed to be a JVM-wide singleton.
- Returns:
- a
CorsAuthorizerconfigured to reject all cross-domain requests
-
withWhitelistedOrigins
Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginmatches a value in the provided set ofwhitelistedOrigins.The returned
CorsAuthorizerwill setAccess-Control-Allow-Credentialsheader totrue. This behavior can be customized viawithWhitelistedOrigins(Set, Function).Callers should not rely on reference identity; this method may return a new or cached instance.
- Parameters:
whitelistedOrigins- the set of whitelisted origins- Returns:
- a credentials-allowed
CorsAuthorizerconfigured to accept only the specifiedwhitelistedOrigins
-
withWhitelistedOrigins
@Nonnull static CorsAuthorizer withWhitelistedOrigins(@Nonnull Set<String> whitelistedOrigins, @Nonnull Function<String, Boolean> allowCredentialsResolver) Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginmatches a value in the provided set ofwhitelistedOrigins.The provided
allowCredentialsResolveris used to control the value ofAccess-Control-Allow-Credentials: it's a function which takes a normalizedOriginas input and should returntrueif clients are permitted to include credentials in cross-origin HTTP requests andfalseotherwise.The returned
CorsAuthorizerwill omit theAccess-Control-Allow-Credentialsresponse header to reduce CSRF attack surface area. This behavior can be customized viawithWhitelistAuthorizer(Function, Function).Callers should not rely on reference identity; this method may return a new or cached instance.
- Parameters:
whitelistedOrigins- the set of whitelisted originsallowCredentialsResolver- function which takes a normalizedOriginas input and should returntrueif clients are permitted to include credentials in cross-origin HTTP requests andfalseotherwise- Returns:
- a
CorsAuthorizerconfigured to accept only the specifiedwhitelistedOrigins, withallowCredentialsResolverdictating whether credentials are allowed
-
withWhitelistAuthorizer
@Nonnull static CorsAuthorizer withWhitelistAuthorizer(@Nonnull Function<String, Boolean> whitelistAuthorizer) Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginis allowed by the providedwhitelistAuthorizerfunction.The
whitelistAuthorizerfunction should returntrueif the suppliedOriginis acceptable andfalseotherwise.The returned
CorsAuthorizerwill omit theAccess-Control-Allow-Credentialsresponse header to reduce CSRF attack surface area. This behavior can be customized viawithWhitelistAuthorizer(Function, Function).Callers should not rely on reference identity; this method may return a new or cached instance.
- Parameters:
whitelistAuthorizer- a function that returnstrueif the input is a whitelisted origin andfalseotherwise- Returns:
- a credentials-allowed
CorsAuthorizerconfigured to accept only the origins permitted bywhitelistAuthorizer
-
withWhitelistAuthorizer
@Nonnull static CorsAuthorizer withWhitelistAuthorizer(@Nonnull Function<String, Boolean> whitelistAuthorizer, @Nonnull Function<String, Boolean> allowCredentialsResolver) Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginis allowed by the providedwhitelistAuthorizerfunction.The
whitelistAuthorizerfunction should returntrueif the suppliedOriginis acceptable andfalseotherwise.The provided
allowCredentialsResolveris used to control the value ofAccess-Control-Allow-Credentials: it's a function which takes a normalizedOriginas input and should returntrueif clients are permitted to include credentials in cross-origin HTTP requests andfalseotherwise.Callers should not rely on reference identity; this method may return a new or cached instance.
- Parameters:
whitelistAuthorizer- a function that returnstrueif the input is a whitelisted origin andfalseotherwiseallowCredentialsResolver- function which takes a normalizedOriginas input and should returntrueif clients are permitted to include credentials in cross-origin HTTP requests andfalseotherwise- Returns:
- a
CorsAuthorizerconfigured to accept only the origins permitted bywhitelistAuthorizer, withallowCredentialsResolverdictating whether credentials are allowed
-