Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set request headers on Swagger UI #1566

Closed
prabhat1790 opened this issue Aug 26, 2015 · 3 comments
Closed

how to set request headers on Swagger UI #1566

prabhat1790 opened this issue Aug 26, 2015 · 3 comments

Comments

@prabhat1790
Copy link

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

}
@webron
Copy link
Contributor

webron commented Aug 27, 2015

@sunshineo
Copy link

The solution is no longer working.

@dangduytung
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants