招摇注解获得授权按钮

sos*_*olo 4 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"
Run Code Online (Sandbox Code Playgroud)

我使用注释所做的所有草率文档。但是我找不到执行此按钮的注释。您能帮我找到这个注释吗?

谢谢!

sos*_*olo 5

我用了:

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

然后我在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"));
Run Code Online (Sandbox Code Playgroud)