I am trying to setup SwaggerUI for exposing my APIs. I require to add authorization token in the request headers while making the API calls, is there a way to configure this on the UI?
My Swagger Configuration file looks like this -
@Configuration
@EnableSwagger2
@ComponentScan(basePackages = "com.rokitt.**")
public class SwaggerConfiguration {
@Bean
public Docket petApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("galactica-api").apiInfo(apiInfo()).paths(apiPaths()).build();
}
private Predicate<String> apiPaths() {
return or(regex("/rest/.*"));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Galactica APIs").description("Backend APIs for Galactica")
.termsOfServiceUrl("http://springfox.io").contact("Backend Team").license("Apache License Version 2.0")
.licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE").version("2.0").build();
}
// these components have been copied from
// http://springfox.github.io/springfox/docs/current/#getting-started
// in an attempt to configure authToken in the header while making the api calls
// but it is still not working
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("/rest/.*")).build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessNothing");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Lists.newArrayList(new SecurityReference("mykey", authorizationScopes));
}
}
The solution is no longer working.
You miss set securityContext to Docket like :
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.vnpt.technology"))
.paths(postPaths()).build().apiInfo(metaData()).securitySchemes(Lists.newArrayList(apiKey()))
.securityContexts(Lists.newArrayList(securityContext()));
}
Most helpful comment
The solution is no longer working.