Rotating Keycloak realm signing keys

Plan Keycloak realm key rotation so tokens keep validating and users stay signed in, covering active, passive and disabled keys, priority, JWKS caches and offline tokens.

On this page

Why Keycloak realm key rotation needs a plan before you start

Key rotation means replacing the cryptographic keys a Keycloak realm uses to sign the tokens it issues, and it can be done on a running realm without logging everyone out, provided relying applications validate tokens against the realm's published keys and pick up the new key before the old one stops signing. The realm's signing keys sign the ID tokens, access tokens and logout tokens that applications trust. Each application checks those signatures using the public keys listed in the realm's JSON Web Key Set, so the realm's published keys are the contract between Keycloak and every client that validates tokens.

Security teams usually ask for rotation for three reasons: a written key lifecycle policy, a suspected exposure of key material, or an auditor's request for evidence that keys are replaced on a defined schedule. The page's suggested interval is covered in its own section, so the question to settle first is which of these triggers applies and how quickly the change must land. A planned rotation and an emergency replacement after suspected compromise differ in how much overlap you can allow, so write down the trigger before anyone opens the admin console.

Before changing anything, inventory the relying parties that validate tokens locally. Check which applications fetch the JWKS endpoint on a schedule, which cache keys in memory, and which pin a specific certificate. Confirm with each application owner how often their library refreshes keys and what happens when a token carries an unknown key ID. Those answers, not Keycloak's settings alone, determine how long the old and new keys must overlap.

How a signing key moves from active to passive to disabled

The key starts as the active key in the realm, which signs every new token. Keycloak then demotes it to passive when a new key takes over signing, so it still appears in the JWKS endpoint and tokens it signed earlier keep verifying. The relying application reads that endpoint through its own JWKS cache, which is the trust boundary to watch: a cache that refreshes slowly can reject tokens signed by the new key. Before disabling the old key, confirm that every application has refreshed its cache and that no long-lived offline tokens still depend on it.

The JWKS endpoint publishes active and passive keys, so verifiers accept older tokens during the handover.

What active, passive and disabled keys do in a realm

An active key is the one Keycloak uses to sign new tokens and it is published in the realm's JSON Web Key Set (JWKS) so relying applications can verify those tokens. In a realm with several key providers, only one active key is selected to sign at any moment, so the active key is the one you change when you rotate. Keycloak's server administration guide describes the key providers and their states in the realm settings, and it is the place to confirm the exact selection rules for your version.

  • Signing role: the active key signs new ID tokens, access tokens and other realm-signed tokens issued after it is selected.
  • Published in JWKS: the active key's public part appears in the realm certificate endpoint, which applications use to verify signatures.
  • Single signing key: only one active key should win selection at a time, so confirm which provider Keycloak picks when more than one is active.
  • Priority-driven choice: when several active providers exist, the one with the highest priority value is the one that signs.
  • Confirm in your version: check the realm keys screen and the JWKS output after each change, because provider behavior has been refined across releases.

Before any change, record which key ID is active and confirm your relying applications fetch the JWKS from the realm endpoint.

Rotation options and when each fits

This table compares three ways to replace a Keycloak realm signing key: adding a new active key with the old one kept passive, staging the change through priority, and retiring the old key immediately. Read each row against your client inventory, your token lifetimes and your change control, because the risk of logging users out or breaking relying parties depends on those details.

ApproachEffect on existing tokensJWKS cache and offline token riskWhen it suits a regulated environment
Add a new active key, keep the old one passiveTokens signed with the old key still validate because the passive key stays published, so sessions continue.Clients that refresh their JWKS will pick up the new key; clients with stale caches may fail until they refetch. Offline tokens keep validating while the old key remains available.Usually the default choice, since it gives relying parties a window to refresh and gives you a documented rollback path. Record the change and the retirement date in your change record.
Staged rotation with priority changesNew tokens are signed with the higher priority key, and existing tokens remain valid while the old key is still published.Requires confirming which applications cache keys and for how long. Offline tokens issued under the old key depend on it staying available during the stage.Suits environments with many relying parties and formal change windows, because each stage can be verified before the next. Ask your auditors which evidence of each stage they expect to see.
Immediate retirement of the old keyEvery token signed with the old key stops validating at once, so users are forced to sign in again.Any application that has not fetched the new key, or that pins a certificate, fails at once. Offline tokens signed with the old key stop working.Generally unsuitable for production unless a suspected key compromise requires it. In that case, document the incident decision and plan for a mass re-authentication.

How to rotate a Keycloak realm key in stages

This sequence replaces a realm signing key while tokens issued under the old key remain valid, so users and applications keep working through the change. Each step ends with a check you can run before moving on.

  1. Inventory keys and key providers in the realm

    Before changing anything, list the realm's keys in the admin console under Realm settings, then Keys, and record each key's type, algorithm, status, priority and key ID (kid). Note every key provider in use, including any generated or imported providers and any that are disabled but still listed. Identify which clients and resource servers validate tokens from this realm and whether any of them store a copy of the public key. Done when you have a written baseline of the current active key, its kid, and the owners of every system that validates tokens against it.

  2. Add the new key with higher priority

    Add a new key provider for the realm, such as a generated RSA key, and set its priority higher than the current active key so Keycloak selects it for new token signatures. Keep the old key active rather than deleting or disabling it, so tokens already issued keep validating. Done when the realm key list shows two keys with the new one marked as the highest priority and the old one still active. Confirm the exact provider options in the Keycloak server administration guide for your version, since the labels in the console can change between releases.

  3. Confirm the JWKS endpoint publishes both keys

    Fetch the realm's JWKS document from the OpenID Connect discovery endpoint (the jwks_uri listed in the realm's .well-known/openid-configuration) and confirm it contains both the old and new public keys. Then decode a freshly issued access token and check that its header kid matches the new key. Done when the JWKS lists both kid values and new tokens carry the new kid. If the new key is missing, check that its status is active and that the provider is enabled before proceeding.

  4. Let relying applications refresh their key caches

    Applications and APIs that validate tokens locally often cache JWKS responses or pinned certificates for a period you do not control. Ask each owner to confirm how their library refreshes keys and to trigger a refresh or restart in a test environment first. Then verify in each system that a token signed with the new kid is accepted. Done when every validating system has accepted at least one token signed with the new key, and the list of systems that pin certificates rather than fetching the JWKS is recorded with its owner and a decision on how to update it.

  5. Demote the old key to passive

    Change the old key's status from active to passive. Keycloak will stop using it to sign new tokens but will still publish it in the JWKS, so tokens signed earlier keep validating. Done when new tokens carry only the new kid and the old kid still appears in the JWKS. Compare the kid values in a sample of tokens issued after the change against the published set to confirm nothing is still signed with the old key.

  6. Disable the old key after the agreed overlap

    Once the overlap period agreed with application owners has passed, and tokens signed under the old key have expired or been revoked, set the old key to disabled. Keycloak then removes it from the JWKS. Run a final check that a sample of tokens still validates in each dependent system and that no service reports signature errors in its logs. Done when the JWKS contains only the new kid and no validation failures appear. Keep the old key's provider in the realm only if your recovery plan needs it, and record the date of the change for your audit evidence.

How offline tokens and cached JWKS affect the rotation window

The rotation window is set by the longest-lived thing that still needs the old key, and in Keycloak that is often not the access token. A passive key stays published so that tokens signed before the switch can still be verified, but a token signed with an old key does not expire just because the key stopped being active. Offline tokens are the main case: they are issued for offline access, are held by clients for long periods, and are redeemed later against the realm. Before you move a key to passive or disabled, check the offline session lifetime and the count of clients that request offline access, and confirm with your team which of them hold offline tokens today. Offline session settings are documented in the Keycloak server administration guide, and the window you plan should cover them, not only the access token lifetime.

Applications that cache JWKS are the second constraint, and they fail in a different way. A resource server or API gateway that fetched the key set once and keeps it for a fixed period will reject tokens whose kid is not in its cache until the cache refreshes. Some libraries refresh on an unknown kid, some refresh only on a timer, and some pin a certificate or a single key in configuration and never fetch the set at all. Inventory every service that validates Keycloak tokens, and for each one record where the keys come from, the cache lifetime, the refresh trigger on an unknown kid, and whether a certificate is pinned. Confirm these behaviors with the library version each team runs, not with the library's general documentation. Test the new key in a staging environment by issuing a token with the new key before the production switch, and watch for rejected tokens.

Retire the old key only after the inventory shows that no client holds tokens signed with it that must still work, and that every validator has picked up the new key set. Keep the old key passive for at least the longest offline and cache lifetime you found, then disable it and watch authentication errors for a period before you remove it. If a team cannot give you a cache lifetime or a refresh behavior, treat that service as a blocker for retirement, not as a low-risk exception. Record the inventory and the test results with the change ticket, since the same evidence supports later internal reviews.

Security and operational risks during key rotation

Rotating a realm signing key touches more than the Keycloak server. The risks below sit at the edges: who still trusts the old key, which applications pin certificates, what other realms and clients depend on, and who can change keys in the first place. Each card names the mitigation and the check to run before and after the change.

Compromised key stays published

If a signing key is suspected of exposure, moving it to passive does not stop it from being published in the realm's JWKS endpoint, and tokens signed with it still validate. Revoke the key by disabling it, then confirm the old key no longer appears in the JWKS document and that tokens it signed are rejected by your resource servers. Treat any token signed by that key issued after the suspected exposure as untrusted, and plan a session and token revocation along with the key change.

Keycloak docs

Disabling a key too early

Removing a key from validation before every token it signed has expired breaks sessions that are still valid, and resource servers that cache the old key will reject tokens they should accept. Keep the previous key as passive until the longest-lived token signed with it has expired, including offline tokens. Verify by checking that the key's replacement is active and that sign-in and token refresh succeed for a test user on each client.

Certificate pinning in client applications

Some mobile apps, desktop clients and internal services pin the realm certificate or a specific public key rather than fetching keys from the JWKS endpoint. A new signing key will not match the pin, and token validation fails for those clients only. Inventory which applications pin keys before rotation, update the pins in a release that ships before the rotation, and confirm each pinned client still validates tokens after the change.

Untracked keys in other realms and clients

Keys are set per realm, but clients can be configured to sign or encrypt with their own keys, and other realms may trust this realm as an identity provider. A rotation that covers only the realm's default keys can leave client-level or cross-realm trust unchanged and break validation elsewhere. Build a list of every realm, client and brokered trust relationship that depends on the key, and confirm each one after the rotation.

Missing audit evidence of rotation

Auditors and internal reviewers will ask when each key was created, activated, made passive or disabled, and who approved the change. Keycloak's admin events and logs capture some of this, but the record is only useful if it is enabled, retained and linked to a change ticket. Confirm that admin event logging is on for the realm, export the key list before and after the rotation, and store both with the approval record.

Admin access to change keys

Changing realm keys is a privileged action, and any administrator who can edit realm keys can also alter token trust for the whole realm. Limit the key management role to a small group of named administrators, require MFA for those accounts, and keep the master realm out of day-to-day work. Verify the role assignments before and after each rotation, and check that the key changes appear in the admin event log under those named accounts.

How much work is Keycloak key rotation and what do we need to provide?

How long does a Keycloak realm key rotation take?

The duration depends on how many realms, clients and external systems rely on the current signing key, and on how many of those systems cache keys or pin certificates. A single realm with a few well-documented clients is a much smaller job than a shared realm feeding dozens of services across several teams. Keycloak itself lets you add a new key as passive, promote it to active, and retire the old one in stages, so the time is mostly spent on inventory and verification rather than on the change in the console. Ask any partner to base its timeline on a written inventory of relying parties, not on the number of realms alone.

What do application owners have to provide during a rotation?

Application owners need to tell you how their software fetches and caches signing keys. The key questions are whether the library reloads the JWKS endpoint when it sees an unknown key ID, how long it caches the key set, and whether anyone has pinned a certificate or public key in code, configuration or a secrets store. Owners also need to confirm which tokens they validate offline and whether any batch jobs or long-lived services hold tokens that must survive the change. Plan a short confirmation step with each owner, because a team that cannot answer these questions is the most likely source of a logout incident.

Can our internal team rotate Keycloak keys without outside help?

Yes, if your team already administers Keycloak and has a staging environment that mirrors production realms and clients. The console steps are not the hard part. The effort lies in building the relying-party inventory, testing each application's key handling in staging, scheduling the change around its consumers, and writing a rollback path. Teams that have not done this before often underestimate the verification work. An outside partner can take on the planning, testing and runbook, or act as a second reviewer while your staff perform the change.

What does a fixed-scope rotation engagement include?

A fixed-scope engagement covers what has been agreed in writing before work starts. For key rotation that usually means the realms in scope, an inventory of relying applications and how each one validates tokens, a rotation plan with active, passive and disabled key stages, a test run in a non-production environment, the production change window and a post-change check of login and token validation. Items such as application code changes, custom providers or work on the applications themselves should be listed as in or out of scope. DeployOpen does not publish a price list, so the quote follows directly from that written scope.

What recurs after the first key rotation?

The one-time work is the inventory, the first rotation and the runbook. What recurs is the scheduled rotation itself, the review of relying applications when they change, and re-testing after Keycloak upgrades. Your own team should also keep the inventory current, since a new application that pins a certificate can break at the next rotation. Ask any partner whether a retainer covers repeat rotations, upgrade testing and continuous checks, and what it leaves to your staff. Keycloak's suggested rotation interval should be set by your own policy, and that policy should be confirmed against the current Keycloak documentation.

How do we prepare evidence of key rotation for an auditor?

Auditors generally want to see that signing keys are rotated under a defined policy, that the change was planned and approved, and that it was verified. Keep a dated record of the realm key configuration before and after each change, the rotation policy and its owner, the relying-party inventory, the test results from staging, and the change ticket with approvals. Keep records of any exceptions, such as applications that needed extra time. DeployOpen prepares systems, controls and evidence for compliance assessments, but independent auditors and certification bodies make the assessment, so confirm with your auditor which artifacts they expect.

What are the main risks that increase rotation effort?

Effort rises sharply when applications cache keys for long periods, when certificates are pinned in code or in mobile apps, when offline tokens are in circulation, or when no one owns a given client. Each of these forces a slower staged rollout or a coordinated release. Offline tokens deserve particular attention, because they can outlive the normal session and must be checked against the key state you choose. Identify these cases during inventory, since discovering them mid-change is what turns a planned rotation into an incident.

How DeployOpen scopes and delivers Keycloak key rotation

DeployOpen plans and carries out realm signing key rotation for self-hosted Keycloak in your own infrastructure. The work starts with an inventory of which applications, APIs and offline token consumers depend on the current keys, then moves through a staged rotation with verification and a written runbook your team can repeat.

How we scope

  • The written scope lists each realm, its current active, passive and disabled keys, and the signing algorithms in use.
  • Scope covers an inventory of clients, resource servers and libraries that cache JWKS or pin certificates.
  • Engagements are fixed-scope, with deliverables and timelines agreed in writing before work starts.

How we build

  • We stage the new key as passive, set priorities, and test token validation in a non-production realm first.
  • Rotation steps are scripted or documented so each change, its owner and its rollback point are recorded.
  • We test offline token consumers and cached JWKS clients against the new key before the old key is disabled.

How we hand over

  • You receive a rotation runbook, the verification checklist used, and a record of each key's state and change date.
  • Documentation names the owners for application-side changes, such as JWKS cache settings and certificate pins.
  • Your team runs a rehearsal of the runbook, with the outcome and open items recorded in the handover document.

How we support

  • Ongoing support, upgrades and continuous testing are available as retainers, including the next scheduled rotation.
  • Keycloak upgrades and release changes are reviewed against the signing key configuration before rollout.
  • Your team confirms the recurring rotation interval and owners with its own security policy and application owners.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.