In order to enable CORS on a Lagom service the following steps are required:
- Include
filtersas a dependency on your-implproject.filtersis a package provided by Play Framework. - Mix-in the
CORSComponentson yourApplication. - Also on you
Application, override thelazy val httpFilters(if you aren't overriding it already) and include thecorsFilteron the sequence of filters. - Finally, add an ACL on your
Service.Descriptormatching theOPTIONSmethod for the paths you are exposing on your Service Gateway.
You can test this recipe using 2 separate terminals.
On one terminal start the service:
sbt runAll
On a separate terminal, use curl to trigger a pre-flight request:
curl -H "Access-Control-Request-Method: GET" \
-H "Access-Control-Request-Headers: origin, x-requested-with" \
-H "Origin: http://www.some-domain.com" \
-X OPTIONS http://localhost:9000/api/hello/123 -v
Note how the request uses the OPTIONS method and targets the Lagom Service Gateway (localhost:9000).
This topic has been discussed in the Lagom Mailing List a few times, if this recipe doesn't resolve your doubts, feel free to ask for help in the community.
You will also find the Play Framework documentation on the topic quite useful.