Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authorization on Swagger UI #2911

Open
lewimuchiri opened this issue Feb 28, 2023 · 2 comments
Open

Authorization on Swagger UI #2911

lewimuchiri opened this issue Feb 28, 2023 · 2 comments

Comments

@lewimuchiri
Copy link

lewimuchiri commented Feb 28, 2023

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.

@aklish
Copy link
Member

aklish commented Mar 5, 2023

I believe this is covered in the docs here:

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

@lewimuchiri
Copy link
Author

lewimuchiri commented Mar 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants