Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Compare LDAP bind authentication and password comparison in Spring Security. Which would you use against Active Directory and why?

In bind authentication Spring Security connects to the directory as the user with the password they typed, and the directory itself checks it. In password comparison Spring reads or compares the stored password attribute, which needs extra directory permissions and matching hash formats, so it is rarely appropriate. Against Active Directory we use ActiveDirectoryLdapAuthenticationProvider, which binds as user@domain and reads group membership; the connection must use LDAPS or StartTLS, and directory groups are mapped to GrantedAuthority values.

Why this question is asked

LDAP, the Lightweight Directory Access Protocol, is how many companies store their users and groups, and Active Directory is its most common implementation. Spring Security offers two ways to check a password against a directory, and choosing the wrong one either fails outright against AD or weakens security. Interviewers ask this question to check that we understand what each method actually sends over the wire. In this answer, we will recap LDAP basics, explain bind authentication, then password comparison, configure Active Directory login, secure the connection, and map groups to authorities.

1A Quick Recap of LDAP

An LDAP directory is a tree of entries. Each entry has a distinguished name (DN), its full path in the tree, such as uid=anna,ou=people,dc=example,dc=com, and attributes such as cn, mail, memberOf and a password attribute. Clients talk to the directory over a connection on which they first bind, which means authenticate with a DN and password, and then search for entries.

Applications usually bind with a service account, a technical user with read permissions, to search for users and groups. The question is how to check the password of the person trying to log in.

2Bind Authentication: Let the Directory Check the Password

In bind authentication, Spring Security finds the user's DN (from a pattern or a search) and then opens a connection and binds as that user with the password they typed. If the directory accepts the bind, the password is correct. The directory applies its own rules: hash formats, account lockout, disabled accounts, expired passwords. Our application never reads a password hash.

Java
@Bean
AuthenticationManager ldapAuthenticationManager(BaseLdapPathContextSource contextSource) {
LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
factory.setUserDnPatterns("uid={0},ou=people"); // anna -> uid=anna,ou=people,dc=example,dc=com
return factory.createAuthenticationManager();
}
// bind as uid=anna,... with the typed password: success -> authenticated
// wrong password -> LDAP error 49 (invalid credentials) -> BadCredentialsException

This is the method to use almost always. It works with any directory, respects the directory's policies, and needs no special permissions beyond what every user already has: the right to bind as themselves.

3Password Comparison: Why It Is Rarely Appropriate

In password comparison, Spring Security binds with the service account and then either reads the user's password attribute (userPassword) and compares it with an encoded form of the typed password, or asks the directory to compare a value. The PasswordComparisonAuthenticator implements this.

There are several problems. The service account needs permission to read or compare password attributes, which is a powerful permission to hand to an application. The application must encode the typed password exactly the way the directory stored it, so hash formats must match. The directory's own lockout and expiry rules may be bypassed, because the user never binds. And Active Directory does not let clients read its password attribute at all, so comparison is simply not possible there. It is occasionally used with an embedded test directory, but it is not a choice for production against AD.

Bind compared with password comparison
BindPassword comparison
Who checks the passwordthe directorythe application or a compare operation
Permissions neededthe user binds as themselvesservice account must read or compare passwords
Hash formatdirectory's concernapplication must match it
Lockout and expiryenforced by the directorymay be bypassed
Works with Active Directoryyesno: the password attribute is not readable

4Logging In Against Active Directory

Active Directory has its own conventions, so Spring Security provides ActiveDirectoryLdapAuthenticationProvider. It binds with the user principal name, user@domain such as anna@corp.example.com, so we do not need to know where in the tree the user lives. After the bind it searches for the user's entry and reads the memberOf attribute to find their groups. It also translates AD's detailed error codes, so we can tell a wrong password from a locked or expired account.

Java
@Bean
ActiveDirectoryLdapAuthenticationProvider adProvider() {
var provider = new ActiveDirectoryLdapAuthenticationProvider(
"corp.example.com", // domain: anna -> anna@corp.example.com
"ldaps://dc1.corp.example.com:636"); // LDAPS, never plain ldap://
provider.setConvertSubErrorCodesToExceptions(true); // locked account -> LockedException, and so on
return provider;
}
// anna / correct password -> authenticated, authorities from memberOf: [Finance-Users, VPN-Users]

Notice that no service account is needed for the login itself: the user's own bind gives the permission to read their own entry.

5Always Encrypt the Connection

A simple bind sends the password over the connection as it is. On plain LDAP, port 389 without StartTLS, anyone who can see the traffic reads every password. So we use LDAPS, LDAP over TLS on port 636, or StartTLS, which upgrades a port 389 connection to TLS before the bind. The JVM must trust the directory's certificate, usually issued by an internal certificate authority that we add to the truststore. Many AD environments now require signing or encryption for LDAP binds anyway, and an application on plain LDAP will start failing when that policy is enforced.

6Mapping Directory Groups to Authorities

Spring Security decides access with GrantedAuthority values, such as ROLE_ADMIN. Directory groups have names like CN=Finance-Admins,OU=Groups,DC=corp,DC=example,DC=com. Something has to translate. For generic LDAP, DefaultLdapAuthoritiesPopulator searches a group base for groups that contain the user and turns each group name into an authority. For AD, the provider reads memberOf and produces authorities from the group names, and we can add a GrantedAuthoritiesMapper to turn directory names into the application's roles.

Java
@Bean
GrantedAuthoritiesMapper adGroupsToRoles() {
Map<String, String> map = Map.of("Finance-Admins", "ROLE_ADMIN", "Finance-Users", "ROLE_USER");
return authorities -> authorities.stream()
.map(a -> map.get(a.getAuthority()))
.filter(Objects::nonNull)
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toSet());
}
// memberOf: Finance-Admins, VPN-Users -> [ROLE_ADMIN] (VPN-Users is not relevant here)

Mapping explicitly keeps the application independent of the directory's naming and ignores the dozens of unrelated groups a typical AD user belongs to. The provider accepts the mapper through setAuthoritiesMapper().

7Follow-up questions

Can bind authentication use a pooled connection?

The service-account connection used for searches can be pooled, but the user's bind should use a fresh connection, because after binding the connection is authenticated as that user. Spring Security handles this by binding on a separate, non-pooled context.

What about nested groups in Active Directory?

memberOf lists only direct memberships. If roles depend on groups inside groups, we need either a search using AD's matching-rule-in-chain filter or the tokenGroups attribute, both of which have performance costs to keep in mind.

Should a new application still use LDAP directly?

Often not. A central identity provider federated with AD gives the application OIDC login with MFA and single sign-on, and the application never sees a password. Direct LDAP bind remains reasonable for internal tools where that is not available.

8Conclusion

In this answer, we saw that bind authentication lets the directory check the password by binding as the user, while password comparison reads or compares the password attribute, needs powerful permissions and matching hashes, bypasses directory policies and does not work with Active Directory. For AD we use ActiveDirectoryLdapAuthenticationProvider, which binds as user@domain and reads memberOf, always over LDAPS or StartTLS, and we map directory groups to the application's authorities.

What to say in the interviewAbout 90 seconds aloud

Open with

Bind authentication means Spring Security opens a connection and binds to the directory as the user, with the password they typed. If the directory accepts the bind, the password is right, and the directory enforces its own rules like lockout and expiry; the application never touches a hash.

Then, if they want more

Password comparison means Spring binds as a service account and then reads the user's password attribute and compares it with an encoded form of the typed password, or uses a compare operation.

That needs a service account with permission to read or compare passwords, the hash formats must match, directory policies can be bypassed, and against Active Directory it does not work at all, because AD does not expose the password attribute. So against AD I use bind, specifically ActiveDirectoryLdapAuthenticationProvider.

I give it the domain and the URL, it binds as user@domain, so I do not need to know the user's DN, then reads memberOf for groups and translates AD's error codes into exceptions like locked or expired account.

The connection must be LDAPS on 636 or StartTLS, because a simple bind sends the password as it is, and the JVM must trust the directory's certificate. Finally I map AD group names to application roles with an authorities mapper, or with DefaultLdapAuthoritiesPopulator for generic LDAP, ignoring unrelated groups.

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 Security 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 "Compare LDAP bind authentication and password comparison in Spring Security. Which would...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/1489a368e9/