Skip to content

Instantly share code, notes, and snippets.

@craigbeck
Created July 10, 2014 23:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigbeck/fb71818063175b9b4210 to your computer and use it in GitHub Desktop.
Save craigbeck/fb71818063175b9b4210 to your computer and use it in GitHub Desktop.
Enable cross origin requests (CORS) using the Jersey CrossOriginFilter to Dropwizard 0.7.1
// in run method of your application:
final FilterRegistration.Dynamic cors = environment.servlets().addFilter("crossOriginRequsts", CrossOriginFilter.class);
cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
@Toilal
Copy link

Toilal commented Nov 19, 2014

Hi @craigbeck. I try to migrate to dropwizard 0.8.0, but this doesn't work. Do you know what to change to make it work on latest 0.8.0-rc1 version ?

@adaviding
Copy link

Has anybody found the answer to this question yet? I would like to know.

@mrserverless
Copy link

this works for 0.7, 0.8 and 0.9. I think some people have issues because the Jetty CrossOriginFilter's DEFAULT_ALLOWED_HEADERS doesn't contain Authorization and DEFAULT_ALLOWED_METHODS doesn't contain OPTIONS. So you may need to add them in depending on the error you're getting. I have a longer version here that worked for me

@JamieKalloe
Copy link

This should work, there is a typo in the snippet however, "crossOriginRequsts" should be "crossOriginRequests"

@JimiHFord
Copy link

Worth noting that if you have authentication / authorization filters setup, you'll likely want to configure the filter with this:

cors.setInitParameter(CrossOriginFilter.CHAIN_PREFLIGHT_PARAM, Boolean.FALSE.toString());

This will prevent preflight requests (that don't have authorization headers on them) from getting filtered by your down-stream authentication / authorization filters which would result in a 401~403 instead of a 200.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment