如何使用 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
发布评论