Jersey addServlet 0.7

886 views
Skip to first unread message

Bryan Pham

unread,
Sep 11, 2013, 3:52:10 AM9/11/13
to dropwiz...@googlegroups.com
I'm trying to add CORS support.  Does anybody know how to add filter into jersey in the environment setting with the latest dropwizard ?  

I know we can add it to servlet by doing the following :

environment.servlets().addFilter("/*", CrossOriginFilter.class);

but environment.jersey() doesn't seem to expose a way to add a filter.  

Matt Veitas

unread,
Sep 11, 2013, 8:12:26 AM9/11/13
to dropwizard-user
Example of adding both a filter and a servlet using 0.7.0

final FilterRegistration.Dynamic builder = environment.servlets().addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
builder.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");

// Add the Dispatcher Servlet to enable the MVC request mapping for the Spring Security
DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
final ServletRegistration.Dynamic servletBuilder = environment.servlets().addServlet("dispatchServlet", dispatcherServlet);
servletBuilder.addMapping("/auth/*");


--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jeremy Levy

unread,
Sep 11, 2013, 11:14:46 AM9/11/13
to dropwiz...@googlegroups.com
Here is specifically how we do it:


  /* Allow CORS */
        FilterBuilder filterConfig = environment.addFilter(CrossOriginFilter.class, "/*");
        filterConfig.setInitParam(CrossOriginFilter.PREFLIGHT_MAX_AGE_PARAM, String.valueOf(60 * 60 * 24)); // 1 day - jetty-servlet CrossOriginFilter will convert to Int.
        filterConfig.setInitParam(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, configuration.getAllowedOrigins()); // comma separated list of allowed origin domains
        filterConfig.setInitParam(CrossOriginFilter.ALLOWED_METHODS_PARAM, "PUT,GET,POST,HEAD,DELETE"); // comma separated list
        filterConfig.setInitParam(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin,Authorization"); // comma separated list

--
_______________________________________________
Jeremy Levy | Chief Technology Officer
MeetMoi

http://www.meetmoi.com
http://www.twitter.com/meetmoi
http://www.facebook.com/meetmoi

Ryan Pendergast

unread,
Sep 12, 2013, 12:20:41 PM9/12/13
to dropwiz...@googlegroups.com
Jeremy he is asking for 0.7.0, your example is DW 0.6.

Matt Veitas

unread,
Sep 12, 2013, 12:21:57 PM9/12/13
to dropwiz...@googlegroups.com
See my reply below on the pattern for adding servers and filters with 0.7.0

Sent from my iPhone

Jeremy McJunkin

unread,
Sep 12, 2013, 12:26:55 PM9/12/13
to dropwiz...@googlegroups.com
Thanks Matt,

This is how we added CORS support in 0.7.0

Dynamic filter = environment.servlets().addFilter("CORS", CrossOriginFilter.class);
    filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
    filter.setInitParameter("allowedOrigins", "*");
    filter.setInitParameter("allowedHeaders", "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
    filter.setInitParameter("allowedMethods", "GET,PUT,POST,DELETE,OPTIONS");
    filter.setInitParameter("preflightMaxAge", "5184000"); // 2 months
    filter.setInitParameter("allowCredentials", "true");

Bryan Pham

unread,
Sep 13, 2013, 12:28:04 AM9/13/13
to dropwiz...@googlegroups.com
Thanks guys.  Working great kudos !
Reply all
Reply to author
Forward
0 new messages