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?
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.
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.
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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With