Class CorsPolicy
java.lang.Object
de.craftsblock.craftsnet.api.http.cors.CorsPolicy
Represents a Cross-Origin Resource Sharing (CORS) policy, which defines how requests
from other origins (domains) can interact with the server's resources.
This class allows fine-grained control over which origins, methods, and headers are allowed
in cross-origin requests, and whether credentials are permitted.
The policy can be customized to either allow or disallow specific origins, http methods, headers, and control headers exposure.
Important: The cors policy is applied to an exchange automatically. You don't need to take further actions!
- Since:
- 3.1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe http header that indicates whether the response to the request can be exposed when the credentials flag is true.static final StringThe http header that defines which custom request headers are allowed during a CORS request.static final StringThe http header that indicates which http methods are permitted when accessing the resource.static final StringThe http header that specifies which origin sites are allowed to access the resource.static final StringThe http header that specifies which headers are safe to expose to the client.static final StringThe http header that indicates how long the results of a preflight request can be cached.static final StringThe http header sent by browsers in preflight requests to indicate which custom headers will be included in the actual request. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAllowedHeader(String... headers) Adds one or more http headers to the list of allowed headers for cross-origin requests.voidaddAllowedMethod(HttpMethod... methods) Adds one or more http methods to the list of allowed methods for cross-origin requests.voidaddAllowedOrigin(String... origins) Adds one or more origins to the list of allowed origins for cross-origin requests.voidaddExposedHeader(String... headers) Adds one or more http headers to the list of headers exposed to the client.voidAllows all http headers for cross-origin requests, meaning any header can be sent from the client.voidAllows all http methods (e.g., GET, POST, DELETE) for cross-origin requests.voidAllows all origins for cross-origin requests.voidAllows credentials (such as cookies or authentication data) in cross-origin requests.voidApplies the current CORS policy to anExchangeobject by adding appropriate headers to the response based on the allowed origins, methods, headers, and credentials.voiddisable()Resets the CORS policy by disabling all settings and restoring it to its default state.voidDisallows all headers, requiring cross-origin requests to specify only permitted headers.voidDisallows all http methods, limiting cross-origin requests to only specific methods.voidDisallows all origins, restricting cross-origin requests to specific origins if set.voidDisallows credentials in cross-origin requests.Retrieves the list of allowed http headers for cross-origin requests.@Unmodifiable List<HttpMethod>Retrieves the list of allowed http methods for cross-origin requests.Retrieves the list of allowed origins for cross-origin requests.intRetrieves the maximum age in seconds for how long preflight request results can be cached by the client.Retrieves the list of exposed headers that the client can access.booleanChecks if all http headers are allowed for cross-origin requests.booleanChecks if all http methods are allowed for cross-origin requests.booleanChecks if all origins are allowed for cross-origin requests.booleanChecks if credentials (such as cookies) are allowed in cross-origin requests.voidremoveAllowedHeader(String... headers) Removes one or more http headers from the list of allowed headers.voidremoveAllowedOrigin(HttpMethod... methods) Removes one or more http methods from the list of allowed methods.voidremoveAllowedOrigin(String... origins) Removes one or more origins from the list of allowed origins.voidremoveExposedHeader(String... headers) Removes one or more headers from the list of exposed headers.voidsetControlMaxAge(@org.jetbrains.annotations.Range(from=-1L, to=2147483647L) int controlMaxAge) Sets the maximum age (in seconds) for how long the results of a preflight request can be cached by the client.voidupdate(CorsPolicy policy) Updates the current CORS policy by copying the settings from another policy.
-
Field Details
-
ALLOW_ORIGIN_HEADER
The http header that specifies which origin sites are allowed to access the resource.- See Also:
-
ALLOW_METHODS_HEADER
The http header that indicates which http methods are permitted when accessing the resource.- See Also:
-
ALLOW_HEADERS_HEADER
The http header that defines which custom request headers are allowed during a CORS request.- See Also:
-
EXPOSE_HEADERS_HEADER
The http header that specifies which headers are safe to expose to the client.- See Also:
-
ALLOW_CREDENTIALS_HEADER
The http header that indicates whether the response to the request can be exposed when the credentials flag is true.- See Also:
-
MAX_AGE_HEADER
The http header that indicates how long the results of a preflight request can be cached.- See Also:
-
REQUEST_HEADERS_HEADER
The http header sent by browsers in preflight requests to indicate which custom headers will be included in the actual request.- See Also:
-
-
Constructor Details
-
CorsPolicy
public CorsPolicy()Creates a new, empty CORS policy. By default, all origins, methods, and headers are disallowed, and credentials are not allowed. Specific settings can be configured using the provided methods.
-
-
Method Details
-
allowAllOrigins
public void allowAllOrigins()Allows all origins for cross-origin requests. Once enabled, any domain can make requests to the server. -
disallowAllOrigins
public void disallowAllOrigins()Disallows all origins, restricting cross-origin requests to specific origins if set. -
allowAllMethods
public void allowAllMethods()Allows all http methods (e.g., GET, POST, DELETE) for cross-origin requests. -
disallowAllMethods
public void disallowAllMethods()Disallows all http methods, limiting cross-origin requests to only specific methods. -
allowAllHeaders
public void allowAllHeaders()Allows all http headers for cross-origin requests, meaning any header can be sent from the client. -
disallowAllHeaders
public void disallowAllHeaders()Disallows all headers, requiring cross-origin requests to specify only permitted headers. -
allowCredentials
public void allowCredentials()Allows credentials (such as cookies or authentication data) in cross-origin requests. -
disallowCredentials
public void disallowCredentials()Disallows credentials in cross-origin requests. -
addAllowedOrigin
Adds one or more origins to the list of allowed origins for cross-origin requests.- Parameters:
origins- The origins to be allowed.
-
removeAllowedOrigin
Removes one or more origins from the list of allowed origins.- Parameters:
origins- The origins to be removed.
-
addAllowedMethod
Adds one or more http methods to the list of allowed methods for cross-origin requests.- Parameters:
methods- The http methods to be allowed.
-
removeAllowedOrigin
Removes one or more http methods from the list of allowed methods.- Parameters:
methods- The http methods to be removed.
-
addAllowedHeader
Adds one or more http headers to the list of allowed headers for cross-origin requests.- Parameters:
headers- The http headers to be allowed.
-
removeAllowedHeader
Removes one or more http headers from the list of allowed headers.- Parameters:
headers- The http headers to be removed.
-
addExposedHeader
Adds one or more http headers to the list of headers exposed to the client. Exposed headers can be accessed by the client application.- Parameters:
headers- The http headers to be exposed.
-
removeExposedHeader
Removes one or more headers from the list of exposed headers.- Parameters:
headers- The headers to be removed.
-
setControlMaxAge
public void setControlMaxAge(@org.jetbrains.annotations.Range(from=-1L, to=2147483647L) int controlMaxAge) Sets the maximum age (in seconds) for how long the results of a preflight request can be cached by the client.- Parameters:
controlMaxAge- The max age in seconds.
-
getAllowedOrigins
Retrieves the list of allowed origins for cross-origin requests.- Returns:
- A list of allowed origins.
-
getAllowedMethods
Retrieves the list of allowed http methods for cross-origin requests.- Returns:
- A list of allowed http methods.
-
getAllowedHeaders
Retrieves the list of allowed http headers for cross-origin requests.- Returns:
- A list of allowed http headers.
-
getExposedHeaders
Retrieves the list of exposed headers that the client can access.- Returns:
- A list of exposed headers.
-
isAllowAllOrigins
public boolean isAllowAllOrigins()Checks if all origins are allowed for cross-origin requests.- Returns:
- true if all origins are allowed, false otherwise.
-
isAllowAllMethods
public boolean isAllowAllMethods()Checks if all http methods are allowed for cross-origin requests.- Returns:
- true if all methods are allowed, false otherwise.
-
isAllowAllHeaders
public boolean isAllowAllHeaders()Checks if all http headers are allowed for cross-origin requests.- Returns:
- true if all headers are allowed, false otherwise.
-
isAllowCredentials
public boolean isAllowCredentials()Checks if credentials (such as cookies) are allowed in cross-origin requests.- Returns:
- true if credentials are allowed, false otherwise.
-
getControlMaxAge
public int getControlMaxAge()Retrieves the maximum age in seconds for how long preflight request results can be cached by the client.- Returns:
- The max age, or -1 if not set.
-
disable
public void disable()Resets the CORS policy by disabling all settings and restoring it to its default state. -
update
Updates the current CORS policy by copying the settings from another policy.- Parameters:
policy- The policy whose settings should be copied.
-
apply
Applies the current CORS policy to anExchangeobject by adding appropriate headers to the response based on the allowed origins, methods, headers, and credentials.- Parameters:
exchange- The exchange on which to apply the CORS policy.
-