rcde0
rcde0

Reputation: 4460

Adding Authorization in requests made using Swagger UI

I have been trying to add authorization in requests that I try from swagger-ui, but in the request, the authorization header is always coming as null.

This is what I have done.

  private ApiKey apiKey() {
    return new ApiKey("apiKey", "Authorization",
        "header"); //`apiKey` is the name of the APIKey, `Authorization` is the key in the request header
  }

  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select().apis(RequestHandlerSelectors.basePackage("com.example.app"))
        .paths(PathSelectors.any()).build().apiInfo(apiInfo()).securitySchemes(Arrays.asList(apiKey()));
  }

Can anyone please give some pointers? Thanks.

Upvotes: 1

Views: 194

Answers (1)

preetham
preetham

Reputation: 828

You can try this SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
            .build().securitySchemes(Lists.newArrayList(apiKey()));

}

private ApiKey apiKey() {
    return new ApiKey("AUTHORIZATION", "api_key", "header");
}
}

Upvotes: 1

Related Questions