Search code examples
playframeworkcorsplayframework-2.4

Play CORS approaches


I need to implement CORS filter in a Play application and searching the internet I saw that there are two approaches: 1. Configuring CORS filters in the application.conf file and creating a Filters.class 2. Creating a Global.class that ads the headers to the request as well as having a preflight() method in my main Controller.

What I could not find is advantages of one offer the other and when should I use one method and when the other. Any insights on this?


Solution

  • Everything in the Global singleton is deprecated in Play 2.4.

    The new way to add filters is to define a class extending HttpFilters, it will be loaded by guice runtime injection..

    If you want to enabled the built-in CORS filter, just follow the steps described here https://www.playframework.com/documentation/2.4.x/CorsFilter.

    If you use compile-time DI, you can set the filters in your application loader.