Skip to content

LDAP User Federation

User federation lets Keycloak read (and optionally write) user accounts from an external store instead of its own internal database. The most common store is LDAP (Lightweight Directory Access Protocol), which is the protocol used by:

  • OpenLDAP — the open-source LDAP server.
  • Microsoft Active Directory (AD) — the enterprise directory service found in most corporate networks.
  • Apache Directory Server, 389 Directory Server, and others.

When a user tries to log in, Keycloak looks them up in the LDAP directory and delegates the password check to the LDAP bind. From the user’s perspective, they are just logging in with their normal directory credentials.

  1. In the admin console, make sure you are in the correct realm.
  2. Click User federation in the left sidebar.
  3. Click Add provider and select LDAP.
  4. Keycloak displays the LDAP configuration form.

Fill in the connection details:

FieldWhat to enter
VendorSelect Active Directory for AD, or Other for OpenLDAP. This pre-fills sensible defaults for DN structures and attributes.
Connection URLThe LDAP server URI, e.g., ldap://ldap.example.com:389 or ldaps://ldap.example.com:636 (LDAPS for encrypted).
Enable StartTLSPreferred alternative to LDAPS for encrypting the connection on port 389.
Connection poolingEnable in production to reuse LDAP connections.

Click Test connection to verify Keycloak can reach the server before continuing.

Keycloak needs a service account to search the directory. This is called the bind DN.

FieldWhat to enter
Bind typesimple for username/password bind.
Bind DNThe distinguished name of the service account, e.g., cn=keycloak-svc,ou=service-accounts,dc=example,dc=com.
Bind credentialsThe service account password. Store this in a Keycloak vault or environment variable — never hardcode it.

Click Test authentication to verify the bind credentials work.

FieldWhat to enter
Users DNThe base DN to search for users, e.g., ou=users,dc=example,dc=com.
Username LDAP attributeThe LDAP attribute that maps to the Keycloak username. For AD use sAMAccountName; for OpenLDAP use uid.
RDN LDAP attributeUsually cn or the same as the username attribute.
UUID LDAP attributeFor AD: objectGUID. For OpenLDAP: entryUUID. This is used as Keycloak’s internal user ID.
User object classesFor AD: person, organizationalPerson, user. For OpenLDAP: inetOrgPerson, organizationalPerson.

These two settings define how Keycloak synchronises users and whether it writes back to LDAP.

READ_ONLY — Keycloak cannot modify any LDAP attributes. Password changes are rejected. Use for corporate AD where Keycloak should not touch the directory.
WRITABLE — Keycloak can update LDAP attributes and sync password changes back to LDAP. Use only if Keycloak is authorised to write to the directory.
UNSYNCED — Keycloak stores attribute changes locally (in its own DB) without writing to LDAP.
# Recommended default for most deployments
Edit mode: READ_ONLY
  • On (default) — Keycloak imports user records into its local database on first login and after each sync. Queries are fast because they hit the local DB.
  • Off — Keycloak queries LDAP on every login. Less data stored locally, but every authentication hits the LDAP server.
  1. Click Save.
  2. Back on the User federation page, open your LDAP provider.
  3. Scroll down to the Synchronization section.
  4. Click Synchronize all users to do an immediate full sync, or configure Changed users sync to run periodically.

After the sync, go to Users in the left sidebar. You should see your LDAP users listed with a “federated” badge.

OptionBenefitCost
READ_ONLYSafest — Keycloak can never write back to LDAP, so there’s no risk of breaking the organisation’s directory. Ideal for a corporate AD owned by ITUsers cannot change their password or profile through Keycloak; they must do it in the source system (e.g. AD)
WRITABLEUsers can change their password or attributes directly through Keycloak — convenient for self-serviceKeycloak can write to the production directory — a misconfiguration or a compromised service account can affect real LDAP/AD data organisation-wide
UNSYNCEDStores attribute changes locally (in Keycloak’s own database) without touching LDAP at all — flexible for extra attributes LDAP doesn’t haveData in Keycloak and LDAP can diverge over time, because local overrides never sync back to the source of truth
  • Importing all users from a large directory unpaged — for a directory with tens or hundreds of thousands of users, an unpaged sync can crash the LDAP server or Keycloak itself from memory pressure or timeouts. Set a batch size and use paged search if the LDAP server supports it.
  • Setting edit mode to WRITABLE without sign-off from whoever owns the directory — Keycloak instantly becomes a client that can write to production AD/LDAP, risking employee data damage if a mapping is wrong. Always start with READ_ONLY and move up only if truly necessary.
  • Not configuring connection pooling or a scheduled sync in production — querying LDAP directly on every login (import off) with no connection pooling puts unnecessary load on the LDAP server when many users log in at once.

Real-world example

An organisation bridging Keycloak with an existing Active Directory — employees keep logging in with the same AD password they already use for Windows and email, with no new password to remember and no separate Keycloak account. IT keeps controlling password policy through AD as before.

A company with OpenLDAP as its central employee directory — uses READ_ONLY LDAP federation so every internal tool behind Keycloak sees the same set of users, without syncing data back and forth between multiple systems.

What LDAP attribute should you use as the username for Active Directory?
What does LDAP edit mode READ_ONLY mean?
What is the purpose of the bind DN in LDAP federation?
When "Import users" is turned off, what happens on every login?