天天看點

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

複制