Skip to content

Commit e95b733

Browse files
committed
Change FilterBasedLdapUserSearch to use LdapClient
Closes: gh-17290 Signed-off-by: Andrey Litvitski <[email protected]>
1 parent e37424c commit e95b733

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

ldap/src/main/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearch.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,22 @@
2323

2424
import org.springframework.core.log.LogMessage;
2525
import org.springframework.dao.IncorrectResultSizeDataAccessException;
26+
import org.springframework.ldap.core.ContextMapper;
2627
import org.springframework.ldap.core.ContextSource;
2728
import org.springframework.ldap.core.DirContextOperations;
29+
import org.springframework.ldap.core.LdapClient;
2830
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
31+
import org.springframework.ldap.query.LdapQuery;
32+
import org.springframework.ldap.query.LdapQueryBuilder;
2933
import org.springframework.security.core.userdetails.UsernameNotFoundException;
30-
import org.springframework.security.ldap.SpringSecurityLdapTemplate;
3134
import org.springframework.util.Assert;
3235

3336
/**
3437
* LdapUserSearch implementation which uses an Ldap filter to locate the user.
3538
*
3639
* @author Robert Sanders
3740
* @author Luke Taylor
41+
* @author Andrey Litvitski
3842
* @see SearchControls
3943
*/
4044
public class FilterBasedLdapUserSearch implements LdapUserSearch {
@@ -94,18 +98,22 @@ public FilterBasedLdapUserSearch(String searchBase, String searchFilter, BaseLda
9498
@Override
9599
public DirContextOperations searchForUser(String username) {
96100
logger.trace(LogMessage.of(() -> "Searching for user '" + username + "', with " + this));
97-
SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(this.contextSource);
98-
template.setSearchControls(this.searchControls);
101+
LdapClient ldapClient = LdapClient.builder()
102+
.contextSource(this.contextSource)
103+
.defaultSearchControls(() -> this.searchControls)
104+
.build();
105+
LdapQuery query = LdapQueryBuilder.query().base(this.searchBase).filter(this.searchFilter, username);
99106
try {
100-
DirContextOperations operations = template.searchForSingleEntry(this.searchBase, this.searchFilter,
101-
new String[] { username });
107+
DirContextOperations operations = ldapClient.search()
108+
.query(query)
109+
.toObject((ContextMapper<DirContextOperations>) (ctx) -> (DirContextOperations) ctx);
110+
if (operations == null) {
111+
throw UsernameNotFoundException.fromUsername(username);
112+
}
102113
logger.debug(LogMessage.of(() -> "Found user '" + username + "', with " + this));
103114
return operations;
104115
}
105116
catch (IncorrectResultSizeDataAccessException ex) {
106-
if (ex.getActualSize() == 0) {
107-
throw UsernameNotFoundException.fromUsername(username);
108-
}
109117
// Search should never return multiple results if properly configured
110118
throw ex;
111119
}

0 commit comments

Comments
 (0)