RealmDeployDocs
GuidesFramework quickstarts

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:

SettingValue
Client typeOpenID Connect
Client IDquarkus-app
Client authenticationOn
Authentication flowStandard flow. Turn Direct access grants off.
Root URLhttp://localhost:8080
Valid redirect URIshttp://localhost:8080/callback
Valid post logout redirect URIshttp://localhost:8080/signed-out
Web originsLeave 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:

.env
KEYCLOAK_ISSUER=https://auth.example.com/realms/acme
KEYCLOAK_CLIENT_ID=quarkus-app
KEYCLOAK_CLIENT_SECRET=paste-the-client-secret

Install

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

src/main/resources/application.properties
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=authenticated

quarkus.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.

src/main/java/org/acme/HomeResource.java
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("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
    }
}

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:

  1. Open Clients, choose that client, and open its Client scopes tab.
  2. Choose <client-id>-dedicated.
  3. Choose Configure a new mapper (or Add mapper → By configuration), then Audience.
  4. 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.

src/main/resources/application.properties
quarkus.http.port=8081

quarkus.oidc.auth-server-url=${KEYCLOAK_ISSUER}
quarkus.oidc.application-type=service
quarkus.oidc.token.audience=orders-api

In 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:

src/main/java/org/acme/ApiResource.java
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

ErrorCauseFix
Log shows OIDC server is not available at the '...' URLQuarkus 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_urihttp://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 401The 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 KeycloakThe 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 outhttp://localhost:8080/signed-out is not in Valid post logout redirect URIs.Add it exactly.
API returns 401 for a token that decodes fineThe 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 appIn 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

On this page