Interface CorsAuthorizer
Standard threadsafe implementations can be acquired via these factory methods:
rejectAllInstance()(don't permit CORS requests)acceptAllInstance()(permit all CORS requests, not recommended for production)fromWhitelistedOrigins(Set)(permit whitelisted origins only)fromWhitelistedOrigins(Set, Function)(permit whitelisted origins only + control credentials behavior)fromWhitelistAuthorizer(Function)(permit origins via function)fromWhitelistAuthorizer(Function, Function)(permit origins via function + control credentials behavior)
- Author:
- Mark Allen
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull CorsAuthorizerAcquires a threadsafeCorsAuthorizerconfigured to permit all cross-domain requests regardless ofOrigin.Authorizes a non-preflight CORS request.authorizePreflight(@NonNull Request request, @NonNull CorsPreflight corsPreflight, @NonNull Map<@NonNull HttpMethod, @NonNull ResourceMethod> availableResourceMethodsByHttpMethod) Authorizes a CORS preflight request.static @NonNull CorsAuthorizerfromWhitelistAuthorizer(@NonNull Function<String, Boolean> whitelistAuthorizer) Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginis allowed by the providedwhitelistAuthorizerfunction.static @NonNull CorsAuthorizerfromWhitelistAuthorizer(@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.static @NonNull CorsAuthorizerfromWhitelistedOrigins(@NonNull Set<@NonNull String> whitelistedOrigins) Acquires a threadsafeCorsAuthorizerconfigured to accept only those cross-domain requests whoseOriginmatches a value in the provided set ofwhitelistedOrigins.static @NonNull CorsAuthorizerfromWhitelistedOrigins(@NonNull Set<@NonNull 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.static @NonNull CorsAuthorizerAcquires a threadsafeCorsAuthorizerconfigured to reject all cross-domain requests regardless ofOrigin.
-
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<@NonNull HttpMethod, @NonNull 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
-
acceptAllInstance
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
fromWhitelistedOrigins(Set)orfromWhitelistAuthorizer(Function)for production systems.- Returns:
- a
CorsAuthorizerconfigured to permit all cross-domain requests
-
rejectAllInstance
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
-
fromWhitelistedOrigins
static @NonNull CorsAuthorizer fromWhitelistedOrigins(@NonNull Set<@NonNull String> whitelistedOrigins) 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 viafromWhitelistedOrigins(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
-
fromWhitelistedOrigins
static @NonNull CorsAuthorizer fromWhitelistedOrigins(@NonNull Set<@NonNull 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 viafromWhitelistAuthorizer(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
-
fromWhitelistAuthorizer
static @NonNull CorsAuthorizer fromWhitelistAuthorizer(@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 viafromWhitelistAuthorizer(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
-
fromWhitelistAuthorizer
static @NonNull CorsAuthorizer fromWhitelistAuthorizer(@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
-