Swagger 2.0 在哪里声明基本身份验证架构

2024-04-28

如何使用 Swagger 2.0 注释定义基本身份验证并将其显示在 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 https://github.com/swagger-api/swagger-core/wiki/Annotations#authorization-authorizationscope

它说“一旦您在 API 中声明并配置了支持的授权方案,您就可以使用这些注释来说明资源或特定操作需要哪种授权方案”但我找不到任何有关的内容在哪里声明和配置授权方案。

Update:

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

@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 时,必须首先将基本身份验证定义为安全方案之一,如下所示:

List<SecurityScheme> schemeList = new ArrayList<>();
schemeList.add(new BasicAuth("basicAuth"));

return new 
  Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo)
                                     .securitySchemes(schemeList)
                                     ...

然后,您可以在服务中使用 Springfox 注释为您想要要求身份验证的操作设置基本身份验证:

@ApiOperation(value = "Return list of categories", response=Category.class, responseContainer="List", httpMethod="GET", authorizations = {@Authorization(value="basicAuth")})
public Response getCategories();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swagger 2.0 在哪里声明基本身份验证架构 的相关文章

随机推荐