Quick revision
The short answer
We search with LdapTemplate and the LdapQueryBuilder, for example query().base("ou=people").where("objectclass").is("person").and("mail").like("*@" + domain), and map each entry with an AttributesMapper, a ContextMapper or ODM classes annotated with @Entry and @Attribute. We avoid LDAP injection by never concatenating user input into filter strings: the query builder encodes values, LdapEncoder.filterEncode covers the rare manual case, and LdapNameBuilder builds distinguished names safely.
What to remember
In this answer, we recapped LDAP entries, DNs and filters, searched ou=people with LdapTemplate and the query builder, and mapped results with an AttributesMapper, a ContextMapper or ODM classes using @Entry and @Attribute. We saw how a concatenated filter lets input add its own conditions, why the builder's encoding prevents it, why like() still needs input validation because the star stays a wildcard, how LdapEncoder.filterEncode covers manual filters, why LdapNameBuilder should build DNs, and why authentication belongs to Spring Security.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We search with LdapTemplate and the LdapQueryBuilder, for example query().base("ou=people").where("objectclass").is("person").and("mail").like("*@" + domain), and map each entry with an AttributesMapper, a ContextMapper or ODM classes annotated with @Entry and @Attribute. We avoid LDAP injection by never concatenating user input into filter strings: the query builder encodes values, LdapEncoder.filterEncode covers the rare manual case, and LdapNameBuilder builds distinguished names safely.
Why this question is asked
Many companies still keep their users in an LDAP directory such as OpenLDAP or Active Directory, so Java services regularly need to search it. Interviewers ask this question because it combines a practical API with a security concern that is easy to miss: LDAP filters can be injected just like SQL. In this answer, we will recap LDAP entries and filters, write the search, map results three ways, show an injection with string concatenation and how the builder prevents it, and build distinguished names safely.
1A Quick Recap of LDAP Entries and Filters
An LDAP directory is a tree of entries. Each entry has a distinguished name, or DN, which is its full path in the tree, such as uid=asha,ou=people,dc=example,dc=com, and a set of attributes such as cn (common name), mail and objectClass. A search starts at a base DN, here ou=people, and returns the entries below it that match a filter.
Filters have their own syntax, written in prefix form with parentheses: (&(objectClass=person)(mail=*@example.com)) means entries that are persons and whose mail ends in @example.com. The star is a wildcard. Characters such as parentheses, the star and the backslash have special meaning, and that is where injection comes from.
2Writing the Search
Spring LDAP gives us LdapTemplate, which handles connections and exceptions, and a fluent query builder. We search ou=people for persons with a given mail domain and map each result to a record.
Notice the domain check at the top. The builder's is() encodes every special character in the value. like() is different on purpose: it keeps the star as a wildcard so we can write patterns, and encodes the other special characters. That means a user who sends a star in the domain could widen the search, so for like() we also validate the input against what a domain can look like.
3Three Ways to Map Results
An AttributesMapper, as above, receives only the attributes of each entry, which is enough for simple read models. A ContextMapper receives a DirContextOperations object, which also gives us the entry's DN and convenient methods such as getStringAttribute("mail"), useful when we need the DN to update the entry later.
The third way is ODM, object-directory mapping, which works like JPA for LDAP. We annotate a class once and let Spring map it in both directions.
ODM pays off when the same class is read, created and updated in several places. For a single search, a mapper is simpler.
4What LDAP Injection Looks Like
Here is the broken version, building the filter as a string. Suppose the domain comes from a request parameter and an attacker sends a crafted value.
- Value from the userdomain, uid, search text
- Built with the query builder?YesEncoded: structure is safeNoMust pass LdapEncoder.filterEncode
- Used in like()?YesAlso validate: the star stays a wildcardNoDone
3 steps in order. Play them, or step through them one at a time.
The input closed our parenthesis and added its own condition, exactly like SQL injection adds an OR 1=1. With a login filter, the same trick can bypass a password check. The query builder prevents this, because the value becomes a parenthesis-free encoded string, so the filter keeps the structure we wrote. If we ever must build a filter string by hand, every value goes through LdapEncoder.filterEncode, which turns ( into \28, ) into \29 and * into \2a.
5Building Distinguished Names Safely
DNs have the same problem: a comma or a plus sign in a value changes the structure of the name. So we build them with LdapNameBuilder instead of concatenation.
Finally, if the goal is to authenticate users against the directory, we should not write our own bind code. Spring Security's LDAP support handles binding, password comparison, group lookup and the edge cases, and it is tested far more widely than code we write ourselves.
6Follow-up questions
How do you page through 100,000 entries?
Directories limit how many entries one search may return, often to 1,000 on Active Directory, so we use the paged results control. In Spring LDAP we create a PagedResultsDirContextProcessor with a page size such as 500, run the search with it inside SingleContextSource.doWithSingleContext so every page uses the same connection, and loop while processor.hasMore() is true, adding each page to the result or, better, processing it and discarding it so memory stays flat. For 100,000 entries that is 200 round trips, which is normal for a batch job.
Is like() vulnerable if I validate the input?
With strict validation, such as allowing only letters, digits, dots and hyphens in a domain, the special characters cannot appear and the search is safe. Validation and encoding together are the defence in depth.
Why prefer Spring Security for login instead of a search and bind?
Because authentication has many traps, such as empty passwords that some servers treat as anonymous binds, and Spring Security already handles them.
7Conclusion
In this answer, we recapped LDAP entries, DNs and filters, searched ou=people with LdapTemplate and the query builder, and mapped results with an AttributesMapper, a ContextMapper or ODM classes using @Entry and @Attribute. We saw how a concatenated filter lets input add its own conditions, why the builder's encoding prevents it, why like() still needs input validation because the star stays a wildcard, how LdapEncoder.filterEncode covers manual filters, why LdapNameBuilder should build DNs, and why authentication belongs to Spring Security.
Open with
I would use LdapTemplate with the LdapQueryBuilder: query, base ou=people, where objectclass is person, and mail like star at the domain. For mapping, an AttributesMapper that reads uid, cn and mail into a record is enough for a simple search; a ContextMapper gives me the DN as well, and for a model used in several places I would use ODM, a class annotated with @Entry with the object classes and base, an @Id Name for the DN and @Attribute fields, then call ldapTemplate.find with the query and the class.
Then, if they want more
To avoid LDAP injection, I never concatenate user input into a filter string. A value like example.com, close parenthesis, open OR objectclass star, would otherwise add its own clause and list the whole directory. The query builder encodes the values, so the filter keeps the structure I wrote.
One caveat is that like keeps the star as a wildcard by design, so for that I also validate the domain against an allowed pattern. If I ever have to build a filter by hand, every value goes through LdapEncoder.filterEncode. For distinguished names, I use LdapNameBuilder, which escapes commas and other special characters, instead of string concatenation.
And if the real goal is login, I would use Spring Security's LDAP authentication rather than writing my own bind code.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Using Spring LDAP, find all users in ou=people with a given email domain and map them to...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/6a67213fe9/