spring - How do I enable CORS headers in the Swagger /v2/api-docs offered by Springfox Swagger? -
i have following file in project:
@configuration @order(ordered.lowest_precedence) public class swaggerconfig { @bean public docket apiswagger2documentation() { .... } }
and in application.java there is:
@springbootapplication @componentscan(basepackages = { ... }) @enableswagger2 public class application { ... }
the swagger json available under /v2/api-docs
, works fine.
what do, enable cors headers endpoint.
for own controllers, have added @crossorigin
controller classes, apis have cors headers, works fine. swagger json url haven't written controller myself, cannot use annotation.
i have added following method swaggerconfig
, described in "global cors configuration" in cors support in spring framework.
@bean public webmvcconfigurer corsconfigurer() { system.out.println("*** corsconfigurer called"); return new webmvcconfigureradapter() { @override public void addcorsmappings(corsregistry registry) { system.out.println("*** addcorsmappings called"); registry.addmapping("/v2/api-docs"); } }; }
both print statements printed, method being called. when call url curl:
curl -h "origin: foo.com" \ -h "access-control-request-method: get" \ -x options \ --verbose \ http://localhost:9274/v2/api-docs
the cors headers not in response. (in contrast own controller methods, annotated @crossorigin
, response have cors headers.)
i using springfox-swagger2 version 2.7.0, , spring-boot-starter-web 1.5.2.
what can enable cors headers on swagger json api endpoint?
i think need generic web filter opposed web mvc configuration.
@bean public corsfilter corsfilter() { urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource(); // allow , access. ok swagger spec corsconfiguration config = new corsconfiguration(); config.setallowcredentials(true); config.addallowedorigin("*"); config.addallowedheader("*"); config.addallowedmethod("*"); source.registercorsconfiguration("/v2/api-docs", config); return new corsfilter(source); }
Comments
Post a Comment