public class Cors extends Object
Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources (e.g. fonts, JavaScript, etc.) on a web page to be requested from another domain outside the domain from which the resource originated.
This class represent the available options for configure CORS in Jooby.
{
use("*", new CorsHandler(new Cors()));
}
Previous example, adds a cors filter using the default cors options.
| Constructor and Description |
|---|
Cors()
Creates default Cors.
|
Cors(com.typesafe.config.Config config)
Creates Cors options from Config:
|
| Modifier and Type | Method and Description |
|---|---|
List<String> |
allowedHeaders() |
List<String> |
allowedMethods() |
boolean |
allowHeader(String header) |
boolean |
allowHeaders(List<String> headers)
True if all the headers are allowed.
|
boolean |
allowHeaders(String... headers)
True if all the headers are allowed.
|
boolean |
allowMethod(String method)
True if the method is allowed.
|
boolean |
allowOrigin(String origin)
Test if the given origin is allowed or not.
|
boolean |
anyHeader() |
boolean |
anyOrigin() |
boolean |
credentials()
If true, set the Access-Control-Allow-Credentials header.
|
Cors |
disabled()
Disabled cors (enabled = false).
|
boolean |
enabled() |
List<String> |
exposedHeaders() |
int |
maxAge() |
List<String> |
origin()
An origin must be a "*" (any origin), a domain name (like, http://foo.com) and/or a regex (like, http://*.domain.com).
|
Cors |
withExposedHeaders(List<String> exposedHeaders)
Set the list of exposed headers.
|
Cors |
withExposedHeaders(String... exposedHeaders)
Set the list of exposed headers.
|
Cors |
withHeaders(List<String> headers)
Set one or more allowed headers.
|
Cors |
withHeaders(String... headers)
Set one or more allowed headers.
|
Cors |
withMaxAge(int preflightMaxAge)
Set the preflight max age header.
|
Cors |
withMethods(List<String> methods)
Set one or more allowed methods.
|
Cors |
withMethods(String... methods)
Set one or more allowed methods.
|
Cors |
withOrigin(List<String> origin)
Set the allowed origins.
|
Cors |
withOrigin(String... origin)
Set the allowed origins.
|
Cors |
withoutCreds()
Set credentials() to false.
|
@Inject
public Cors(@Named(value="cors")
com.typesafe.config.Config config)
Cors options from Config:
origin: "*" credentials: true allowedMethods: [GET, POST] allowedHeaders: [X-Requested-With, Content-Type, Accept, Origin] exposedHeaders: []
config - Config to use.
public Cors()
Cors. Default options are:
origin: "*" credentials: true allowedMethods: [GET, POST] allowedHeaders: [X-Requested-With, Content-Type, Accept, Origin] exposedHeaders: []
public Cors withoutCreds()
credentials() to false.
public boolean enabled()
cors.enabled property. Default is: true.
public Cors disabled()
public boolean credentials()
Access-Control-Allow-Credentials header. Controlled by: cors.credentials property. Default is: true
Access-Control-Allow-Credentials header must be set.
public boolean anyOrigin()
public List<String> origin()
*
public boolean allowOrigin(String origin)
origin - The origin to test.
public Cors withOrigin(String... origin)
origin - One ore more origin.
public Cors withOrigin(List<String> origin)
origin - One ore more origin.
public boolean allowMethod(String method)
method - Method to test.
public Cors withMethods(String... methods)
methods - One or more method.
public Cors withMethods(List<String> methods)
methods - One or more method.
public boolean anyHeader()
*.
public boolean allowHeader(String header)
header - A header to test.
public boolean allowHeaders(String... headers)
headers - Headers to test.
public boolean allowHeaders(List<String> headers)
headers - Headers to test.
public List<String> allowedHeaders()
X-Requested-With, Content-Type, Accept and Origin.
public Cors withHeaders(String... headers)
* if any header is allowed.
headers - Headers to set.
public Cors withHeaders(List<String> headers)
* if any header is allowed.
headers - Headers to set.
public Cors withExposedHeaders(String... exposedHeaders)
exposedHeaders - Headers to expose.
public Cors withExposedHeaders(List<String> exposedHeaders)
exposedHeaders - Headers to expose.
public int maxAge()
public Cors withMaxAge(int preflightMaxAge)
preflightMaxAge - Number of seconds or -1 to turn this off.
Copyright © 2019. All rights reserved.