kumuluz / kumuluzee-cors

KumuluzEE CORS extension for Cross-Origin Resource Sharing (CORS) support.
https://ee.kumuluz.com
Other
1 stars 2 forks source link
cloud-native cors java javaee kumuluzee microservice web

KumuluzEE CORS

KumuluzEE CI

KumuluzEE CORS filter extension for the KumuluzEE microservice framework.

KumuluzEE CORS project is CORS filter extension for the KumuluzEE microservice framework. It enables fitting of Cross-Origin Resource Sharing support to Java web applications.

CORS supports:

CORS specification is available at CORS.

Usage

You can enable the KumuluzEE CORS filter by adding the following dependency:

<dependency>
    <groupId>com.kumuluz.ee.cors</groupId>
    <artifactId>kumuluzee-cors</artifactId>
    <version>${kumuluzee-cors.version}</version>
</dependency>

CORS filter configuration

When kumuluzee-cors dependency is included in the project, CORS filter can be configured in two ways: using @CrossOrigin annotation or using servlet filter by providing cors-filter configuration section in preferred config source (i.e. config.yaml, etcd etc.).

@CrossOrigin annotation

Annotation can be put on the following classes of the JAX-RS application:

If @CrossOrigin annotation is provided on the Application class, all resources inherit the Cross Origin configuration. Configuration can be overridden by applying @CrossOrigin annotation on the resource class or method of the resource class.

If @CrossOrigin annotation is not provided on the Application class, only resource classes or methods with @CrossOrigin annotation have CORS Filter enabled.

@CrossOrigin annotation parameters

Configuration for each annotation can be provided through config framework. Configuration for annotations is provided in namespace kumuluzee.cors-filter.annotations.<annotation-name>. Parameter keys are the same as in annotation, except in kebab-case.

Example of annotation configuration in config:

kumuluzee:
  cors-filter:
    annotations:
      my-resource:
        allow-origin: "https://kumuluz.com"
        allow-subdomains: false

Configuration in config takes priority over values defined in CrossOrigin annotation. To load the annotation configuration from config the key of the config must be the same as name parameter if name parameter is provided. If name parameter is not provided in annotation config key must equal to the name of the class, if annotation is used on a class, or equal to the -, if annotation is used on method. If multiple methods share the same name in the same class, annotations must have name provided to distinguish between them.

Example of named CrossOrigin annotation:

@RequestScoped
@Path("/myresource")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@CrossOrigin(name = "my-resource", allowOrigin = "https://my.origin.com")
public class MyResource {
}
Example of using @CrossOrigin annotation

Application class

@ApplicationPath("/v1")
@CrossOrigin
public class MyApplication extends Application {
}

Resource class and method

@RequestScoped
@Path("/myresource")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@CrossOrigin(allowOrigin = "https://my.origin.com")
public class MyResource {
    ...

    @GET
    @Path("/test")
    @CrossOrigin(allowOrigin="https://my-test.origin.com")
    public Response test() {
        ...
    }

CORS servlet filter

CORS servlet filter is enabled by providing cors-filter section in the config source. (!Important: If @CrossOrigin annotation is used in the application, servlet filter will not be initialized, annotations will take priority).

The following configuration parameters can be set for CORS filter:

Example CORS filter configuration (config.yaml):

kumuluzee:
  cors-filter:
    servlet:
      enabled: true
      allow-generic-http-requests: false
      allow-origin: "*"

CORS servlet filter should be used for Servlet applications which don't use @WebServlet annotations. It can also be used for the JAX-RS applications, but no @CrossOrigin annotation should be used.

Remember to restart your web application or server after changing the CORS configuration!

Disabling CORS Servlet filter

To disable CORS servlet filter, simply set kumuluzee.cors-filter.servlet.enabledto false.

Changelog

Recent changes can be viewed on GitHub on the Releases Page

Contribute

See the contributing docs

When submitting an issue, please follow the guidelines.

When submitting a bugfix, write a test that exposes the bug and fails before applying your fix. Submit the test alongside the fix.

When submitting a new feature, add tests that cover the feature.

License

MIT