Three timers decide when users sign in again
Keycloak keeps a single sign-on session for each signed-in user. Three realm settings decide how long that session and its tokens last: the access token lifespan, SSO session idle and SSO session max. Together they decide how often a user types their password, and how long a stolen token stays useful.
Each timer answers a different question. The access token lifespan says how long an app may call APIs before it needs a fresh token. SSO session idle says how long the session survives without use. SSO session max says how long it may live at all, however active the user is.
| Setting | Realm JSON field | Default |
|---|---|---|
| Access token lifespan | accessTokenLifespan | 5 minutes |
| SSO session idle | ssoSessionIdleTimeout | 30 minutes |
| SSO session max | ssoSessionMaxLifespan | 10 hours |
The realm JSON stores all three in seconds. In the admin console they sit under Realm settings, on the Sessions and Tokens tabs.
Why they matter in production
Set the timers too short and users sign in again several times a day. Support tickets grow, and people start to distrust the sign-in page. Set them too long and a forgotten browser on a shared computer stays signed in for days.
The access token lifespan has a security weight of its own. An access token is usually checked by your APIs from its signature alone. It keeps working until it expires, even after the user signs out or an admin ends the session. A short lifespan limits how long a leaked token is worth anything.
The values also shape load. Shorter access tokens mean more refresh requests to Keycloak. With many active users, that difference shows up in CPU and database load.
How Keycloak 26 tracks a session
When a user signs in, Keycloak creates the SSO session and issues tokens to the app. The app uses the access token until it expires, then sends the refresh token to the token endpoint for a new pair.
Each refresh counts as use of the session and resets the idle timer. Visiting another app that shares the session does the same, because the browser reaches Keycloak with the session cookie. Nothing resets SSO session max: when it is reached, the session ends and the next refresh fails.
A refresh token cannot outlive its session. It expires when the session would idle out or reach its max, whichever comes first. When a refresh fails, the app must send the user back to the sign-in page.
{
"realm": "acme",
"accessTokenLifespan": 300,
"ssoSessionIdleTimeout": 1800,
"ssoSessionMaxLifespan": 36000
}Keycloak also allows a short grace period past the idle time, so a session may end slightly later than the setting says. Do not rely on the exact second.
Client session settings
A client can override the session times for its own sessions, under the client’s Advanced tab. Keycloak uses the shorter of the client and realm values, so an override can only shorten sessions. A value of 0 means the client uses the realm setting.
Remember me and offline tokens
When the realm offers Remember me, sessions created with it can use their own idle and max values. Offline tokens, requested with the offline_access scope, follow separate offline session settings and survive the end of the normal session. Both are outside what this tool models.
What this tool does and does not do
The visualizer takes the three realm settings and a list of times when the user has your app open. It assumes the app refreshes its access token each time it expires while open. It then walks through the day minute by minute.
- It draws when the app is in use and when a Keycloak session exists.
- It lists each sign-in and each session end, and why the session ended.
- It counts sign-ins and token refreshes for the day.
- It warns when the access token outlives SSO idle, and when SSO idle is longer than SSO max.
It does not model client session overrides, Remember me, offline tokens or the idle grace period. It does not know how your app refreshes tokens: some refresh early, some only on demand, and some keep refreshing in a background tab. Treat the result as a planning aid, then confirm on a test realm.
How to use the visualizer
- Enter the access token lifespan, SSO idle and SSO max in minutes. The defaults are filled in.
- Write the periods when a typical user has the app open, one per line, such as
09:00-12:00. - Read the timeline: grey bars show the app in use, blue bars show a session.
- Read the list of sign-ins and session ends, and the counts below it.
- Change one value at a time until the day matches what your users expect.
The sample day has three periods with a lunch break and an evening check. With the defaults, the lunch break is longer than SSO idle, so the user signs in again after lunch.
Choosing values for real apps
There is no single right answer. These are common patterns, not Keycloak defaults.
Office apps used all day
If users take a lunch break and expect to stay signed in, SSO idle must cover the break. A longer idle with a max of a working day gives one sign-in per day. Keep the access token short either way.
Admin consoles and sensitive apps
For tools that change money, data or permissions, shorter sessions are worth the extra sign-ins. Use a client session override for those clients instead of shortening the whole realm.
Customer-facing apps
Shoppers and readers often return after hours or days. Remember me, or longer sessions, may suit them better than a work-day max. Weigh that against shared and public devices.
Mobile apps
Mobile apps often use offline tokens so the user stays signed in across restarts. Offline sessions have their own settings, so plan them separately from the browser session.
Use cases
- First production deploy: check that the defaults fit your users before launch, not after the first complaints.
- Upgrade from an older version: compare session settings in the old and new realm exports, so an import does not reset them.
- Audit: show reviewers how long a session and a token can last, with the day drawn out.
- Support: reproduce a report of being signed out after lunch by entering the user’s day.
Common problems and fixes
Users are signed out every 30 minutes
That is the default SSO session idle. If the app does not refresh its token or reach Keycloak within that window, the session ends. Raise SSO idle, or make sure the app refreshes while it is open.
Users must sign in again at every refresh
This happens when the access token lives longer than SSO idle. By the time the app refreshes, the session has idled out. Keep the access token lifespan well below SSO idle.
Refresh fails with invalid_grant
The token endpoint answers invalid_grant when the refresh token or its session has expired, or the session was ended. The app should catch this and start a new sign-in instead of retrying.
Signing out does not stop API calls
The access token is still valid until it expires. Keep it short, or have the API check tokens with the introspection endpoint when it must react at once.
Security notes
- Keep access tokens short. Minutes, not hours.
- Set SSO session max, so even an active session ends at some point.
- Use shorter client sessions for admin and payment apps.
- Remember that offline tokens can live for weeks: grant
offline_accessonly to apps that need it.
Best practices
- Change one setting at a time and watch sign-in counts.
- Keep the access token lifespan shorter than SSO idle.
- Keep session settings in your realm file and version control.
- Test the result with a real browser on a test realm.