Spring

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

기록만이살길 2022. 11. 13. 20:02
반응형

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

1. 질문(문제점):

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

2. 해결방안:

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"))
반응형