CORS middleware and utilities for Amaya Framework. Provides configuration of allowed origins, methods, headers, credentials, and preflight handling.
To install it, you will need:
- Java 11+
- Maven/Gradle
- Amaya Core or set of core modules
- Flexible configuration of allowed origins, including regex support
- Control of allowed HTTP methods and headers
- Support for preflight (OPTIONS) requests
- Automatic handling of CORS response headers,
including
Access-Control-Allow-Credentials
,Access-Control-Max-Age
, andAccess-Control-Expose-Headers
- Fluent builder API for concise configuration
- Resettable configuration for reusability
dependencies {
implementation group: 'io.github.amayaframework', name: 'amaya-core', version: '3.3.0'
implementation group: 'io.github.amayaframework', name: 'amaya-cors', version: '2.1.0'
}
<dependencies>
<dependency>
<groupId>io.github.amayaframework</groupId>
<artifactId>amaya-core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>io.github.amayaframework</groupId>
<artifactId>amaya-cors</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
import io.github.amayaframework.core.WebBuilders;
import io.github.amayaframework.cors.Cors;
import io.github.amayaframework.http.HttpMethod;
public class Main {
public static void main(String[] args) throws Throwable {
var app = WebBuilders.create()
.withServerFactory(/*your server factory here*/)
.configureApplication(Cors.configurer(cfg -> {
cfg.allowedOrigins().allow("http://example.com", "https://another.com");
cfg.allowedMethods().allow(HttpMethod.GET, HttpMethod.POST);
cfg.allowedHeaders().allow("X-Test", "Authorization");
cfg.exposedHeaders().allow("X-Expose");
cfg.allowCredentials(true);
cfg.maxAge(3600);
}))
.build();
app.bind(8080);
app.configurer().add((ctx, next) -> {
ctx.response().writer().println("Hello (now with CORS)");
});
app.run();
}
}
import io.github.amayaframework.core.WebBuilders;
import io.github.amayaframework.cors.Cors;
import io.github.amayaframework.http.HttpMethod;
public class Main {
public static void main(String[] args) throws Throwable {
var app = WebBuilders.create()
.withServerFactory(/*your server factory here*/)
.configureApplication(Cors.configurer(cfg -> {
cfg.allowAny(); // Overrides specific origin/method/header settings
}))
.build();
app.bind(8080);
app.configurer().add((ctx, next) -> {
ctx.response().writer().println("Hello (now with CORS)");
});
app.run();
}
}
- Gradle - Dependency management
- amaya-core - Various amaya modules
- RomanQed - Main work
See also the list of contributors who participated in this project.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details