Example usage for org.springframework.security.ldap SpringSecurityLdapTemplate compare

List of usage examples for org.springframework.security.ldap SpringSecurityLdapTemplate compare

Introduction

In this page you can find the example usage for org.springframework.security.ldap SpringSecurityLdapTemplate compare.

Prototype

public boolean compare(final String dn, final String attributeName, final Object value) 

Source Link

Document

Performs an LDAP compare operation of the value of an attribute for a particular directory entry.

Usage

From source file:org.apache.ranger.service.PasswordComparisonAuthenticator.java

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;//w  ww  . jav a2  s.  c o m
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();

    Iterator dns = getUserDns(username).iterator();

    SpringSecurityLdapTemplate ldapTemplate = new SpringSecurityLdapTemplate(getContextSource());

    while (dns.hasNext() && user == null) {
        final String userDn = (String) dns.next();

        try {
            user = ldapTemplate.retrieveEntry(userDn, getUserAttributes());
        } catch (NameNotFoundException ignore) {
        }
    }

    if (user == null && getUserSearch() != null) {
        user = getUserSearch().searchForUser(username);
    }

    if (user == null) {
        throw new UsernameNotFoundException("User not found: " + username, username);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Performing LDAP compare of password attribute '" + passwordAttributeName + "' for user '"
                + user.getDn() + "'");
    }

    String encodedPassword = passwordEncoder.encodePassword(password, null);
    byte[] passwordBytes = encodedPassword.getBytes();

    if (!ldapTemplate.compare(user.getDn().toString(), passwordAttributeName, passwordBytes)) {
        throw new BadCredentialsException(
                messages.getMessage("PasswordComparisonAuthenticator.badCredentials", "Bad credentials"));
    }

    return user;
}

From source file:org.cloudfoundry.identity.uaa.ldap.PasswordComparisonAuthenticator.java

public DirContextOperations searchAuthenticate(DirContextOperations user, byte[] passwordBytes,
        SpringSecurityLdapTemplate ldapTemplate) {
    if (logger.isDebugEnabled()) {
        logger.debug("Performing LDAP compare of password attribute '" + passwordAttributeName + "' for user '"
                + user.getDn() + "'");
    }/*w w  w  . j a  v  a 2s  . c  om*/

    if (!ldapTemplate.compare(user.getDn().toString(), passwordAttributeName, passwordBytes)) {
        throw new BadCredentialsException(
                messages.getMessage("PasswordComparisonAuthenticator.badCredentials", "Bad credentials"));
    }

    return user;
}

From source file:org.springframework.security.ldap.authentication.PasswordComparisonAuthenticator.java

private boolean isLdapPasswordCompare(DirContextOperations user, SpringSecurityLdapTemplate ldapTemplate,
        String password) {//from w  w  w . ja  va  2  s .  co m
    String encodedPassword = passwordEncoder.encode(password);
    byte[] passwordBytes = Utf8.encode(encodedPassword);
    return ldapTemplate.compare(user.getDn().toString(), passwordAttributeName, passwordBytes);
}