Skip to content
This repository was archived by the owner on Apr 30, 2019. It is now read-only.

Commit b759a18

Browse files
committed
Sets up CORS for the service.
1 parent 9f8de2f commit b759a18

5 files changed

Lines changed: 64 additions & 1 deletion

File tree

cors/cors-java/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
11
# CORS recipe for Lagom's Javadsl
22

3+
In order to enable CORS on a Lagom service the following steps are required:
4+
5+
1. include `filters` as a dependency on your `-impl` project. `filters` is a package provided by Play Framework.
6+
2. Create a class that implements `DefaultHttpFilters` and inject Play's `CORSFilter`
7+
3. Register that newly created class on your `application.conf` using: `play.http.filters = "com.your.package.YourFilterClassName"`
8+
4. Finally, add an ACL on your `Service.Descriptor` matching the `OPTIONS` method for the paths you are exposing on your Service Gateway.
9+
10+
## Testing the recipe
11+
12+
13+
You can test this recipe using 2 separate terminals.
14+
15+
On one terminal start the service:
16+
17+
```
18+
sbt runAll
19+
```
20+
21+
On a separate terminal, use `curl` to trigger a pre-flight request:
322

423
```
524
curl -H "Access-Control-Request-Method: GET" \
625
-H "Access-Control-Request-Headers: origin, x-requested-with" \
726
-H "Origin: http://www.some-domain.com" \
827
-X OPTIONS http://localhost:9000/api/hello/123 -v
928
```
29+
30+
Note how the request uses the `OPTIONS` method and targets the [Lagom Service Gateway](https://www.lagomframework.com/documentation/1.3.x/java/ServiceLocator.html) (`localhost:9000`).
31+
32+
## More resources
33+
34+
This topic has been discussed in the Lagom Mailing List [a](https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/lagom-framework/_3Hjvp18NNU/ygu8Pa5wAQAJ) [few](https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/lagom-framework/7YZccqRUS4g/HNMykAiGBAAJ) [times](https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/lagom-framework/3y0wgIMillE/ItT1rPDfBgAJ), if this recipe doesn't resolve your doubts, feel free to ask for help in the community.
35+
36+
You will also find the [Play Framework documentation](https://playframework.com/documentation/2.5.x/CorsFilter) on the topic quite useful.

cors/cors-java/build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ lazy val `cors-java-impl` = (project in file("cors-java-impl"))
1818
.enablePlugins(LagomJava)
1919
.settings(
2020
libraryDependencies ++= Seq(
21+
filters
2122
)
2223
)
2324
.dependsOn(`cors-java-api`)

cors/cors-java/cors-java-api/src/main/java/com/lightbend/lagom/recipes/corsjava/api/CorsjavaService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import akka.NotUsed;
77
import com.lightbend.lagom.javadsl.api.Descriptor;
88
import com.lightbend.lagom.javadsl.api.Service;
9+
import com.lightbend.lagom.javadsl.api.ServiceAcl;
910
import com.lightbend.lagom.javadsl.api.ServiceCall;
11+
import com.lightbend.lagom.javadsl.api.transport.Method;
1012

1113
import static com.lightbend.lagom.javadsl.api.Service.named;
1214
import static com.lightbend.lagom.javadsl.api.Service.pathCall;
@@ -22,7 +24,11 @@ public interface CorsjavaService extends Service {
2224
default Descriptor descriptor() {
2325
return named("corsjava").withCalls(
2426
pathCall("/api/hello/:id", this::hello)
25-
).withAutoAcl(true);
27+
).withAutoAcl(
28+
true
29+
).withServiceAcls(
30+
ServiceAcl.methodAndPath(Method.OPTIONS, "/api/hello/.*")
31+
);
2632
}
2733

2834
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.lightbend.lagom.recipes.corsjava.impl;
2+
3+
import play.filters.cors.CORSFilter;
4+
import play.http.DefaultHttpFilters;
5+
6+
import javax.inject.Inject;
7+
8+
// See https://playframework.com/documentation/2.5.x/CorsFilter
9+
public class MyCORSFilter extends DefaultHttpFilters {
10+
@Inject
11+
public MyCORSFilter(CORSFilter corsFilter) {
12+
super(corsFilter);
13+
}
14+
}

cors/cors-java/cors-java-impl/src/main/resources/application.conf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,18 @@
44
play.crypto.secret=whatever
55
play.modules.enabled += com.lightbend.lagom.recipes.corsjava.impl.CorsjavaModule
66

7+
play.http.filters = "com.lightbend.lagom.recipes.corsjava.impl.MyCORSFilter"
8+
9+
// To properly setup the CORSFilter, please refer to https://playframework.com/documentation/2.5.x/CorsFilter
10+
// This example is only meant to show what's required for Lagom to use CORS.
11+
play.filters.cors {
12+
// review the values of all these settings to fulfill your needs. These values are not meant for production.
13+
pathPrefixes = ["/api"]
14+
allowedOrigins = null
15+
allowedHttpMethods = null
16+
allowedHttpHeaders = null
17+
exposedHeaders = []
18+
supportsCredentials = false
19+
preflightMaxAge = 6 hour
20+
}
21+

0 commit comments

Comments
 (0)