Quarkus + Swagger UI + Keycloak

Viewed 946

I would like to use my Keycloak authentication for Swagger UI generated by Quarkus. This is my config in application.properties:

quarkus.swagger-ui.oauth2-redirect-url=http://localhost:8080/q/swagger-ui/oauth2-redirect.html
quarkus.smallrye-openapi.security-scheme=oidc
quarkus.smallrye-openapi.security-scheme-name=Keycloak
quarkus.smallrye-openapi.oidc-open-id-connect-url=http://localhost:8888/auth/realms/myrealm/.well-known/openid-configuration
quarkus.swagger-ui.oauth2-redirect-url=http://localhost:8080/q/swagger-ui

And my REST Controllers are annotated like this:

@Path("/my-path")
@Authenticated
@SecurityRequirement(name = "Keycloak")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MyController {

    @GET
    public List<MyObject> listAll() {
       // [...]
    }
}

Now when I click on the "Authorize" button in Swagger, instead of being redirected to my Keycloak login page, I get a popup with all the different Available authorizations:

enter image description here

I cannot use any of these since my Keycloak is not allowing direct grants.

How can I achieve a login with redirect? Is this even possible?

Bonus question: Why do the swagger properties show up as unused in IntelliJ?

enter image description here

1 Answers

All right, I found a way to make it work, albeit with the implicit flow and a dedicated second (public) client in Keycloak.

The Quarkus application.properties:

# Swagger
quarkus.smallrye-openapi.security-scheme=oauth2-implicit
quarkus.smallrye-openapi.security-scheme-name=Keycloak
quarkus.smallrye-openapi.oauth2-implicit-authorization-url=http://localhost:8888/auth/realms/my-realm/protocol/openid-connect/auth
quarkus.swagger-ui.oauth2-redirect-url=http://localhost:8080/q/swagger-ui/oauth2-redirect.html

The redirect URL neeeded to be oauth2-redirect.html

Now I only need to enter the client_id:

enter image description here

And then I am redirected to Keycloak, and then back to Swagger-UI. Hope this helps anyone hoping to achieve the same

Related