Custom Jetty filters in Dropwizard

I am trying to add my own header filter to my Dropwizard instance to check if the version of the request is synchronized with the version of the Dropwizard instance.

I see you can use FilterBuilderto add jetty CrossOriginFilters. However, it’s hard for me to figure out how to set up a custom filter.

thanks

+4
source share
2 answers

Through the environment class.

https://dropwizard.imtqy.com/dropwizard/manual/core.html#environments

@Override
public void run(MyApplicationConfiguration configuration, Environment environment) {
    environment.servlets().addFilter("Custom-Filter-Name", new MyCustomFilter()).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}

You can select sending types by changing EnumSet.allOf(DispatcherType.class)

+11
source

This is how I got it to work with Droplizard 0.7.1 (the API seems to have changed from the other examples I found there)

:

final FilterRegistration.Dynamic cors = environment.servlets().addFilter("crossOriginRequsts", CrossOriginFilter.class);
cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");

https://gist.github.com/craigbeck/fb71818063175b9b4210

+7

All Articles