Search code examples
javaannotationsswaggerswagger-ui

Swagger annotation to get authorize button


I am using swagger to document my java REST API. X-Auth-Token should be send in a header of each api (except one). I would like to have the button like in the pet store V2 of authorize. Can be found here: http://petstore.swagger.io/

I understood that it is defined in the jason\yaml file that generated is by swagger. To be exact it is done in yaml like this:

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"

All my swagger documentation I did with annotations. But I couldn't find the annotation that does this button. Can you help me to find this annotation please?

Thank you!


Solution

  • I used:

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

    Then I got an "Authorized" button in the Swagger-UI. I checked what it did - it called a method self.api.clientAuthorizations.add when `self = window.swaggerUi;'.

    So finally I did an automate authorization by calling to an ajax call getting back a token and calling this method:

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