Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Security Hardening Audit

Audit a Keycloak realm for common weaknesses: SSL required, brute force, password policy, redirect URIs, public clients, token lifetimes, MFA and admin exposure, with a checker script.

Most Keycloak incidents come from configuration, not from exotic bugs: a wildcard redirect URI, a forgotten bootstrap admin, an admin console open to the internet. In this final practice lesson you walk through a hardening checklist, then build a small auditor that scans a realm export for the most common mistakes. Use it as a template for your own review before launch and as a recurring check afterwards.

Keycloak cannot run on this site. The auditor is a JavaScript stand-in that reads a JSON snippet shaped like a realm export (field names follow the real export format for the settings it checks). It is a learning aid and not a replacement for a full security review.

The checklist

Transport and exposure

  • Realm SSL required is external at minimum, all for public-facing realms.
  • TLS terminates with a modern configuration; HSTS is sent by the proxy.
  • The admin console and master realm are reachable only from trusted networks or via VPN.
  • Health and metrics (management port) are not public.
  • Cluster ports are internal only.

Admins

  • The bootstrap admin is replaced by named admin accounts with MFA.
  • The master realm holds only administrators; applications live in their own realms.
  • Use fine-grained admin roles instead of making everyone realm admin.
  • Admin events are saved, and you review them.

Authentication

  • Brute force detection on, with temporary lockout.
  • A password policy: minimum length (12 or more), deny username in password, and blacklist of common passwords; consider Argon2 or PBKDF2 with high iterations (the default is strong, do not lower it).
  • MFA (OTP or WebAuthn/passkeys) for admins and sensitive users; prefer passkeys where possible.
  • Email verification enabled so unverified emails do not drive trust decisions.
  • Registration disabled unless needed; if open, add verification and rate limits.
  • Session lifetimes appropriate: SSO idle and maximum, offline session limits.

Clients

  • No wildcard or overly broad redirect URIs; exact URLs only; no * and no http in production.
  • Web origins minimal; do not use * or + blindly.
  • SPAs and mobile apps are public with PKCE S256 enforced; server apps are confidential with secrets in a secret store.
  • Direct access grants (password grant) off unless a legacy case needs it. Implicit flow off.
  • Service accounts only where needed, with minimal roles.
  • Access token lifespan short (5 minutes is a common choice); refresh tokens rotated or revoked on reuse.

Tokens and keys

  • Realm keys: RS256 or a stronger option; rotate on a schedule; remove unused old keys after they expire.
  • APIs verify signature, issuer, audience, and expiry (see the JWT verifier project).
  • Tokens carry only needed claims (see protocol mappers).

Operations

  • Keep Keycloak current with security patches (upgrades).
  • Database encrypted at rest, backups encrypted and tested, least privilege DB user.
  • Logs shipped, alerts on failed login spikes and admin changes (monitoring).
  • Providers and themes reviewed like application code.

Harden in five layers

Network TLS, private admin
Admins Named, MFA
Login Lockout, MFA
Clients Exact redirects

Then keys, tokens and operations.

1 / 2

The auditor

Give the script a realm export. It checks a handful of rules and prints findings by severity:

JS Stand-in: realm configuration auditor

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Fix the findings in the sample one by one (edit the realm object and run again) until the list is empty. That is a good exercise: each fix maps to a console setting you have seen in this course.

Make it a habit

Run the same checks after every change to realm configuration, ideally in CI against the exported realm JSON stored in Git. Pair the automated checks with periodic manual review:

  1. Who are the admins, and do they still need access?
  2. Which clients are unused? Disable and then delete them.
  3. Which service accounts have powerful roles?
  4. Which identity providers are still active, and do their certificates expire soon?
  5. When were keys last rotated?

A second small tool helps with the recurring "what is expiring?" question:

JS Stand-in: expiry watcher

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which redirect URI setting is the most dangerous?

    A bare wildcard lets an attacker send the code or token to any site they control.
  2. 2. Why use temporary rather than permanent brute force lockout?

    Attackers could lock out real users permanently by failing logins on purpose.
  3. 3. Which flow should normally be disabled on every client?

    Implicit flow exposes tokens in URLs and is replaced by Authorization Code with PKCE.
  4. 4. Why store the realm export in Git and audit it in CI?

    Diffs of configuration make changes reviewable and automated checks catch regressions.

Summary

  • Harden network exposure, admin accounts, authentication, clients, tokens, and operations.
  • The most common faults: wildcard redirects, no MFA for admins, public admin console, long tokens.
  • Automate checks against exported configuration and repeat them regularly.
  • Watch expiries of certificates, keys, and credentials.

Next: Keycloak and Identity Management track