Java – play framework 2.3 – CORS title
Updated play 2.5 to provide a new CORS filter
With the completion of the migration of the response class to promise class in the new 2.3 java version, the following code is no longer valid
public class CorsAction extends Action.Simple { public Result call(Context context) throws Throwable{ Response response = context.response(); response.setHeader("Access-Control-Allow-Origin","*"); //Handle preflight requests if(context.request().method().equals("OPTIONS")) { response.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS,PUT,DELETE"); response.setHeader("Access-Control-Max-Age","3600"); response.setHeader("Access-Control-Allow-Headers","Origin,X-Requested-With,Content- Type,Accept,Authorization,X-Auth-Token"); response.setHeader("Access-Control-Allow-Credentials","true"); response.setHeader("Access-Control-Allow-Origin","*"); return ok() } response.setHeader("Access-Control-Allow-Headers","X-Requested-With,Content-Type,X- Auth-Token"); return delegate.call(context); } }
I am developing an application in play (Java) 2.3. I have reviewed and tried different methods to enable CORS - including adding / options to the routing file - but failed
I am very grateful to the new response implementation for how to handle this type of interception, because it does not seem to have any impact on the header file when implemented in the new promise class
Thank you for all your help!!
Solution
Solved this problem:
All API responses from the server should contain headers: "access control allow origin", "*" We need to write a wrapper for all action responses
At global In Java
import java.net.URL; import play.*; import play.libs.F.Promise; import play.mvc.Action; import play.mvc.Http; import play.mvc.Result; public class Global extends GlobalSettings { // For CORS private class ActionWrapper extends Action.Simple { public ActionWrapper(Action<?> action) { this.delegate = action; } @Override public Promise<Result> call(Http.Context ctx) throws java.lang.Throwable { Promise<Result> result = this.delegate.call(ctx); Http.Response response = ctx.response(); response.setHeader("Access-Control-Allow-Origin","*"); return result; } } @Override public Action<?> onRequest(Http.Request request,java.lang.reflect.Method actionMethod) { return new ActionWrapper(super.onRequest(request,actionMethod)); } }
Post, put and other server requests send a pre check request to the server before the main request The response to these pre inspection requests shall contain the following headings:
"Access control allow origin", "allow", "access control allow - Method", "post, delete, options" "access control allow headers", "origin, referer, user agent"
Add to alignment:
OPTIONS /*all controllers.Application.preflight(all)
In application coltroller:
package controllers; import play.mvc.*; public class Application extends Controller { public static Result preflight(String all) { response().setHeader("Access-Control-Allow-Origin","*"); response().setHeader("Allow","*"); response().setHeader("Access-Control-Allow-Methods",OPTIONS"); response().setHeader("Access-Control-Allow-Headers",User-Agent"); return ok(); } }
PS: in this way, I don't have to create a Scala filter for this