首页 文章

Swagger注释获取授权按钮

提问于
浏览
4

我正在使用swagger来记录我的java REST API . X-Auth-Token 应该在每个api的 Headers 中发送(除了一个) . 我想要在宠物商店V2中授权按钮 . 可以在这里找到:http://petstore.swagger.io/

我明白它是在jason \ yaml文件中定义的,是由swagger生成的 . 确切地说,它是在yaml中完成的,如下所示:

securityDefinitions:
  petstore_auth:
    type: "oauth2"
    authorizationUrl: "http://petstore.swagger.io/oauth/dialog"
    flow: "implicit"
    scopes:
      write:pets: "modify pets in your account"
      read:pets: "read your pets"
  api_key:
    type: "apiKey"
    name: "api_key"
    in: "header"

我用注释做的所有招摇文件 . 但我找不到执行此按钮的注释 . 你能帮我找到这个注释吗?

谢谢!

1 回答

  • 4

    我用了:

    @SwaggerDefinition(securityDefinition = @SecurityDefinition(apiKeyAuthDefinitions = {@ApiKeyAuthDefinition(key = "X-Auth-Token",
        in = ApiKeyAuthDefinition.ApiKeyLocation.HEADER, name = "X-Auth-Token")}))
    

    然后我在Swagger-UI中得到了一个"Authorized"按钮 . 我检查了它做了什么 - 当self = window.swaggerUi;'时,它调用了一个方法self.api.clientAuthorizations.add` .

    最后,我通过调用ajax调用获取一个令牌并调用此方法来进行自动授权:

    self.api.clientAuthorizations.add('X-Auth-Token',
                      new SwaggerClient.ApiKeyAuthorization("X-Auth-Token", Object.keys(response).map(function (key)
                      { return response[key]}), "header"));
    

相关问题