Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Authorize button in springdoc-openapi-ui for Basic Authentication

How to enable "Authorize" button in springdoc-openapi-ui (OpenAPI 3.0 /swagger-ui.html) for Basic Authentication.

What annotations have to be added to Spring @Controller and @Configuration classes?

Authorize button

Authorize form for Basic Authentication

like image 696
Evgeniy Khyst Avatar asked Jan 24 '20 14:01

Evgeniy Khyst


People also ask

How do I enable the swagger authorize button?

In the Swagger Editor (the right pane), click the Authorize button, paste the sample API key shown in the description into the Value field (or use your own OpenWeatherMap API key), and click Authorize. Then click Close to close the authorization modal.

How do I enable basic authentication in swagger UI?

Basic authentication is easy to define. In the global securityDefinitions section, add an entry with type: basic and an arbitrary name (in this example - basicAuth). Then, apply security to the whole API or specific operations by using the security section.


1 Answers

Define a global security scheme for OpenAPI 3.0 using annotation @io.swagger.v3.oas.annotations.security.SecurityScheme in a @Configuration bean:

@Configuration
@OpenAPIDefinition(info = @Info(title = "My API", version = "v1"))
@SecurityScheme(
    name = "basicAuth",
    type = SecuritySchemeType.HTTP,
    scheme = "basic"
)
public class OpenApi30Config {

}

Annotate each @RestController method requiring Basic Authentication with @io.swagger.v3.oas.annotations.Operation referencing the defined security scheme:

@Operation(summary = "My endpoint", security = @SecurityRequirement(name = "basicAuth"))
like image 84
Evgeniy Khyst Avatar answered Oct 22 '22 13:10

Evgeniy Khyst