天天看点

springboot+swagger3+oauth2 client credentials模式授权效果版本依赖配置

效果

文档页面上出现授权按钮

springboot+swagger3+oauth2 client credentials模式授权效果版本依赖配置

点击授权按钮输入客户端id,密码获取令牌

springboot+swagger3+oauth2 client credentials模式授权效果版本依赖配置
springboot+swagger3+oauth2 client credentials模式授权效果版本依赖配置

测试请求中自动携带令牌

springboot+swagger3+oauth2 client credentials模式授权效果版本依赖配置

版本

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")
            );
}           

复制