Sub topic 11
Documenting bearer token authentication
This chapter assumes you have a Spring Boot 3 project with the springdoc starter, a working @RestController, and a basic idea of how JWT bearer tokens are sent in an Authorization header. It covers how to describe that scheme in the OpenAPI document so Swagger UI can send authenticated requests.
What documenting security does
Documenting security does not protect anything. Spring Security does that. The OpenAPI document only describes how clients authenticate. Swagger UI reads that description and shows an Authorize button. After you paste a token, it adds Authorization: Bearer <token> to the requests it sends.
Two parts are needed:
- A security scheme, which declares a named way to authenticate.
- A security requirement, which says which operations use that scheme.
Declaring the scheme
@SecurityScheme declares a scheme once, under a name. Put it on any configuration class.
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import org.springframework.context.annotation.Configuration;
@Configuration
@OpenAPIDefinition(info = @Info(title = "Library API", version = "1.0.0"))
@SecurityScheme(
name = "bearerAuth",
type = SecuritySchemeType.HTTP,
scheme = "bearer",
bearerFormat = "JWT")
public class OpenApiSecurityConfig {
}The name is the key that requirements refer to. type = HTTP with scheme = "bearer" means the token goes in the Authorization header. bearerFormat is a hint for readers. It does not make the document validate anything.
Applying the requirement
Declaring the scheme alone does not mark any endpoint as secured. Reference it with @SecurityRequirement. Put it on a controller to cover all its operations, or on one method.
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/orders")
@SecurityRequirement(name = "bearerAuth")
public class OrderController {
@GetMapping
public String listOrders() {
return "orders";
}
}The name must match the scheme name exactly. A mismatch such as bearerauth produces no error, but the lock icon appears with no usable scheme behind it.
Applying it to the whole API
If nearly every endpoint needs a token, repeating @SecurityRequirement is tedious. You can put it on the @OpenAPIDefinition as a global requirement:
@OpenAPIDefinition(
info = @Info(title = "Library API", version = "1.0.0"),
security = @SecurityRequirement(name = "bearerAuth"))Public endpoints can then opt out with an empty @Operation(security = {}).
Common errors
| Symptom | Likely cause |
|---|---|
| No Authorize button | `@SecurityScheme` is missing or not on a scanned class |
| Button shown, requests still return 401 | The scheme is declared, but the endpoint has no requirement, or the token is expired |
| `Bearer` appears twice in the header | The token was pasted with the `Bearer ` prefix. Paste only the token. |
| Swagger UI itself returns 401 or 403 | Spring Security blocks `/swagger-ui/**` and `/v3/api-docs/**` |
The last case is covered in the chapter on protecting the docs in production.
Exercise
You added @SecurityScheme(name = "bearerAuth", ...) and Swagger UI shows the Authorize button. After authorizing, a request to /api/orders still arrives without an Authorization header. What is the most likely cause?
Show answer
The scheme is declared but never applied. Add @SecurityRequirement(name = "bearerAuth") to the controller or method, or set it globally in @OpenAPIDefinition. The name must also match the scheme name exactly.