Skip to content

AmayaFramework/amaya-cors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

amaya-cors amaya-cors

CORS middleware and utilities for Amaya Framework. Provides configuration of allowed origins, methods, headers, credentials, and preflight handling.

Getting Started

To install it, you will need:

  • Java 11+
  • Maven/Gradle
  • Amaya Core or set of core modules

Features

  • 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, and Access-Control-Expose-Headers
  • Fluent builder API for concise configuration
  • Resettable configuration for reusability

Installing

Gradle dependency

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'
}

Maven dependency

<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>

Examples

Configuring CORS globally

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();
    }
}

Using allowAny to permit all origins

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();
    }
}

Built With

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details

About

CORS middleware and utilities for Amaya Framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages