0
votes

I am using LdapTemplate to use LDAP authentication, I am able to succesfully find users in file system but authenticate method returns false and argues that my password is wrong.

I have debugged it and it succesfully finds the object it finds a match but password does not match.

dn: dc=obssjobs,dc=com
objectclass: top
objectclass: domain
objectclass: extensibleObject
dc: obssjobs

# Organizational Units
dn: ou=groups,dc=obssjobs,dc=com
objectclass: top
objectclass: organizationalUnit
ou: groups

dn: ou=people,dc=obssjobs,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people

# Create People
dn: uid=john,ou=people,dc=obssjobs,dc=com
objectclass: top
objectclass: hrexpert
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: John Doe
sn: John
uid: john
password: secret

dn: uid=jihn,ou=people,dc=obssjobs,dc=com
objectclass: top
objectclass: hrexpert
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Jihn Die
sn: Jihn
uid: jihn
password: secret

dn: uid=jahn,ou=people,dc=obssjobs,dc=com
objectclass: top
objectclass: hrexpert
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Jahn Dae
sn: Jahn
uid: jahn
password: 123

Authentication Method

public boolean authenticate(String username, String password) {
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectclass", "hrexpert")).and(new EqualsFilter("cn", username));
        return ldapTemplate.authenticate(DistinguishedName.EMPTY_PATH, filter.toString(), password);
    }

Here is my test in main, logs "false"

@PostConstruct
    public void setup(){
        boolean authenticated=hrExpertService.authenticate("Jahn Dae", "123");
        log.info("authenticated: " + authenticated);
    }
1

1 Answers

0
votes

Please try below code. Works for me:

    import com.novell.ldap.LDAPConnection;  //Import this 

   private LDAPConnection lc  = null;  
   private LDAPSocketFactory ssf; // If using secure ldap


  try {

            if(usingSecureldap){
                prepEnv(getSsl_keystore_url, getSsl_keystore_pass);

                if(ssf==null){
                    ssf = new LDAPJSSESecureSocketFactory();
                }

                lc = new LDAPConnection(ssf);

            }else{  // If not using secure ldap

                lc = new LDAPConnection();
            }



            lc.connect( <Ldap_ip>, <Ldap_port> );
            lc.bind( LDAPConnection.LDAP_V3, <username>+suffix, <password>.getBytes("UTF8") );

            return Boolean.TRUE;
        }
catch (LDAPException e) {return Boolean.FALSE;}


private void prepEnv(String keystorePath, String keystorePass) {
        System.setProperty(TRUSTSTORE_PROP_KEY, keystorePath);
        System.setProperty(PWD_TRUSTSTORE_PROP_KEY, keystorePass);
    }