Swagger 2.0 在哪里声明 Basic Auth Schema [英] Swagger 2.0 where to declare Basic Auth Schema

查看:21
本文介绍了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();

我看过这里:

https://github.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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