效果
文档页面上出现授权按钮
![](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")
);
}
复制