在springdoc-openapi-ui中启用Bearer Token Authentication(JWT)的授权按钮 [英] Enable Authorize button in springdoc-openapi-ui for Bearer Token Authentication (JWT)

查看:1334
本文介绍了在springdoc-openapi-ui中启用Bearer Token Authentication(JWT)的授权按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 springdoc-openapi-ui (OpenAPI)中启用授权"按钮3.0 /swagger-ui.html)用于承载令牌身份验证,例如JWT.

How to enable "Authorize" button in springdoc-openapi-ui (OpenAPI 3.0 /swagger-ui.html) for Bearer Token Authentication, for example JWT.

必须在Spring @Controller@Configuration类中添加哪些批注?

What annotations have to be added to Spring @Controller and @Configuration classes?

推荐答案

@Configuration bean中使用注释@io.swagger.v3.oas.annotations.security.SecurityScheme为OpenAPI 3.0定义全局安全性方案:

Define a global security scheme for OpenAPI 3.0 using annotation @io.swagger.v3.oas.annotations.security.SecurityScheme in a @Configuration bean:

@Configuration
@OpenAPIDefinition(info = @Info(title = "My API", version = "v1"))
@SecurityScheme(
    name = "bearerAuth",
    type = SecuritySchemeType.HTTP,
    bearerFormat = "JWT",
    scheme = "bearer"
)
public class OpenApi30Config {

}

使用引用定义的安全方案的@io.swagger.v3.oas.annotations.Operation注释每个需要承载令牌认证(JWT)的@RestController方法:

Annotate each @RestController method requiring Bearer Token Authentication (JWT) with @io.swagger.v3.oas.annotations.Operation referencing the defined security scheme:

@Operation(summary = "My endpoint", security = @SecurityRequirement(name = "bearerAuth"))

这篇关于在springdoc-openapi-ui中启用Bearer Token Authentication(JWT)的授权按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆