All Projects → lomigmegard → Akka Http Cors

lomigmegard / Akka Http Cors

Licence: apache-2.0
Akka Http directives implementing the CORS specifications defined by W3C

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Akka Http Cors

Egg Cors
CORS plugin for egg
Stars: ✭ 140 (-40.17%)
Mutual labels:  cors
Cloudflare Cors Anywhere
CORS "anywhere" proxy in a Cloudflare worker. DEMO at: https://test.cors.workers.dev/
Stars: ✭ 162 (-30.77%)
Mutual labels:  cors
Kafka Streams Scala
Thin Scala wrapper around Kafka Streams Java API
Stars: ✭ 192 (-17.95%)
Mutual labels:  akka-http
Typed Schema
Typelevel http service definition DSL
Stars: ✭ 145 (-38.03%)
Mutual labels:  akka-http
Rust Webapp Starter
Rust single page webapp written in actix-web with vuejs.
Stars: ✭ 151 (-35.47%)
Mutual labels:  cors
Aiohttp Cors
CORS support for aiohttp
Stars: ✭ 173 (-26.07%)
Mutual labels:  cors
Nelmiocorsbundle
The NelmioCorsBundle allows you to send Cross-Origin Resource Sharing headers with ACL-style per-URL configuration.
Stars: ✭ 1,615 (+590.17%)
Mutual labels:  cors
Akka Management
Akka Management is a suite of tools for operating Akka Clusters.
Stars: ✭ 218 (-6.84%)
Mutual labels:  akka-http
Create React Redux App Structure
Create React + Redux app structure with build configurations ✨
Stars: ✭ 161 (-31.2%)
Mutual labels:  cors
Remora
Kafka consumer lag-checking application for monitoring, written in Scala and Akka HTTP; a wrap around the Kafka consumer group command. Integrations with Cloudwatch and Datadog. Authentication recently added
Stars: ✭ 183 (-21.79%)
Mutual labels:  akka-http
Browser Preview
🎢Preview html file in your default browser
Stars: ✭ 148 (-36.75%)
Mutual labels:  cors
Cors Container
A CORS proxy in a container (Docker) for when you need to `Access-Control-Allow-Origin: *`!
Stars: ✭ 150 (-35.9%)
Mutual labels:  cors
Xrcross
XRCross is a Reconstruction, Scanner, and a tool for penetration / BugBounty testing. This tool was built to test (XSS|SSRF|CORS|SSTI|IDOR|RCE|LFI|SQLI) vulnerabilities
Stars: ✭ 175 (-25.21%)
Mutual labels:  cors
Sanic Cors
A Sanic extension for handling Cross Origin Resource Sharing (CORS), making cross-origin AJAX possible. Based on flask-cors by Cory Dolphin.
Stars: ✭ 143 (-38.89%)
Mutual labels:  cors
Web Security Fundamentals
👨‍🏫 Mike's Web Security Course
Stars: ✭ 195 (-16.67%)
Mutual labels:  cors
Apiproject
[https://www.sofineday.com], golang项目开发脚手架,集成最佳实践(gin+gorm+go-redis+mongo+cors+jwt+json日志库zap(支持日志收集到kafka或mongo)+消息队列kafka+微信支付宝支付gopay+api加密+api反向代理+go modules依赖管理+headless爬虫chromedp+makefile+二进制压缩+livereload热加载)
Stars: ✭ 124 (-47.01%)
Mutual labels:  cors
Flusk
Boilerplate API on how to structure big Flask applications (includes SQLAlchemy, Docker, nginx)
Stars: ✭ 165 (-29.49%)
Mutual labels:  cors
Koa2 Cors
CORS middleware for koa2
Stars: ✭ 223 (-4.7%)
Mutual labels:  cors
Express Es6 Rest Api
🔋 Starter project for an ES6 RESTful Express API.
Stars: ✭ 2,401 (+926.07%)
Mutual labels:  cors
Otoroshi
Lightweight api management on top of a modern http reverse proxy
Stars: ✭ 177 (-24.36%)
Mutual labels:  akka-http

akka-http-cors

Build Status Software License Scala Steward badge

CORS (Cross Origin Resource Sharing) is a mechanism to enable cross origin requests.

This is a Scala/Java implementation for the server-side targeting the akka-http library.

Versions

Version Release date Akka Http version Scala versions
1.1.1 2020-12-12 10.2.2 2.12.12, 2.13.3
1.0.0 2020-05-25 10.1.12 2.12.11, 2.13.2
0.4.0 2019-03-09 10.1.7 2.11.12, 2.12.8, 2.13.0-M5
0.3.4 2019-01-17 10.1.7 2.11.12, 2.12.8, 2.13.0-M5
0.3.0 2018-03-24 10.1.0 2.11.12, 2.12.5, 2.13.0-M3
0.2.2 2017-09-25 10.0.10 2.11.11, 2.12.3
0.2.1 2017-04-03 10.0.5 2.11.8, 2.12.1
0.1.11 2017-01-31 10.0.3 2.11.8, 2.12.1
0.1.0 2016-03-20 2.4.2 2.11.8

Some less interesting versions are not listed in the above table. The complete list can be found in the CHANGELOG file.

Getting Akka Http Cors

akka-http-cors is deployed to Maven Central. Add it to your build.sbt or Build.scala:

libraryDependencies += "ch.megard" %% "akka-http-cors" % "1.1.1"

Quick Start

The simplest way to enable CORS in your application is to use the cors directive. Settings are passed as a parameter to the directive, with your overrides loaded from the application.conf.

import ch.megard.akka.http.cors.scaladsl.CorsDirectives._

val route: Route = cors() {
  complete(...)
}

The settings can be updated programmatically too.

val settings = CorsSettings(...).withAllowGenericHttpRequests(false)
val strictRoute: Route = cors(settings) {
  complete(...)
}

A full example, with proper exception and rejection handling, is available in the akka-http-cors-example sub-project.

Rejection

The CORS directives can reject requests using the CorsRejection class. Requests can be either malformed or not allowed to access the resource.

A rejection handler is provided by the library to return meaningful HTTP responses. Read the akka documentation to learn more about rejections, or if you need to write your own handler.

import akka.http.scaladsl.server.directives.ExecutionDirectives._
import ch.megard.akka.http.cors.scaladsl.CorsDirectives._

val route: Route = handleRejections(corsRejectionHandler) {
  cors() {
    complete(...)
  }
}

Java support

Starting from version 0.2.1 Java is supported, mirroring the Scala API. For usage, look at the full Java CorsServer example.

Configuration

Reference configuration.

allowGenericHttpRequests

Boolean with default value true.

If true, allow generic requests (that are outside the scope of the specification) to pass through the directive. Else, strict CORS filtering is applied and any invalid request will be rejected.

allowCredentials

Boolean with default value true.

Indicates whether the resource supports user credentials. If true, the header Access-Control-Allow-Credentials is set in the response, indicating the actual request can include user credentials.

Examples of user credentials are: cookies, HTTP authentication or client-side certificates.

allowedOrigins

HttpOriginMatcher with default value HttpOriginMatcher.*.

List of origins that the CORS filter must allow. Can also be set to * to allow access to the resource from any origin. Controls the content of the Access-Control-Allow-Origin response header:

  • if parameter is * and credentials are not allowed, a * is set in Access-Control-Allow-Origin.
  • otherwise, the origins given in the Origin request header are echoed.

Hostname starting with *. will match any sub-domain. The scheme and the port are always strictly matched.

The actual or preflight request is rejected if any of the origins from the request is not allowed.

allowedHeaders

HttpHeaderRange with default value HttpHeaderRange.*.

List of request headers that can be used when making an actual request. Controls the content of the Access-Control-Allow-Headers header in a preflight response:

  • if parameter is *, the headers from Access-Control-Request-Headers are echoed.
  • otherwise the parameter list is returned as part of the header.

allowedMethods

Seq[HttpMethod] with default value Seq(GET, POST, HEAD, OPTIONS).

List of methods that can be used when making an actual request. The list is returned as part of the Access-Control-Allow-Methods preflight response header.

The preflight request will be rejected if the Access-Control-Request-Method header's method is not part of the list.

exposedHeaders

Seq[String] with default value Seq.empty.

List of headers (other than simple response headers) that browsers are allowed to access. If not empty, this list is returned as part of the Access-Control-Expose-Headers header in the actual response.

maxAge

Option[Long] (in seconds) with default value Some (30 * 60).

When set, the amount of seconds the browser is allowed to cache the results of a preflight request. This value is returned as part of the Access-Control-Max-Age preflight response header. If None, the header is not added to the preflight response.

Benchmarks

Using the sbt-jmh plugin, preliminary benchmarks have been performed to measure the impact of the cors directive on the performance. The first results are shown below.

Results are not all coming from the same machine.

v0.1.2 (Akka 2.4.4)

> jmh:run -i 40 -wi 30 -f2 -t1
Benchmark                         Mode  Cnt     Score     Error  Units
CorsBenchmark.baseline           thrpt   80  3601.121 ± 102.274  ops/s
CorsBenchmark.default_cors       thrpt   80  3582.090 ±  95.304  ops/s
CorsBenchmark.default_preflight  thrpt   80  3482.716 ±  89.124  ops/s

v0.1.3 (Akka 2.4.7)

> jmh:run -i 40 -wi 30 -f2 -t1
Benchmark                         Mode  Cnt     Score     Error  Units
CorsBenchmark.baseline           thrpt   80  3657.762 ± 141.409  ops/s
CorsBenchmark.default_cors       thrpt   80  3687.351 ±  35.176  ops/s
CorsBenchmark.default_preflight  thrpt   80  3645.629 ±  30.411  ops/s

v0.2.2 (Akka HTTP 10.0.6)

> jmh:run -i 40 -wi 30 -f2 -t1
Benchmark                         Mode  Cnt     Score     Error  Units
CorsBenchmark.baseline           thrpt   80  9730.001 ±  25.281  ops/s
CorsBenchmark.default_cors       thrpt   80  9159.320 ±  25.459  ops/s
CorsBenchmark.default_preflight  thrpt   80  9172.938 ±  26.794  ops/s

References

License

This code is open source software licensed under the Apache 2.0 License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].