Skip to content

LDAP filter validator and builder

Check an LDAP search filter for syntax errors and read what it matches, or build a user filter for Keycloak’s LDAP user federation.

Runs in your browser. Nothing you paste is sent to us.

Filter

A sample filter is filled in.

The filter is valid.

What it matches

Reading
ALL of:
  objectClass equals person
  memberOf equals cn=staff,ou=groups,dc=example,dc=com
This checks the syntax only. Whether attributes such as memberOf exist depends on your directory and its schema.

How to use it

  1. Paste a filter to check it, or switch to Build a filter.
  2. Read where a broken filter fails, with the position marked.
  3. Build a filter from a group, account state and attributes.
  4. Paste it into the provider’s Custom user LDAP filter.

What to know

LDAP filters follow RFC 4515: every condition sits in parentheses, & means all, | means any and ! means not. Characters such as *, ( and ) inside a value must be escaped as \2a, \28 and \29.

Keycloak combines the custom user filter with the user object classes set on the provider, so the filter only needs the extra conditions.

On Active Directory, memberOf with the rule 1.2.840.113556.1.4.1941 also matches nested groups, and the userAccountControl rule leaves out disabled accounts.

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.

FormMeaningExample
(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 ruleSee 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.

A sample custom user filter for Active Directory
(&(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

  1. To check a filter, paste it under Check a filter and read the result or the error.
  2. To build one, switch to Build a filter and pick your directory type.
  3. Enter the group DN, if only one group should sign in, and tick the options you need.
  4. Copy the filter into the provider’s Custom user LDAP filter field and save.
  5. Test it with a real search, then try signing in as a user who should match and one who should not.
Testing a filter against a sample directory
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))" uid

Where 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.
Questions

Frequently asked questions

Where do I put an LDAP filter in Keycloak?

In the realm’s User federation, open the LDAP provider and fill in Custom user LDAP filter. The filter must start with `(` and end with `)`.

How do I sync only members of one group from LDAP to Keycloak?

Set the custom user LDAP filter to `(memberOf=<group DN>)`. On Active Directory, use the `1.2.840.113556.1.4.1941` matching rule to include nested groups. On OpenLDAP this needs the memberof overlay.

Do I need objectClass in the custom filter?

No. Keycloak already filters by the user object classes set on the provider and combines them with your custom filter.

How do I exclude disabled Active Directory accounts?

Add `(!(userAccountControl:1.2.840.113556.1.4.803:=2))` to the filter. It leaves out accounts with the disabled bit set.

Why does Keycloak find no users after I set a filter?

The filter may be invalid or match nobody. Check its syntax, then run the same search with the bind account, for example with `ldapsearch`. A wrong group DN is the most common cause.

How do I escape special characters in an LDAP filter?

Write `*` as `\2a`, `(` as `\28`, `)` as `\29` and a backslash as `\5c`. The builder escapes group DNs and values for you.

Does this tool connect to my directory?

No. It only checks syntax in your browser. Whether attributes exist and which users match depends on your directory, so test with a real search.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.