Add Keycloak login to a Quarkus app
Sign users in to a Quarkus app with Keycloak using quarkus-oidc in web-app mode, protect a REST API with bearer tokens and realm roles, and log out.
A Quarkus web app signs users in with the authorization code flow through the quarkus-oidc extension (application type web-app), and a separate Quarkus API checks Keycloak access tokens (application type service) and reads realm roles for @RolesAllowed.
Written against Quarkus 3.40.1.2 (the 3.40 LTS stream, the current release on quarkus.io), Java 21 and Keycloak 26.8, checked on 25 November 2026.
Replace auth.example.com with your RealmDeploy hostname and acme with your realm.
Create the client in Keycloak
Open Clients, choose Create client, and use these values:
| Setting | Value |
|---|---|
| Client type | OpenID Connect |
| Client ID | quarkus-app |
| Client authentication | On |
| Authentication flow | Standard flow. Turn Direct access grants off. |
| Root URL | http://localhost:8080 |
| Valid redirect URIs | http://localhost:8080/callback |
| Valid post logout redirect URIs | http://localhost:8080/signed-out |
| Web origins | Leave empty. The browser never calls Keycloak from JavaScript. |
Choose Save, then copy the secret from the Credentials tab.
For production, add your app's real URLs next to the local ones, for example https://app.example.com/callback and https://app.example.com/signed-out. Keycloak compares redirect URIs exactly.
Environment variables
Quarkus reads a .env file from the folder it runs in, or the same names as environment variables:
KEYCLOAK_ISSUER=https://auth.example.com/realms/acme
KEYCLOAK_CLIENT_ID=quarkus-app
KEYCLOAK_CLIENT_SECRET=paste-the-client-secretInstall
Create a project with JDK 17 or newer, then add the OIDC and REST extensions:
./mvnw quarkus:add-extension -Dextensions="oidc,rest"This adds io.quarkus:quarkus-oidc and io.quarkus:quarkus-rest to pom.xml, with versions from the Quarkus platform BOM 3.40.1.2.
Dev Services for Keycloak starts a local Keycloak container in dev and test mode, but only while quarkus.oidc.auth-server-url is not set. With the configuration below it is set, so the app talks to your tenant.
Add login
quarkus.oidc.auth-server-url=${KEYCLOAK_ISSUER}
quarkus.oidc.client-id=${KEYCLOAK_CLIENT_ID}
quarkus.oidc.credentials.secret=${KEYCLOAK_CLIENT_SECRET}
quarkus.oidc.application-type=web-app
# One fixed callback URL to register in Keycloak; users return to the page they asked for.
quarkus.oidc.authentication.redirect-path=/callback
quarkus.oidc.authentication.restore-path-after-redirect=true
# Visiting /logout ends the session here and in Keycloak, then returns to /signed-out.
quarkus.oidc.logout.path=/logout
quarkus.oidc.logout.post-logout-path=/signed-out
quarkus.http.auth.permission.logout.paths=/logout
quarkus.http.auth.permission.logout.policy=authenticatedquarkus.oidc.auth-server-url is the realm's issuer. Quarkus reads https://auth.example.com/realms/acme/.well-known/openid-configuration from it. Without redirect-path, Quarkus sends the page the user asked for as the redirect URI, which you would have to register with a wildcard; with it, there is one exact callback.
package org.acme;
import io.quarkus.oidc.IdToken;
import io.quarkus.security.Authenticated;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.jwt.JsonWebToken;
@Path("/")
public class HomeResource {
@Inject
@IdToken
JsonWebToken idToken;
@GET
@Authenticated
@Produces(MediaType.TEXT_HTML)
public String home() {
String name = idToken.getClaim("preferred_username");
return "<p>Signed in as " + escape(name) + "</p><a href=\"/logout\">Sign out</a>";
}
@GET
@Path("signed-out")
@Produces(MediaType.TEXT_HTML)
public String signedOut() {
return "<p>You are signed out.</p><a href=\"/\">Sign in again</a>";
}
private static String escape(String s) {
return s == null ? "" : s.replace("&", "&").replace("<", "<").replace(">", ">");
}
}Run ./mvnw quarkus:dev and open http://localhost:8080. @Authenticated sends a visitor who is not signed in to Keycloak; Quarkus exchanges the code with the client secret, validates the ID token and keeps the session in an encrypted cookie. /signed-out has no annotation, so it is public.
Protect an API
The API is a second Quarkus app on port 8081 that accepts Authorization: Bearer <access token> from other clients. Add the OIDC extension and REST with JSON:
./mvnw quarkus:add-extension -Dextensions="oidc,rest-jackson"Keycloak does not put your API in the access token's aud claim by default, so add an Audience mapper to the client whose tokens call the API:
- Open Clients, choose that client, and open its Client scopes tab.
- Choose
<client-id>-dedicated. - Choose Configure a new mapper (or Add mapper → By configuration), then Audience.
- Enter a Name, set Included Custom Audience to
orders-api, keep Add to access token on, and choose Save.
If your API is registered as a client in the realm, pick it under Included Client Audience instead and use its client ID as the audience.
quarkus.http.port=8081
quarkus.oidc.auth-server-url=${KEYCLOAK_ISSUER}
quarkus.oidc.application-type=service
quarkus.oidc.token.audience=orders-apiIn service mode, Quarkus checks each bearer token's signature against the realm's keys from /protocol/openid-connect/certs, its expiry, its issuer against the issuer in the discovery document, and, with quarkus.oidc.token.audience, its aud claim. It also reads Keycloak's realm_access.roles claim as the user's roles, so @RolesAllowed works without any mapping code:
package org.acme;
import java.util.Map;
import io.quarkus.security.Authenticated;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.jwt.JsonWebToken;
@Path("/api")
@Produces(MediaType.APPLICATION_JSON)
public class ApiResource {
@Inject
JsonWebToken jwt;
@GET
@Path("me")
@Authenticated
public Map<String, String> me() {
return Map.of(
"subject", jwt.getSubject(),
"username", jwt.getClaim("preferred_username"));
}
@GET
@Path("admin/ping")
@RolesAllowed("admin")
public Map<String, String> adminPing() {
return Map.of("status", "ok");
}
}A token without the orders-api audience, from another issuer, or with a bad signature gets a 401; a valid token without the admin realm role gets a 403 on /api/admin/ping. Paste a token into the JWT decoder to see its iss, aud and realm_access, or list its roles with the Keycloak token roles tool.
Log out
The sign-out link goes to /logout, the path set in quarkus.oidc.logout.path. Quarkus removes its session cookie and sends the browser to the realm's end-session endpoint, https://auth.example.com/realms/acme/protocol/openid-connect/logout, with id_token_hint, post_logout_redirect_uri and a state it checks on return. post_logout_redirect_uri is http://localhost:8080/signed-out, built from quarkus.oidc.logout.post-logout-path, and must be listed in Valid post logout redirect URIs. Because the ID token is sent, Keycloak ends its own session without asking. The logout URL builder shows the same URL.
The quarkus.http.auth.permission.logout lines let only a signed-in user start a logout, as in the Quarkus guide.
This logout runs through the browser, so it needs nothing from us. If you also set a Backchannel logout URL on the client, Keycloak calls your app from your tenant, and outgoing connections from your tenant are closed by default. Tell us the host and port first through How to reach us and we open the connection.
Common errors
| Error | Cause | Fix |
|---|---|---|
Log shows OIDC server is not available at the '...' URL | Quarkus cannot load the discovery document: a typo in the hostname or realm, or KEYCLOAK_ISSUER is not set. | Open KEYCLOAK_ISSUER plus /.well-known/openid-configuration in a browser, or use the OIDC discovery viewer. |
Keycloak shows Invalid parameter: redirect_uri | http://localhost:8080/callback is not in Valid redirect URIs, redirect-path is missing, or Quarkus builds an http:// URL behind a TLS proxy. | Add the URI exactly. Compare both with the redirect URI matcher, and see the proxy question below. |
| Login loops back to Keycloak, or the callback returns 401 | The state cookie set before the redirect did not come back, for example because you started on 127.0.0.1 and returned to localhost. | Use one hostname throughout. |
| Callback returns 401 right after signing in at Keycloak | The client secret is wrong, or Client authentication is off, so the code exchange fails. | Copy the secret again from the Credentials tab. |
Keycloak shows Invalid redirect uri after Sign out | http://localhost:8080/signed-out is not in Valid post logout redirect URIs. | Add it exactly. |
| API returns 401 for a token that decodes fine | The token has no orders-api audience, or it came from another realm. | Add the Audience mapper above, get a new token, and check it with the JWT claims validator. |
@RolesAllowed returns 403 in the web app | In web-app mode, Quarkus reads roles from the ID token, and Keycloak puts realm roles in the access token only. | Set quarkus.oidc.roles.source=accesstoken. |
For other OAuth error codes, see the OAuth error lookup. To see why Quarkus rejected a token, raise the log level with quarkus.log.category."io.quarkus.oidc".level=DEBUG.
FAQ
Can one Quarkus app do both login and the API?
Yes. Set quarkus.oidc.application-type=hybrid: a request with an Authorization: Bearer header is checked as a bearer token, any other request goes through the login flow. We keep them apart here so each one is complete on its own.
Where does Quarkus keep the session?
In an encrypted cookie named q_session, which holds the ID, access and refresh tokens. Nothing is stored on the server, so any instance of the app can read it. If the cookie grows too large for your proxy, the Quarkus guide describes a database token state manager.
What changes behind a reverse proxy?
When a proxy ends TLS, Quarkus sees http://. Set quarkus.http.proxy.proxy-address-forwarding=true so it reads the proxy's forwarded headers, or quarkus.oidc.authentication.force-redirect-https-scheme=true to always build https:// redirect URIs.
Sources
- https://quarkus.io/guides/security-oidc-code-flow-authentication
- https://quarkus.io/guides/security-oidc-bearer-token-authentication
- https://quarkus.io/guides/security-openid-connect-dev-services
- https://quarkus.io/guides/security-oidc-configuration-properties-reference
- https://github.com/quarkusio/quarkus/blob/3.40/extensions/oidc/runtime/src/main/java/io/quarkus/oidc/runtime/OidcTenantConfig.java
- https://www.keycloak.org/docs/latest/server_admin/#audience-support
- https://www.keycloak.org/docs/latest/server_admin/#_oidc-logout
- https://openid.net/specs/openid-connect-rpinitiated-1_0.html
Add Keycloak login to a Spring Boot app
Sign users in to a Spring Boot 4 app with Keycloak through Spring Security's OAuth2 client, protect a JWT API with realm roles, and log out of Keycloak.
Add Keycloak login to an ASP.NET Core app
Sign users in to an ASP.NET Core minimal API app with Keycloak through the OpenID Connect handler, protect an API with JwtBearer, and log out of Keycloak.