mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-06 10:42:33 +00:00
Fixed potential problem with multiple userDn patterns.
This commit is contained in:
parent
f0b11109b4
commit
a7d7631f2f
@ -18,6 +18,8 @@ package org.acegisecurity.providers.ldap.authenticator;
|
|||||||
import org.acegisecurity.ldap.LdapUserInfo;
|
import org.acegisecurity.ldap.LdapUserInfo;
|
||||||
import org.acegisecurity.ldap.LdapUtils;
|
import org.acegisecurity.ldap.LdapUtils;
|
||||||
import org.acegisecurity.ldap.InitialDirContextFactory;
|
import org.acegisecurity.ldap.InitialDirContextFactory;
|
||||||
|
import org.acegisecurity.ldap.LdapTemplate;
|
||||||
|
import org.acegisecurity.ldap.AttributesMapper;
|
||||||
import org.acegisecurity.providers.encoding.PasswordEncoder;
|
import org.acegisecurity.providers.encoding.PasswordEncoder;
|
||||||
import org.acegisecurity.BadCredentialsException;
|
import org.acegisecurity.BadCredentialsException;
|
||||||
import org.acegisecurity.userdetails.UsernameNotFoundException;
|
import org.acegisecurity.userdetails.UsernameNotFoundException;
|
||||||
@ -32,6 +34,7 @@ import javax.naming.NamingException;
|
|||||||
import javax.naming.directory.SearchControls;
|
import javax.naming.directory.SearchControls;
|
||||||
import javax.naming.directory.DirContext;
|
import javax.naming.directory.DirContext;
|
||||||
import javax.naming.directory.Attribute;
|
import javax.naming.directory.Attribute;
|
||||||
|
import javax.naming.directory.Attributes;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
@ -82,26 +85,35 @@ public final class PasswordComparisonAuthenticator extends AbstractLdapAuthentic
|
|||||||
// locate the user and check the password
|
// locate the user and check the password
|
||||||
LdapUserInfo user = null;
|
LdapUserInfo user = null;
|
||||||
|
|
||||||
DirContext ctx = getInitialDirContextFactory().newInitialDirContext();
|
|
||||||
Iterator dns = getUserDns(username).iterator();
|
Iterator dns = getUserDns(username).iterator();
|
||||||
|
|
||||||
|
LdapTemplate ldapTemplate = new LdapTemplate(getInitialDirContextFactory());
|
||||||
|
|
||||||
|
while(dns.hasNext() && user == null) {
|
||||||
|
final String userDn = (String)dns.next();
|
||||||
|
|
||||||
|
if(ldapTemplate.nameExists(userDn)) {
|
||||||
|
AttributesMapper mapper = new AttributesMapper() {
|
||||||
|
public Object mapAttributes(Attributes attributes) {
|
||||||
|
return new LdapUserInfo(userDn, attributes);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
user = (LdapUserInfo)ldapTemplate.retrieveEntry(userDn, mapper, getUserAttributes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user == null && getUserSearch() != null) {
|
||||||
|
user = getUserSearch().searchForUser(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
throw new UsernameNotFoundException(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
DirContext ctx = getInitialDirContextFactory().newInitialDirContext();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while(dns.hasNext() && user == null) {
|
|
||||||
String userDn = (String)dns.next();
|
|
||||||
String relativeName = LdapUtils.getRelativeName(userDn, ctx);
|
|
||||||
|
|
||||||
user = new LdapUserInfo(userDn,
|
|
||||||
ctx.getAttributes(relativeName, getUserAttributes()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (user == null && getUserSearch() != null) {
|
|
||||||
user = getUserSearch().searchForUser(username);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (user == null) {
|
|
||||||
throw new UsernameNotFoundException(username);
|
|
||||||
}
|
|
||||||
|
|
||||||
Attribute passwordAttribute = user.getAttributes().get(passwordAttributeName);
|
Attribute passwordAttribute = user.getAttributes().get(passwordAttributeName);
|
||||||
|
|
||||||
if(passwordAttribute != null) {
|
if(passwordAttribute != null) {
|
||||||
|
@ -67,6 +67,11 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
|||||||
authenticator.authenticate("Bob", "bobspassword");
|
authenticator.authenticate("Bob", "bobspassword");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testMultipleDnPatternsWorkOk() {
|
||||||
|
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=nonexistent", "uid={0},ou=people"});
|
||||||
|
authenticator.authenticate("Bob", "bobspassword");
|
||||||
|
}
|
||||||
|
|
||||||
public void testLocalCompareSucceedsWithShaEncodedPassword() {
|
public void testLocalCompareSucceedsWithShaEncodedPassword() {
|
||||||
authenticator = new PasswordComparisonAuthenticator(getInitialCtxFactory());
|
authenticator = new PasswordComparisonAuthenticator(getInitialCtxFactory());
|
||||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user