This commit is contained in:
Andrey Litvitski 2025-07-01 12:15:12 +08:00 committed by GitHub
commit 3adbf5f835
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 8 deletions

View File

@ -23,11 +23,14 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.core.log.LogMessage; import org.springframework.core.log.LogMessage;
import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.LdapClient;
import org.springframework.ldap.core.support.BaseLdapPathContextSource; import org.springframework.ldap.core.support.BaseLdapPathContextSource;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.ldap.query.LdapQueryBuilder;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.ldap.SpringSecurityLdapTemplate;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -35,6 +38,7 @@ import org.springframework.util.Assert;
* *
* @author Robert Sanders * @author Robert Sanders
* @author Luke Taylor * @author Luke Taylor
* @author Andrey Litvitski
* @see SearchControls * @see SearchControls
*/ */
public class FilterBasedLdapUserSearch implements LdapUserSearch { public class FilterBasedLdapUserSearch implements LdapUserSearch {
@ -94,18 +98,22 @@ public class FilterBasedLdapUserSearch implements LdapUserSearch {
@Override @Override
public DirContextOperations searchForUser(String username) { public DirContextOperations searchForUser(String username) {
logger.trace(LogMessage.of(() -> "Searching for user '" + username + "', with " + this)); logger.trace(LogMessage.of(() -> "Searching for user '" + username + "', with " + this));
SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(this.contextSource); LdapClient ldapClient = LdapClient.builder()
template.setSearchControls(this.searchControls); .contextSource(this.contextSource)
.defaultSearchControls(() -> this.searchControls)
.build();
LdapQuery query = LdapQueryBuilder.query().base(this.searchBase).filter(this.searchFilter, username);
try { try {
DirContextOperations operations = template.searchForSingleEntry(this.searchBase, this.searchFilter, DirContextOperations operations = ldapClient.search()
new String[] { username }); .query(query)
.toObject((ContextMapper<DirContextOperations>) (ctx) -> (DirContextOperations) ctx);
if (operations == null) {
throw UsernameNotFoundException.fromUsername(username);
}
logger.debug(LogMessage.of(() -> "Found user '" + username + "', with " + this)); logger.debug(LogMessage.of(() -> "Found user '" + username + "', with " + this));
return operations; return operations;
} }
catch (IncorrectResultSizeDataAccessException ex) { catch (IncorrectResultSizeDataAccessException ex) {
if (ex.getActualSize() == 0) {
throw UsernameNotFoundException.fromUsername(username);
}
// Search should never return multiple results if properly configured // Search should never return multiple results if properly configured
throw ex; throw ex;
} }