Swagger注释获取授权按钮

时间:2017-03-06 15:48:57

标签: java annotations swagger swagger-ui

我正在使用swagger来记录我的java REST API。 X-Auth-Token应该在每个api的标题中发送(除了一个)。 我想要在宠物商店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"

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

谢谢!

2 个答案:

答案 0 :(得分:4)

我用过:

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

然后我在Swagger-UI中得到了一个“授权”按钮。 我检查了它做了什么 - 它称为方法self.api.clientAuthorizations.add 当`self = window.swaggerUi;'。

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

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

答案 1 :(得分:0)

Swagger REST API操作注释@ApiOperation也具有以下属性:授权。可以将此属性设置为{@Authorization(value =“ AuthName”)}

相关问题