Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 836
sosolo Avatar asked Mar 06 '17 15:03

sosolo


People also ask

How do I get the Swagger Authorize button?

In the Swagger Editor (the right pane), click the Authorize button, paste the sample API key shown in the description into the Value field (or use your own OpenWeatherMap API key), and click Authorize. Then click Close to close the authorization modal.

What is @API annotation in Swagger?

Annotation Type Api. @Target(value=TYPE) @Retention(value=RUNTIME) @Inherited public @interface Api. Marks a class as a Swagger resource. By default, Swagger-Core will only include and introspect only classes that are annotated with @Api and will ignore other resources (JAX-RS endpoints, Servlets and so on).

How do I enable the Authorize button in Swagger spring boot?

Open the url 'http://localhost:8080/swagger-ui/index.html'. You will see an 'Authorize' button at top right corner. Click on Authorize button.

How do I enable the Authorize button in Swagger net core?

Apply the Authorize attribute in ASP.NET Core 6 Next, apply the Authorize attribute on the HttpGet action method of the WeatherController as shown in the code snippet given below. With the Authorization attribute applied, an authentication token will now be required to execute this endpoint in Swagger.


1 Answers

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"));
like image 82
sosolo Avatar answered Sep 24 '22 21:09

sosolo