javaspringjwtopenapispringdoc

Enable Authorize button in springdoc-openapi-ui for Bearer Token Authentication (JWT)


How to enable "Authorize" button in springdoc-openapi-ui (OpenAPI 3.0 /swagger-ui.html) for Bearer Token Authentication, for example JWT.

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

Authorize button

Authorize form for Bearer Token Authentication


Solution

  • 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 = "bearerAuth",
        type = SecuritySchemeType.HTTP,
        bearerFormat = "JWT",
        scheme = "bearer"
    )
    public class OpenApi30Config {
    
    }
    

    Annotate each @RestController method requiring Bearer Token Authentication (JWT) with @io.swagger.v3.oas.annotations.Operation referencing the defined security scheme:

    @Operation(summary = "My endpoint", security = @SecurityRequirement(name = "bearerAuth"))