Patriicya Patriicya - 3 months ago 65
Java Question

CORS Play Framework

I'm developing an Angular 2 application on top of a Play Framework 2.5 (Java) back-end. If I access my endpoints through browser URL, they work fine. However, calling it from the Angular 2 application shows the error:


XMLHttpRequest cannot load localhost:9000/app/myendpoint. No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'localhost:3000' is therefore not allowed
access. The response had HTTP status code 500.


I have tryed to follow the documentation and create the filter:

In application.conf:

play.filters {
cors {
pathPrefixes = ["/app"]
allowedOrigins = null
allowedHttpMethods = null
}


Filter:

import play.mvc.*;
import play.api.http.DefaultHttpFilters; <- error with this import
import play.filters.cors.CORSFilter;

public class Filters extends DefaultHttpFilters {
@Inject public Filters(CORSFilter corsFilter) {
super(corsFilter);
}
}


In build.sbt:

libraryDependencies ++= Seq(
...,
filters
)


Does anyone knows the true solution? Thank you :)

Working code:

Filters.java (on project's root)

import com.google.inject.Inject;
import com.google.inject.Singleton;
import play.http.HttpFilters;
import play.mvc.EssentialAction;
import play.mvc.EssentialFilter;
import play.filters.cors.CORSFilter;

@Singleton
public class Filters extends EssentialFilter implements HttpFilters {

@Inject
private CORSFilter corsFilter;

@Override
public EssentialAction apply(EssentialAction next) {
return corsFilter.asJava().apply(next);
}

@Override
public EssentialFilter[] filters() {
EssentialFilter[] result = new EssentialFilter[1];
result[0] = this;
return result;
}
}


In application.conf:

#filters += Filters <- yes, it is commented since my Filters is in project's root
libraryDependencies += filters

play.filters {
cors{
# allow all paths
pathPrefixes = ["/"]
# allow all origins
allowedOrigins = null
allowedHttpMethods = ["GET", "POST", "PUT", "DELETE"]
# allow all headers
allowedHttpHeaders = null
} ...


In build.sbt:

libraryDependencies ++= Seq(
...,
filters
)


Thank you all! :)

Nio Nio
Answer

I remember having the same import issue since I think there were some repackaging changes for Filters in Play somewhere around 2.5.0. And DefaultHttpFilters was replaced by HttpFilters during that refactoring.

Check the source code for the correct path to HttpFilters. Choose the correct tag for the version of Play you are using and then search down the source tree until you find HttpFilters and check the package. This is the path for the Java API for v 2.5.0.

From your post it seems you are using the Java API but are using the import for the Scala version.

The imports for Play 2.5.0 are:-

Scala - import play.api.http.HttpFilters
Java - import play.http.HttpFilters

I'm using Play 2.5.0 (Scala though - not Java) and this import is working fine for me:-

import play.api.http.HttpFilters

From what you have posted I think you should try changing your problematic import to:-

import play.http.HttpFilters

Also, (as others have stated) I assume you have configured the package for your Filters.java file if you have not placed it in the root package.

This is the entry in my application.conf since I am using package filters rather than the default package.:-

play.http.filters = "filters.Filters"