如何通过 akka-http 和基本身份验证使用 swagger

How to use swagger with akka-http & basic auth

我正在尝试使用 swagger 记录 akka-http API & swagger-akka-http. This blog post 给了我一个良好的开端,但现在我被卡住了,试图记录 API 正在使用基本身份验证。

我有的是:

@Path("/foo")
@Api(value = "/foo", produces = "application/json")
class FooService ...

@ApiOperation(value = "Get list of all foos", nickname = "getAllFoos", httpMethod = "GET",
response = classOf[Foo], responseContainer = "Set")
def getAll: Route = get {...

这会生成一个 json,我可以大摇大摆地查看 UI。但是,我无法使用生成的示例,因为缺少 auth 选项。

我没有找到任何使用 swagger-akka-http 的示例,只有一些使用 yaml config

yaml 中,可能如下所示:

securityDefinitions:
  basicAuth:
    type: basic
    description: HTTP Basic Authentication.

但是,我没有yaml。除了通过注释,我也无法控制生成的 .json

IIUC,提及 auth 方法的正确位置是 ApiApiOperation 注释的 authorizations 参数。此参数应包含一个 Authorization 注释数组。

每个 Authorization 注释的 value 属性应该引用 SecurityDefinitionObject

但我不知道如何使用注释来定义这个 SecurityDefinitionObject

Authorization 注释不应单独使用,如果是则忽略。

有什么我遗漏的吗?我是否需要一个带有额外声明的额外 yamljson 文件?如果需要,我应该把它放在哪里?还有更多吗?

谢谢

编辑

使用 0.7.2-SNAPSHOT,生成的 basicAuth 数组位于:

paths: {
    /foos: {
        get: {
        security: [
            {
            basicAuth: [ ]
            }
        ],

现在唯一的问题是让 Swagger UI 正确解释它并在示例中使用身份验证。 AFAIK,如果你需要 UI 中的基本身份验证,你必须自己添加它,就像 here

中描述的那样

我目前维护swagger-akka-http

代码几乎是 swagger.io 代码库的薄包装。

@Api 和@Api操作注释支持授权参数。

https://github.com/swagger-api/swagger-core/blob/master/modules/swagger-annotations/src/main/java/io/swagger/annotations/Api.java

@Api(value = "/myApi", description = "My API", produces = "application/json", 
authorizations=Array(new Authorization(value="basicAuth")))

我从未使用过此功能,但也许您可以尝试一下。