elide icon indicating copy to clipboard operation
elide copied to clipboard

Authorization on Swagger UI

Open lewimuchiri opened this issue 1 year ago • 2 comments

The bundled Swagger UI does not give an option to authorize requests. Usually, Swagger presents an Authorize button when any request is sequred (as shown in the screenshot below). When I use Elide Spring Boot starter, and I secure my endpoints, the Swagger UI doesn't present any option to add JWT token.

image

I've also tried adding the 'Authorization' header for every path as follows:

HeaderParameter oauthParam = new HeaderParameter()
                .name("Authorization")
                .type("string")
                .description("OAuth bearer token")
                .required(true);

        SwaggerBuilder builder = new SwaggerBuilder(dictionary, info)
                .withGlobalParameter(oauthParam);

        return builder.build()
                .basePath(settings.getJsonApi().getPath());

Nothing works.

lewimuchiri avatar Feb 28 '23 15:02 lewimuchiri

I believe this is covered in the docs here:

https://elide.io/pages/guide/v4/13-swagger.html#supporting-oauth

aklish avatar Mar 05 '23 21:03 aklish

Thanks. What was lacking in the documentation was that we needed to define a bean of type SwaggerController.SwaggerRegistrations.

Now after adding the code as per the docs, the Authorize button is now enabled but all my JSON api endpoints disappear. See the "No operations in Spec!" part in the screenshot.

My code:

@Bean
public SwaggerController.SwaggerRegistrations buildSwagger2(EntityDictionary dictionary, ElideConfigProperties settings) {
    Info info = new Info()
            .title(settings.getSwagger().getName())
            .version(settings.getSwagger().getVersion());

    SwaggerBuilder builder = new SwaggerBuilder(dictionary, info);

    Swagger swagger = builder.build()
            .basePath(settings.getJsonApi().getPath())
            .securityDefinition("JWT Token", new ApiKeyAuthDefinition("Authorization", In.HEADER))
            .scheme(Scheme.HTTP);

    return new SwaggerController.SwaggerRegistrations(swagger);
}
image

lewimuchiri avatar Mar 06 '23 05:03 lewimuchiri