英文:
Configure Jwt token in Swagger 3.0 in spring boot 3
问题
我正在使用Spring Boot v3.0.2应用程序作为我的后端,并尝试将Swagger v3.0集成到我的应用程序中,用于API文档目的。我已成功配置了SwaggerUI与我的应用程序,并能够从Swagger UI调用不需要JWT/Bearer令牌的API,但无法调用需要JWT/Bearer令牌的API。只是为了您的了解,我能够在Postman中传递Bearer令牌成功调用API。
我在互联网上看到了一些示例,显示了在SwaggerUI中设置Bearer令牌的“授权”按钮,位于所有API的顶部。但所有这些示例都使用Swagger2.0或更低版本。
我已经尝试在互联网上搜索如何从Swagger UI 3.0传递Jwt令牌的方法,但没有找到。
如果有人能提供一些关于如何在Spring Boot 3中配置API以接受JWT令牌的思路或文档,以及如何在Swagger 3.0中获取“授权”按钮,那将非常有帮助。
英文:
I am using the a Spring Boot v3.0.2 application for my backend, and I am trying to integration Swagger v3.0 with my application for api documentation purpose. I have successfully configured the swaggerUI with my application, and also able to call the api's(which does not requires JWT/Bearer token) from swagger UI but not able to call the api(which requires JWT/Bearer token). And just for your knowledge, I am able to successfully call the api while passing bearer token from postman.
I have seen some examples on internet that shows a "Authorize" button to set Bearer token on the top of all the api's in SwaggerUI. But all those examples are using Swagger2.0 or lower.
Authorize button visual example
I had tried to search full internet on how to pass Jwt token from Swagger UI 3.0 and cannot find so.
It would be helpful if someone provide me some ideas or document about how to configure the api to accept JWT token in spring boot 3 and also how can i get a "Authorize" button in Swagger3.0
答案1
得分: 1
在Swagger页面中,点击“Authorize”按钮只会将授权头部附加到请求中,使用您提供的凭据,您可以通过在您的OpenApi配置类中添加以下注解来启用它:
@SecurityScheme(
name = "Bearer Authentication",
type = SecuritySchemeType.HTTP,
bearerFormat = "JWT",
scheme = "bearer"
)
然后,您可以通过添加以下注解将其应用于方法/控制器:
@SecurityRequirement(name="Bearer Authentication")
至于为您的API实现JWT令牌身份验证,有很多在线指南。LMGTFY
英文:
The Authorize button in the swagger page will only append the authorization header to the request using the credentials you provide, you can enable it by adding the following annotation in your OpenApi config class:
@SecurityScheme(
name = "Bearer Authentication",
type = SecuritySchemeType.HTTP,
bearerFormat = "JWT",
scheme = "bearer"
)
and then you can apply it to a method / controller by adding
@SecurityRequirement(name="Bearer Authentication")
As for implementing JWT token auth for your API there are plenty of guides online. LMGTFY
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论