|
23 | 23 |
|
24 | 24 | import org.springframework.core.log.LogMessage;
|
25 | 25 | import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
| 26 | +import org.springframework.ldap.core.ContextMapper; |
26 | 27 | import org.springframework.ldap.core.ContextSource;
|
27 | 28 | import org.springframework.ldap.core.DirContextOperations;
|
| 29 | +import org.springframework.ldap.core.LdapClient; |
28 | 30 | import org.springframework.ldap.core.support.BaseLdapPathContextSource;
|
| 31 | +import org.springframework.ldap.query.LdapQuery; |
| 32 | +import org.springframework.ldap.query.LdapQueryBuilder; |
29 | 33 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
30 |
| -import org.springframework.security.ldap.SpringSecurityLdapTemplate; |
31 | 34 | import org.springframework.util.Assert;
|
32 | 35 |
|
33 | 36 | /**
|
34 | 37 | * LdapUserSearch implementation which uses an Ldap filter to locate the user.
|
35 | 38 | *
|
36 | 39 | * @author Robert Sanders
|
37 | 40 | * @author Luke Taylor
|
| 41 | + * @author Andrey Litvitski |
38 | 42 | * @see SearchControls
|
39 | 43 | */
|
40 | 44 | public class FilterBasedLdapUserSearch implements LdapUserSearch {
|
@@ -94,18 +98,22 @@ public FilterBasedLdapUserSearch(String searchBase, String searchFilter, BaseLda
|
94 | 98 | @Override
|
95 | 99 | public DirContextOperations searchForUser(String username) {
|
96 | 100 | 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); |
99 | 106 | 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 | + } |
102 | 113 | logger.debug(LogMessage.of(() -> "Found user '" + username + "', with " + this));
|
103 | 114 | return operations;
|
104 | 115 | }
|
105 | 116 | catch (IncorrectResultSizeDataAccessException ex) {
|
106 |
| - if (ex.getActualSize() == 0) { |
107 |
| - throw UsernameNotFoundException.fromUsername(username); |
108 |
| - } |
109 | 117 | // Search should never return multiple results if properly configured
|
110 | 118 | throw ex;
|
111 | 119 | }
|
|
0 commit comments