java - Enabling cors in dropwizard not working -
i'm working on dropwizard application , js ui interacte api. need load json data update views have enable cors in dropwizard before that. did staff seems not working because dropwizard returns allways 204 no content.
@override public void run(final bgconfiguration configuration, final environment environment) throws exception { final map<string, string> params = new hashmap<>(); params.put("access-control-allow-origin", "/*"); params.put("access-control-allow-credentials", "true"); params.put("access-control-expose-headers", "true"); params.put("access-control-allow-headers", "content-type, x-requested-with"); params.put("access-control-allow-methods", "get, post, put, delete, options"); environment.servlets().addfilter("cors", crossoriginfilter.class).setinitparameters(params); }
the bug here filter hasn't been configured url path via addmappingforurlpatterns
method.
this worked me using dropwizard 0.7.1:
import org.eclipse.jetty.servlets.crossoriginfilter; import javax.servlet.dispatchertype; import java.util.enumset; public void run(configuration conf, environment environment) { // enable cors headers final filterregistration.dynamic cors = environment.servlets().addfilter("cors", crossoriginfilter.class); // configure cors parameters cors.setinitparameter("allowedorigins", "*"); cors.setinitparameter("allowedheaders", "x-requested-with,content-type,accept,origin"); cors.setinitparameter("allowedmethods", "options,get,put,post,delete,head"); // add url mapping cors.addmappingforurlpatterns(enumset.allof(dispatchertype.class), true, "/*"); }
i'm assuming you're testing live in browser, can verify via cli curl command this:
$ curl -h "origin: http://example.com" \ -h "access-control-request-method: post" \ -h "access-control-request-headers: x-requested-with" \ -x options --verbose \ http://localhost:8080
you should see bunch of access-control-*
http headers in response.
Comments
Post a Comment