Search in sources :

Example 1 with SpringSecurityLdapTemplate

use of org.springframework.security.ldap.SpringSecurityLdapTemplate in project spring-security by spring-projects.

the class PasswordComparisonAuthenticator method authenticate.

// ~ Methods
// ========================================================================================================
public DirContextOperations authenticate(final Authentication authentication) {
    Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication, "Can only process UsernamePasswordAuthenticationToken objects");
    // locate the user and check the password
    DirContextOperations user = null;
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();
    SpringSecurityLdapTemplate ldapTemplate = new SpringSecurityLdapTemplate(getContextSource());
    for (String userDn : getUserDns(username)) {
        try {
            user = ldapTemplate.retrieveEntry(userDn, getUserAttributes());
        } catch (NameNotFoundException ignore) {
        }
        if (user != null) {
            break;
        }
    }
    if (user == null && getUserSearch() != null) {
        user = getUserSearch().searchForUser(username);
    }
    if (user == null) {
        throw new UsernameNotFoundException("User not found: " + username);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Performing LDAP compare of password attribute '" + passwordAttributeName + "' for user '" + user.getDn() + "'");
    }
    if (usePasswordAttrCompare && isPasswordAttrCompare(user, password)) {
        return user;
    } else if (isLdapPasswordCompare(user, ldapTemplate, password)) {
        return user;
    }
    throw new BadCredentialsException(messages.getMessage("PasswordComparisonAuthenticator.badCredentials", "Bad credentials"));
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) DirContextOperations(org.springframework.ldap.core.DirContextOperations) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 2 with SpringSecurityLdapTemplate

use of org.springframework.security.ldap.SpringSecurityLdapTemplate in project spring-security by spring-projects.

the class FilterBasedLdapUserSearch method searchForUser.

// ~ Methods
// ========================================================================================================
/**
	 * Return the LdapUserDetails containing the user's information
	 *
	 * @param username the username to search for.
	 *
	 * @return An LdapUserDetails object containing the details of the located user's
	 * directory entry
	 *
	 * @throws UsernameNotFoundException if no matching entry is found.
	 */
public DirContextOperations searchForUser(String username) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching for user '" + username + "', with user search " + this);
    }
    SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);
    template.setSearchControls(searchControls);
    try {
        return template.searchForSingleEntry(searchBase, searchFilter, new String[] { username });
    } catch (IncorrectResultSizeDataAccessException notFound) {
        if (notFound.getActualSize() == 0) {
            throw new UsernameNotFoundException("User " + username + " not found in directory.");
        }
        // rethrow
        throw notFound;
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException)

Example 3 with SpringSecurityLdapTemplate

use of org.springframework.security.ldap.SpringSecurityLdapTemplate in project spring-security by spring-projects.

the class ApacheDSEmbeddedLdifTests method setUp.

@Before
public void setUp() throws Exception {
    // TODO: InMemoryXmlApplicationContext would be useful here, but it is not visible
    this.server = new ApacheDSContainer(LDAP_ROOT, "classpath:test-server-custom-attribute-types.ldif");
    this.server.setPort(LDAP_PORT);
    this.server.afterPropertiesSet();
    this.ldapTemplate = new SpringSecurityLdapTemplate(createLdapContextSource());
}
Also used : SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) Before(org.junit.Before)

Example 4 with SpringSecurityLdapTemplate

use of org.springframework.security.ldap.SpringSecurityLdapTemplate in project OpenClinica by OpenClinica.

the class LdapUserService method init.

// Eclipse warning here is an Eclipse bug, not an issue with the code
@PostConstruct
public void init() {
    ldapTemplate = new SpringSecurityLdapTemplate(contextSource);
    ldapTemplate.setIgnorePartialResultException(true);
}
Also used : SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) PostConstruct(javax.annotation.PostConstruct)

Example 5 with SpringSecurityLdapTemplate

use of org.springframework.security.ldap.SpringSecurityLdapTemplate in project spring-security by spring-projects.

the class LdapUserDetailsManagerTests method setUp.

@Before
public void setUp() throws Exception {
    mgr = new LdapUserDetailsManager(getContextSource());
    template = new SpringSecurityLdapTemplate(getContextSource());
    DirContextAdapter ctx = new DirContextAdapter();
    ctx.setAttributeValue("objectclass", "organizationalUnit");
    ctx.setAttributeValue("ou", "test people");
    template.bind("ou=test people", ctx, null);
    ctx.setAttributeValue("ou", "testgroups");
    template.bind("ou=testgroups", ctx, null);
    DirContextAdapter group = new DirContextAdapter();
    group.setAttributeValue("objectclass", "groupOfNames");
    group.setAttributeValue("cn", "clowns");
    group.setAttributeValue("member", "cn=nobody,ou=test people,dc=springframework,dc=org");
    template.bind("cn=clowns,ou=testgroups", group, null);
    group.setAttributeValue("cn", "acrobats");
    template.bind("cn=acrobats,ou=testgroups", group, null);
    mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=test people", "uid"));
    mgr.setGroupSearchBase("ou=testgroups");
    mgr.setGroupRoleAttributeName("cn");
    mgr.setGroupMemberAttributeName("member");
    mgr.setUserDetailsMapper(new PersonContextMapper());
}
Also used : SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) DefaultLdapUsernameToDnMapper(org.springframework.security.ldap.DefaultLdapUsernameToDnMapper) Before(org.junit.Before)

Aggregations

SpringSecurityLdapTemplate (org.springframework.security.ldap.SpringSecurityLdapTemplate)5 Before (org.junit.Before)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 PostConstruct (javax.annotation.PostConstruct)1 IncorrectResultSizeDataAccessException (org.springframework.dao.IncorrectResultSizeDataAccessException)1 NameNotFoundException (org.springframework.ldap.NameNotFoundException)1 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)1 DirContextOperations (org.springframework.ldap.core.DirContextOperations)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 DefaultLdapUsernameToDnMapper (org.springframework.security.ldap.DefaultLdapUsernameToDnMapper)1