如何使用 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
它说“一旦你声明并配置了你在 API 中支持的授权方案,你就可以使用这些注释来记录资源或特定操作需要哪种授权方案”但我找不到任何关于在哪里声明和配置授权方案。
更新:
我找到了有关如何声明架构的代码,但我仍然没有在 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);
}
}