Swagger-ui: how to set request headers on Swagger UI

Created on 26 Aug 2015  路  3Comments  路  Source: swagger-api/swagger-ui

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));
    }

}

Most helpful comment

The solution is no longer working.

All 3 comments

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()));
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrecarlucci picture andrecarlucci  路  3Comments

LaysDragon picture LaysDragon  路  3Comments

easyest picture easyest  路  3Comments

songtianyi picture songtianyi  路  3Comments

Deraen picture Deraen  路  4Comments