BlogKeycloak

Keycloak Token Lifetime vs RabbitMQ and Kafka Connections

Scott Sternloff

By Scott Sternloff, Senior Enterprise Architect

LinkedIn · Updated · 5 min read

Keycloak Token Lifetime vs RabbitMQ and Kafka Connections
On this page

Short answer

A broker connection can outlive the token it was opened with, and each broker documents a different consequence. RabbitMQ disconnects an AMQP 1.0 client when the latest token expires, while Kafka's brokers can be set to disconnect connections that do not re-authenticate in time.

A broker connection can outlive the token it was opened with, and each broker documents a different consequence. RabbitMQ disconnects an AMQP 1.0 client when the latest token expires, while Kafka's brokers can be set to disconnect connections that do not re-authenticate in time.

The lifetime you set in Keycloak therefore becomes a connection policy for your messaging layer. This note walks through what each product's documentation says, so you can align the settings. Production support for the identity side is on our Keycloak support page.

What happens to a RabbitMQ connection when the token expires?

It depends on the protocol. RabbitMQ's OAuth 2 documentation says that on AMQP 1.0, if the latest token on an existing connection expires, RabbitMQ disconnects the client. On AMQP 0.9.1 it refuses further operations after a limited time but does not disconnect.

ProtocolDocumented behavior at expiryHow the client avoids it
AMQP 1.0RabbitMQ disconnects the clientRefresh early with a PUT to /auth/tokens
AMQP 0.9.1Further operations refused after a limited time; no disconnectupdate-secret, or reconnect with a new token

The page gives no recommended token lifetime and does not cover MQTT or STOMP expiry, so check those separately.

Which Keycloak settings decide how long a token lives?

Several do, not one. Keycloak's server administration guide puts session and token timeouts under the Sessions and Tokens tabs of Realm settings, and a short access token alone does not shorten a session.

  • Access Token Lifespan controls the lifetime of an OIDC access token.
  • SSO Session Idle and Max end the user session; the guide says Client Session Idle and Max should be shorter than their SSO counterparts.
  • Revoke Refresh Token makes each refresh token usable once, so clients must store the new one.
  • Offline tokens are not subject to the SSO idle and max timeouts.

The guide adds a two-minute window to idle timeouts when persistent user sessions are not active, to cope with cluster nodes that have not yet seen a refresh. If you run more than one node, read cluster and session cache failure modes.

How does Kafka handle token expiry on OAUTHBEARER connections?

Kafka's SASL/OAUTHBEARER documentation says Kafka periodically refreshes a token before it expires so clients can keep making connections. It does not say that the broker closes a connection when a token expires.

The broker setting that does bound a session is connections.max.reauth.ms. Its default is 0; a positive value is the maximum session lifetime the broker advertises, and the broker disconnects a connection that is not re-authenticated within it. Kafka's broker configuration reference lists this, and the same page marks an empty expected audience as accepting tokens with no aud claim, which it strongly discourages.

The documentation also says the default OAUTHBEARER implementation uses unsecured tokens and is suitable only for non-production installs.

What happens to signing keys when Keycloak rotates them?

RabbitMQ validates the JWT signature with keys it finds through the auth_oauth2.issuer setting, which for Keycloak has the form https://host/realms/realm, using OpenID Connect Discovery. A jwks_uri can override the discovered endpoint and must be HTTPS. RabbitMQ downloads a signing key once, the first time it sees it. Its documentation says that after a rotation, newer tokens refer to a key RabbitMQ does not have, which triggers another download. It does not describe a periodic refresh of keys already downloaded.

Keycloak's guide describes one active key pair with optional passive keys that still verify older signatures. It suggests creating new keys every three to six months and deleting the old ones one to two months later, so tokens and cookies can be refreshed first. Kafka exposes jwks.endpoint.url and a refresh interval setting on the broker. Align your key deletion with the longest token you still honor.

Why do audience and scope settings break logins that look correct?

Because the broker validates claims that must be present in the JWT. RabbitMQ says the token aud must contain the configured resource_server_id or the token is rejected, and that disabling the check is not recommended.

It also says Keycloak roles under resource_access.account.roles are not RabbitMQ scopes, so they must be mapped with scope aliases. The default scope prefix is resource_server_id followed by a dot. On Kafka, the audience list and the scope claim name are broker settings you must match to what Keycloak issues. Test one real token end to end before rollout.

If you use Keycloak authorization services, RabbitMQ's page says the requesting party token stores permissions under authorization.permissions, so auth_oauth2.additional_scopes_key must be set to authorization.permissions.scopes for RabbitMQ to read them as OAuth 2.0 scopes. The same resource_server_id is sent as the resource parameter when the management UI starts the Authorization Code flow, so one wrong value affects both AMQP clients and the UI.

What should you align before production, and when should you ask for help?

  1. Write down the longest connection you expect and compare it with the access token lifespan.
  2. Make clients refresh before expiry: PUT to /auth/tokens on AMQP 1.0, update-secret on AMQP 0.9.1, Kafka's refresh settings.
  3. Decide on connections.max.reauth.ms for Kafka.
  4. Match audience and scope claims to resource_server_id and Kafka's expected audience.
  5. Schedule key rotation so old keys outlast old tokens.

If a connection drops on a schedule you cannot explain, that is the moment to bring in a senior engineer who can read both the broker and the Keycloak side. AceMQ supports Keycloak as the OpenID Connect provider for RabbitMQ and Kafka; see Keycloak consulting. It does not replace your own testing of these documented settings.

Sources

Frequently Asked Questions

Does RabbitMQ disconnect clients when a Keycloak token expires?

On AMQP 1.0, yes: RabbitMQ's documentation says it disconnects the client when the latest token on the connection expires. On AMQP 0.9.1 it refuses further operations after a limited time but keeps the connection open. Clients can refresh early to avoid either.

How do I refresh a token on a RabbitMQ connection?

RabbitMQ documents a PUT to /auth/tokens for AMQP 1.0, an HTTP-over-AMQP request, and the update-secret method for AMQP 0.9.1 clients that support it. Without that support the client must disconnect and reconnect with a new token.

What does Kafka connections.max.reauth.ms do?

It sets the maximum session lifetime the broker advertises to clients. The broker disconnects a connection that is not re-authenticated within that time and then used for anything else. The default is 0, and it can be prefixed with a listener name and mechanism.

Scott Sternloff

Author

Scott SternloffSenior Enterprise Architect

Senior Enterprise Architect at AceMQ. Writes the technical coverage here: RabbitMQ, Kafka and Redis architecture, clustering and migrations.

Talk to an engineer

Need a hand with Keycloak?

Senior engineers who run it in production. Get a quote or a free consultation, no sales pitch.

Free Consultation

Get Expert Eyes on Your Keycloak Environment

Whether you're troubleshooting a production incident, planning a migration, or want a second opinion on your architecture — our team is ready. No pitch, just answers.

Email Us