Connect Active Directory to Keycloak (LDAP)
Federate Microsoft Active Directory users and groups into your Keycloak realm over LDAPS, read-only, with password checks against your domain controllers.
With LDAP user federation, users sign in on the Keycloak login page with their Active Directory username and password. Keycloak looks the user up in your directory, checks the password against it, and can copy users and their AD groups into the realm. This is not identity brokering: there is no redirect to another login page, Keycloak talks to your domain controllers directly.
Checked against the Keycloak 26.8 docs and admin console, and Microsoft's Active Directory search filter docs, on 25 November 2026.
Replace auth.example.com with your RealmDeploy hostname and acme with your realm. The examples use the domain corp.example.com (DC=corp,DC=example,DC=com) and a domain controller published as ldap.corp.example.com.
Keycloak connects out to your directory, not the other way round. Your tenant runs in our cloud, so your domain controller must be reachable from the internet on a host name and port, or over a connection we agree with you. Outgoing connections from your tenant are closed by default, and we must open this one before anything below works.
Use LDAPS (ldaps://, usually port 636) only. Never expose plain LDAP (port 389) to the internet: binds would send your service account's password and every user's password in clear text.
Before you start, tell us through How to reach us how your directory is reachable: the host name, the port, and any firewall rule on your side. We check what is possible and open the connection.
Before you start
You need:
- A service account in AD with read access to the users and groups Keycloak should see, for example
CN=svc-keycloak,OU=Service Accounts,DC=corp,DC=example,DC=com. It does not need write rights, because this guide sets Keycloak to read-only. - The DNs of the organizational units that hold the users and the groups, for example
OU=Staff,DC=corp,DC=example,DC=comandOU=Groups,DC=corp,DC=example,DC=com. - An LDAPS certificate on the domain controller whose name matches the host name in the connection URL. Keycloak checks the host name strictly on LDAP connections. A certificate from a public certificate authority is trusted as is. If it comes from your own internal CA, Keycloak has to trust that CA, which is a server setting we manage: send us the CA certificate (only the public certificate, never a private key) and we check it.
In Keycloak
Add the LDAP provider
Open User federation and choose Add Ldap providers (or Add new provider → LDAP if the realm already has one). Fill in the sections below, then choose Save.
Decide Edit mode and Username LDAP attribute before the first save: Keycloak creates its default mappers from them and does not change those mappers if you change the settings later.
General options
| Setting | Value |
|---|---|
| UI display name | active-directory |
| Vendor | Active Directory |
Choosing Active Directory fills in AD defaults for the attributes below.
Connection and authentication settings
| Setting | Value |
|---|---|
| Connection URL | ldaps://ldap.corp.example.com:636. For failover, list several domain controllers separated by spaces only. |
| Enable StartTLS | Off (the connection is already TLS) |
| Use Truststore SPI | Always (the default; Keycloak marks this setting as deprecated) |
| Connection pooling | On (the default) |
| Bind type | simple |
| Bind DN | CN=svc-keycloak,OU=Service Accounts,DC=corp,DC=example,DC=com |
| Bind credentials | The service account's password |
Choose Test connection, then Test authentication. The first checks that Keycloak reaches the server over TLS; the second checks the bind DN and password.
LDAP searching and updating
| Setting | Value |
|---|---|
| Edit mode | READ_ONLY |
| Users DN | OU=Staff,DC=corp,DC=example,DC=com |
| Username LDAP attribute | sAMAccountName (users sign in as jdoe) or userPrincipalName (users sign in with the full name that ends in corp.example.com). The AD default is cn, which is the full name in most domains. |
| RDN LDAP attribute | cn |
| UUID LDAP attribute | objectGUID |
| User object classes | person, organizationalPerson, user |
| User LDAP filter | Optional, for example (memberOf=CN=Keycloak Users,OU=Groups,DC=corp,DC=example,DC=com) to let in only members of one group. The filter must start with ( and end with ). |
| Search scope | Subtree, if users sit in OUs below the Users DN; One Level otherwise |
| Pagination | On |
The LDAP filter builder helps you write and check the User LDAP filter.
With READ_ONLY, Keycloak never writes to AD. Users cannot change their name, email or password in Keycloak; they change their password the way they do today, in Windows or your own tools.
Synchronization settings
| Setting | Value |
|---|---|
| Import users | On. Keycloak keeps a local copy of each user (never the password). |
| Sync Registrations | Off |
| Periodic full sync | On, for example every 86400 seconds (daily) |
| Periodic changed users sync | On, for example every 3600 seconds (hourly) |
| Remove invalid users during searches | Off, so a user who disappears from AD is kept in Keycloak as read-only and disabled rather than deleted |
Kerberos integration
| Setting | Value |
|---|---|
| Allow Kerberos authentication | Off |
| Use Kerberos for password authentication | Off |
Kerberos (Windows single sign-on) needs a keytab file on the Keycloak server itself, and changes how browsers talk to Keycloak; this guide leaves it off.
Advanced settings
| Setting | Value |
|---|---|
| Trust Email | On if your IT manages the mail attribute, so Keycloak treats those addresses as verified |
After saving, open the provider's Action menu and choose Sync all users once. The periodic syncs keep it current from then on.
The default mappers
On the provider's Mappers tab, Keycloak has created mappers for username, first name, last name, email and the account dates. For Active Directory it also adds MSAD account controls, which reads userAccountControl and pwdLastSet, so a user whose AD account is disabled is disabled in Keycloak too.
Groups and roles
Add a mapper that copies AD groups into Keycloak groups:
- On the provider's Mappers tab, choose Add mapper.
- Set Name to
groupsand Mapper type togroup-ldap-mapper, fill in the rest and choose Save:
| Field | Value |
|---|---|
| LDAP Groups DN | OU=Groups,DC=corp,DC=example,DC=com |
| Group Name LDAP Attribute | cn |
| Group Object Classes | group |
| Preserve Group Inheritance | Off. Keycloak gives a group one parent; AD groups can sit in several, and Keycloak's sync can fail on that. |
| Ignore Missing Groups | Off |
| Membership LDAP Attribute | member |
| Membership Attribute Type | DN |
| LDAP Filter | Optional, for example (cn=app-*) to copy only some groups |
| Mode | READ_ONLY |
| User Groups Retrieve Strategy | GET_GROUPS_FROM_USER_MEMBEROF_ATTRIBUTE |
| Member-Of LDAP Attribute | memberOf |
| Drop non-existing groups during sync | Off, until you have checked the result once |
| Groups Path | / (top level), or an existing Keycloak group such as /ad |
- Open the mapper again and choose Sync LDAP groups to Keycloak from its action menu.
With GET_GROUPS_FROM_USER_MEMBEROF_ATTRIBUTE, Keycloak reads the user's memberOf values and keeps only the groups under LDAP Groups DN. To include groups a user belongs to through other groups, choose LOAD_GROUPS_BY_MEMBER_ATTRIBUTE_RECURSIVELY instead; Keycloak then asks AD with its in-chain matching rule (LDAP_MATCHING_RULE_IN_CHAIN), which costs the domain controller more work.
To turn AD groups into Keycloak roles instead of groups, use the role-ldap-mapper the same way, or give Keycloak groups their roles under Groups → (group) → Role mapping.
Your apps see the groups in Keycloak's tokens once their client has a Group Membership mapper (Client scopes → <client-id>-dedicated → Configure a new mapper).
Test it
- Under Users, search for an AD user. The user shows the federation link to
active-directory. - On the user's Groups tab, check the AD groups.
- In a private browser window, open
https://auth.example.com/realms/acme/accountand sign in with the AD username and password. - Disable a test user in AD, wait for the next changed users sync or choose Sync changed users, and check that the sign-in is refused.
Common errors
| Error | Cause | Fix |
|---|---|---|
| Test connection fails or times out | The outgoing connection is not open yet, the host or port is wrong, or a firewall on your side drops the traffic. | Tell us the host and port through How to reach us, and check your firewall. |
| Test connection fails with a certificate or TLS error | The certificate's name does not match the host name in Connection URL, or it is issued by an internal CA Keycloak does not trust. | Use the name on the certificate in the URL. Send us your CA certificate. The X.509 decoder shows a certificate's names. |
| Test authentication fails | Wrong Bind DN or password, or the service account is locked or expired. | Copy the account's full DN from AD and reset its password. |
| Sync finds no users | Users DN is wrong, Search scope is One Level while users are in sub-OUs, or the User LDAP filter matches nobody. | Fix the DN, set Subtree, test the filter. |
Invalid username or password. for a user who exists | The user types a different name than Username LDAP attribute holds, or the user is outside the Users DN or filter. | Choose sAMAccountName or userPrincipalName to match what users type, and check the filter. |
| A user cannot change their password | Expected with READ_ONLY. | Users change passwords in AD. |
| Groups are missing | The groups are outside LDAP Groups DN, the group sync has not run, or nested membership needs the recursive strategy. | Fix the DN, run Sync LDAP groups to Keycloak, or switch the strategy. |
| Group sync fails | Preserve Group Inheritance is on and a group has several parents. | Turn it off. |
FAQ
Do passwords leave our directory?
Keycloak never imports passwords. Each password check is a bind from your tenant to your domain controller over LDAPS, so the directory must be reachable whenever users sign in.
Can you connect over a VPN or a private link?
Tell us how your directory is reachable today, and we check what is possible for your tenant. Until we have agreed something else with you in writing, plan for LDAPS over the internet.
Is there an option that needs no connection into our network?
If your Active Directory already syncs to Microsoft Entra ID, connect Entra ID instead: see Connect Microsoft Entra ID over OpenID Connect. Keycloak then only talks to Microsoft's cloud, not to your domain controllers.
Can Keycloak create users in AD?
Only with Edit mode WRITABLE and Sync Registrations on, and a service account with write rights. This guide keeps Keycloak read-only, which is the safer start.
Sources
- Keycloak Server Administration Guide: LDAP and Active Directory (storage mode, edit mode, LDAPS and truststore, failover, synchronization, LDAP mappers)
- Keycloak: Configuring trusted certificates (strict host name checking for LDAP)
- Keycloak source 26.8.1: LDAPStorageProviderFactory (default mappers, MSAD account controls) and GroupLDAPStorageMapperFactory (group mapper fields and defaults)
- Keycloak admin console 26.8.1: LDAP settings forms (field names, Active Directory defaults)
- Microsoft: Search filter syntax (
LDAP_MATCHING_RULE_IN_CHAIN)