LDAP filters and Keycloak user federation
Keycloak can read users from an LDAP directory or Active Directory instead of its own database. This is called user federation. You add an LDAP provider to a realm, point it at the directory, and Keycloak looks users up there when they sign in.
A directory often holds far more accounts than should use your apps: service accounts, former staff, contractors, disabled users. The Custom user LDAP filter on the provider narrows the search, so Keycloak only sees the users you mean.
The filter is a standard LDAP search filter, as defined in RFC 4515. One wrong parenthesis or unescaped character and the directory rejects the search, or quietly matches nothing. This tool checks the syntax and helps you build a filter for common cases.
Why the filter matters in production
Without a filter, everyone the provider can see in its Users DN can sign in, if they know their directory password. That may include accounts you never meant to give access to.
The filter also controls what Keycloak imports. When import is on, Keycloak copies matching users into its database and keeps them in sync. A broad filter means a large import, slower syncs and more user records to clean up later.
Finally, a broken filter fails in ways that are hard to see. Sign-in fails for everyone, or sync finds no users, and the error is buried in the server log. Checking the filter before you save it avoids that.
LDAP filter syntax in short
Every condition sits in its own parentheses. Conditions are combined with prefix operators, written before the list they apply to.
| Form | Meaning | Example |
|---|---|---|
(attr=value) | Equals | (department=Sales) |
(attr=*) | The attribute is present | (mail=*) |
(attr=a*b) | Matches a pattern | (cn=Jo*) |
(attr>=value) | Greater than or equal | (employeeNumber>=1000) |
(&(…)(…)) | All of the conditions | (&(mail=*)(department=Sales)) |
(|(…)(…)) | Any of the conditions | (|(department=Sales)(department=Support)) |
(!(…)) | Not the condition | (!(department=Interns)) |
(attr:rule:=value) | Extensible match with a matching rule | See the Active Directory section |
Some characters must be escaped inside a value: * as \2a, ( as \28, ) as \29 and a backslash as \5c. A group name with parentheses in its DN is the usual place this goes wrong.
How Keycloak uses the custom filter
On the LDAP provider, Keycloak builds its own search from the Users DN, the search scope and the user object classes. Your custom filter is combined with that search, so a user must match both. You do not need objectClass conditions in the custom filter.
The field expects a complete filter, starting with ( and ending with ). Leave it empty to allow every user under the Users DN with the configured object classes.
The filter applies whenever Keycloak searches the directory for users: at sign-in, in user searches in the admin console, and during sync when import is on.
(&(memberOf=cn=staff,ou=groups,dc=example,dc=com)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))Provider settings that work with the filter
The filter is one of several settings that decide which users Keycloak sees. Check them together, because a filter cannot widen what the others exclude.
- Users DN: the part of the tree Keycloak searches. Users outside it never match, whatever the filter says.
- Search scope: One Level searches only directly under the Users DN; Subtree also searches the branches below it.
- User object classes: the object classes a user entry must have. Keycloak adds them to every search.
- Import users: when on, matching users are copied into Keycloak and kept in sync. When off, Keycloak asks the directory each time.
- Sync settings: periodic full and changed-user syncs bring new matches in, on the schedule you set.
The provider page also has buttons to test the connection and the bind credentials. Use them before you look at the filter, so you know the directory is reachable and the bind account works.
Active Directory and OpenLDAP specifics
Active Directory
Active Directory supports memberOf on user objects. A plain (memberOf=<group DN>) matches direct members only.
To include members of nested groups, use the matching rule 1.2.840.113556.1.4.1941, written as (memberOf:1.2.840.113556.1.4.1941:=<group DN>). It can be slower on large directories.
Disabled accounts have bit 2 set in userAccountControl. The filter (!(userAccountControl:1.2.840.113556.1.4.803:=2)) leaves them out, using the bitwise AND rule.
OpenLDAP and similar servers
On OpenLDAP, memberOf only exists when the memberof overlay is turned on. Without it, a memberOf filter matches nobody. Check with your directory team, or filter on an attribute you know is set.
Other servers have their own ways to mark disabled accounts. There is no standard attribute, so ask what your directory uses.
What this tool does and does not do
The checker parses a filter following RFC 4515 and either explains it in plain words or shows where it fails, with the character position marked. The builder writes a filter from a group, account state, email presence and one extra attribute, and checks the result.
- It catches unbalanced parentheses, missing operators and values, and text after the end.
- It catches unescaped parentheses in values and backslashes not followed by two hex digits.
- It refuses
*with operators other than equals. - It escapes the group DN and attribute value for you in the builder.
It checks syntax only. It does not connect to your directory, so it cannot tell whether an attribute such as memberOf exists there, or whether the filter matches anyone. Use Keycloak’s test buttons and a real search for that.
How to use it
- To check a filter, paste it under Check a filter and read the result or the error.
- To build one, switch to Build a filter and pick your directory type.
- Enter the group DN, if only one group should sign in, and tick the options you need.
- Copy the filter into the provider’s Custom user LDAP filter field and save.
- Test it with a real search, then try signing in as a user who should match and one who should not.
ldapsearch -H ldaps://ldap.example.com \
-D "cn=reader,dc=example,dc=com" -W \
-b "ou=people,dc=example,dc=com" \
"(&(objectClass=person)(memberOf=cn=staff,ou=groups,dc=example,dc=com))" uidWhere it pays off
Connecting a directory for the first time
Start narrow: one group of pilot users. Check the filter here, save it, and sync. Widen the filter once sign-in works for the pilot group.
Moving between environments
Test and production directories often have different group DNs. Keep the filter next to the provider settings in your realm file, and check each version before you import it.
Audit of who can sign in
Paste the filter from the provider into the checker. The plain-words reading shows reviewers exactly which accounts are let in.
Common errors and fixes
Nobody can sign in after saving the filter
The filter is either invalid or matches nobody. Check the syntax here, then run the same search with a tool like ldapsearch using the provider’s bind account.
memberOf matches nobody
On OpenLDAP the overlay may be off. On any server, the group DN must match exactly, including spaces and escapes.
Users removed from the group can still sign in
Users imported earlier may still exist in Keycloak. Run a sync, and check how your provider handles users that no longer match. Remove leftover users if needed.
Security notes
- Use
ldaps://or StartTLS, so directory passwords never cross the network in clear text. - Give the bind account read access only.
- Leave disabled accounts out with a filter, even if the directory also refuses them.
- Keep the filter as narrow as the apps need.
Best practices
- Grant access by group membership, not by naming individual users.
- Escape DNs that contain special characters.
- Test every change with a real search before saving.
- Write down why each condition is in the filter.