效果
文檔頁面上出現授權按鈕
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAjM2EzLcd3LcJzLcJzdllmVldWYtl2Pn5GcuMWY4YTOmRjMmNDNhFzN2MjM5IDOzkTOlFTYwIzYmlDZvwVMwUTN3QzNtUGall3LcVmdhNXLwRHdo9CXt92YucWbpRWdvx2Yx5yazF2Lc9CX6MHc0RHaiojIsJye.png)
點選授權按鈕輸入用戶端id,密碼擷取令牌
測試請求中自動攜帶令牌
版本
springboot 2.5.4
springdoc 1.5.10
依賴
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webmvc-core</artifactId>
<version>1.5.10</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.5.10</version>
</dependency>
複制
配置
@Bean
public OpenAPI api() {
return new OpenAPI()
.info(new Info().title("My API")
.description("My sample application")
.version("v0.0.1")
.license(new License().name("Apache 2.0").url("https://blog.csdn.net/zhoudingding")))
.externalDocs(new ExternalDocumentation()
.description("My Blog")
.url("https://blog.csdn.net/zhoudingding"))
// 注冊SecuritySchemes
.components(new Components().securitySchemes(Map.of(
"Client Credentials",
new SecurityScheme().type(SecurityScheme.Type.OAUTH2)
.flows(new OAuthFlows().clientCredentials(
new OAuthFlow().tokenUrl(oauth2Host + "/oauth/token")
.scopes(new Scopes().addString("all", "Grants for all."))
))
)))
.addSecurityItem(
new SecurityRequirement().addList("Client Credentials")
);
}
複制