Custom Jetty Filters in Dropwizard

眉间皱痕 提交于 2019-12-06 05:28:56

问题


I'm attempting to add a custom header filter in my Dropwizard instance to check to see if the request's version is synced to the Dropwizard instance's version.

I see you can use FilterBuilder to add jetty CrossOriginFilters. However, I am having trouble figuring out how to set a custom filter.

Thanks


回答1:


Via the Environment class.

https://dropwizard.github.io/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 choose which Dispatch types by changing EnumSet.allOf(DispatcherType.class)




回答2:


This is how I got it to work using Dropwwizard 0.7.1 (APIs appear to have changed from other examples I found out there)

In run method of your application:

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

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



来源:https://stackoverflow.com/questions/19166603/custom-jetty-filters-in-dropwizard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!