我正在团队中开发一个新项目,我们正在遵循 API 优先方法来实现 API。我们使用openapi-generator-maven-pluginOpenAPI 3.0.3 格式的 yml 文件生成 API。为了生成 swagger 文件,我们使用 springfox 2.9.2。我面临的问题是当我尝试为请求增加安全性时。
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
security:
- bearerAuth: [ ]
Run Code Online (Sandbox Code Playgroud)
该Authorize按钮不会出现在 swagger 页面中,仅出现请求附近的锁,但它不会执行任何操作(见下图)。
我观察到,如果我打开/v2/api-docsswagger json 不包含安全定义部分。
我设法添加安全性的唯一方法是通过 Docket 对象中的代码添加安全部分,如下所示:
new Docket(DocumentationType.SWAGGER_2)
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Collections.singletonList(bearerJwtKey()))
.select()
.apis(RequestHandlerSelectors.basePackage("com.example"))
.paths(PathSelectors.any())
.build();
Run Code Online (Sandbox Code Playgroud)
这是为 Swagger UI 添加安全性的唯一方法还是我遗漏了什么?