1
我試圖讓一個dropwizard 0.7.0(澤西島)REST服務發送HTML CORS頭像「訪問控制允許來源」。我發現了各種教程,並瞭解瞭如何在0.7.0和以前的版本中做到這一點。最有希望的方式似乎是described in the dropwizard group,但是當我使用給定的代碼時,不會發送任何頭文件。該過濾器似乎沒有工作。當我使用自定義過濾器時,它正在被初始化,但不被使用。到目前爲止,在日誌中沒有什麼異常。使用帶有dropwizard的CORS頭過濾器0.7.0
這是我(最新)運行方法:
@Override
public void run(BackendConfiguration configuration, Environment environment) throws Exception {
// XXX why doesn't this work?
FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORS", CrossOriginFilter.class);
filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
filter.setInitParameter("allowedOrigins", "*"); // allowed origins comma separated
filter.setInitParameter("allowedHeaders", "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
filter.setInitParameter("allowedMethods", "GET,PUT,POST,DELETE,OPTIONS,HEAD");
filter.setInitParameter("preflightMaxAge", "5184000"); // 2 months
filter.setInitParameter("allowCredentials", "true");
final DBIFactory factory = new DBIFactory();
final DBI jdbi = factory.build(environment, configuration.getDataSourceFactory(), "h2");
environment.jersey().register(new UserResource(jdbi));
}
有什麼我做錯了嗎?
非常感謝!在[閱讀](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)後,我想我還理解了這個概念:) – Tilman