spring-bootswaggerswagger-uispringdocspringdoc-openapi-ui

How to ask Swagger UI to add bearer token field for each endpoint globally?


Because of the issue described here I am migrating to Springdoc. And now in Swagger UI I don't have a field for bearer token for each endpoint but it is expected because those endpoints are secured. enter image description here

I have the following configuration:

@Bean
public OpenAPI myAPI() {
  return new OpenAPI()
      .components(new Components()
          .addSecuritySchemes("bearer-key",
              new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")))
      .info(new Info()
          .title("MY API")
          .description("Documentation of API v.1.0")
          .version("1.0")
      ).addSecurityItem(
          new SecurityRequirement().addList("bearer-jwt", Arrays.asList("read", "write")));
}

I've found workaround - marking each invidual endpoint with:

@Operation(summary = "some description", security = { @SecurityRequirement(name = "bearer-key") })

But I don't like to do this repeated work.

Is there a way to make it global for each rest endpont?


Solution

  • It works with small diference

    when I used swagger 2 I had to provide token like

    Bearer eyJhbGciOi....

    but now I had to provide it like

    eyJhbGciOi....

    public OpenAPI myAPI() {
            return new OpenAPI()
                    .components(new Components()
                            .addSecuritySchemes("bearer-key",
                                    new SecurityScheme()
                                            .type(SecurityScheme.Type.HTTP)
                                            .scheme("bearer")
                                            .bearerFormat("JWT")
                            )
                    )
                    .info(new Info()
                            .title("My API")
                            .description("Documentation of API v.1.0")
                            .version("1.0")
                    ).addSecurityItem(
                            new SecurityRequirement()
                                    .addList("bearer-jwt", Arrays.asList("read", "write"))
                                    .addList("bearer-key", Collections.emptyList())
                    );
        }