Swagger 2.0 在哪里声明 Basic Auth Schema

编程入门 行业动态 更新时间:2024-10-24 18:22:54
本文介绍了Swagger 2.0 在哪里声明 Basic Auth Schema的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用 Swagger 2.0 注释定义基本身份验证并将其显示在 Swagger UI 中.

How do I define basic authentication using Swagger 2.0 annotations and have it display in swagger UI.

在我拥有的资源中:

@ApiOperation(value = "Return list of categories", response=Category.class, responseContainer="List", httpMethod="GET", authorizations = {@Authorization(value="basicAuth")}) public Response getCategories();

我看过这里:

github/swagger-api/swagger-core/wiki/Annotations#authorization-authorizationscope

它说一旦您声明并配置了您在 API 中支持的授权方案,您就可以使用这些注释来记录资源或特定操作需要哪种授权方案"但我找不到任何东西讨论在哪里声明和配置授权方案.

And it says "Once you've declared and configured which authorization schemes you support in your API, you can use these annotation to note which authorization scheme is required on a resource or a specific operation" But I can't find anything that talks about where to declare and configure the authorization schemes.

更新:

我找到了有关如何声明架构的代码,但我仍然没有在 UI 中看到任何有关身份验证架构的信息.我不确定我错过了什么

I found code on how to declare the schema, but I still do not see any information about the authentication schema in the UI. I'm not sure what I am missing

@SwaggerDefinition public class MyApiDefinition implements ReaderListener { public static final String BASIC_AUTH_SCHEME = "basicAuth"; @Override public void beforeScan(Reader reader, Swagger swagger) { } @Override public void afterScan(Reader reader, Swagger swagger) { BasicAuthDefinition basicAuthDefinition = new BasicAuthDefinition(); swagger.addSecurityDefinition(BASIC_AUTH_SCHEME, basicAuthDefinition); } }

推荐答案

使用 Springfox 2.6 注解,在配置中设置 Docket 时,必须首先将 Basic 身份验证定义为安全方案之一,如下所示:

Using Springfox 2.6 annotations, you must first define Basic authentication as one of the security schemes when you set up the Docket in your configuration, like this:

List<SecurityScheme> schemeList = new ArrayList<>(); schemeList.add(new BasicAuth("basicAuth")); return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo) .securitySchemes(schemeList) ...

然后你可以在你的服务中使用 Springfox 注解来为你想要认证的操作设置 Basic Auth:

Then you can use the Springfox annotations in your service to set Basic Auth for the operation for which you want to require authentication:

@ApiOperation(value = "Return list of categories", response=Category.class, responseContainer="List", httpMethod="GET", authorizations = {@Authorization(value="basicAuth")}) public Response getCategories();

更多推荐

Swagger 2.0 在哪里声明 Basic Auth Schema

本文发布于:2023-07-14 21:44:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声明   Swagger   Schema   Auth   Basic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!