Skip to content
Advertisement

How adding Authorize Button to swagger-ui in java EE application

I have a java EE application and I want to add swagger-ui to document my java REST API.
my Swagger-ui it works but I want to add the Authorize Button to swagger-ui in my java EE application
thnak you

this is my pom.xml

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1.1</version>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/io.swagger/swagger-jaxrs -->
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-jaxrs</artifactId>
    <version>1.6.1</version>
</dependency>
  </dependencies>

Application.java

@ApplicationPath("api")
public class Aplicacion extends Application{
    
    public Aplicacion() {

        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setTitle("TJaxRs Swagger");
        beanConfig.setBasePath("/JaxRs/api");
        beanConfig.setResourcePackage("org.api.recursos");
        beanConfig.setScan(true);
    }

}

enter image description here

enter image description here

Advertisement

Answer

I would strongly recommend OpenAPI Specification to comprehend API definition details at the first step. https://swagger.io/docs/specification/authentication/. Then, Swagger Core Jersey’s Github page might be the right address https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5#using-a-servlet.

For finding quick answer I’m going to explain when and why “Authorize” button appear on the right top corner. Please take a look at default Swagger’s pet example via https://editor.swagger.io. You will see securityDefinitions right after the endpoint definitions. By having this, we can define security criteria. It is like a variable declaration in software.

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"

That is not save us, we have to link defined security options with APIs. Within API, you can put defined security options which is being enabled for specified API.

  responses:
    "200":
      description: "successful operation"
      schema:
        $ref: "#/definitions/ApiResponse"
  security:
  - petstore_auth:
    - "write:pets"
    - "read:pets"

In the Java site, bean config would give a chance to modify generated Swagger content. That would be something like below. Hopefully, you can get benefit from it.

BeanConfig swaggerConfigBean = new BeanConfig();
swaggerConfigBean.setConfigId(CONFIG_ID);
swaggerConfigBean.setTitle(TITLE);
swaggerConfigBean.setContact(CONTRACT);
swaggerConfigBean.setSchemes(new String[]{"http", "https"});
swaggerConfigBean.setResourcePackage(yourPackage);
swaggerConfigBean.setVersion(VERSION);
swaggerConfigBean.setBasePath(BASE_PATH);
swaggerConfigBean.setPrettyPrint(true);
swaggerConfigBean.setScan(true);

Swagger swagger = swaggerConfigBean.getSwagger();
swagger.addSecurityDefinition(SECURITY_DEF_FOR_API_KEY, new ApiKeyAuthDefinition(X_HEADER_API_KEY,
        In.HEADER));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement