LDAP模板身份验证使用正确的密码返回false

时间:2019-07-16 10:18:26

标签: java spring ldap spring-ldap

我正在使用LdapTemplate来使用LDAP身份验证,我能够成功在文件系统中找到用户,但是authenticate方法返回false并认为我的密码错误。

我已调试它,它成功找到了一个匹配的对象,但密码不匹配。

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

身份验证方法

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);
    }

这是我的主要测试,记录为“ false”

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

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。为我工作:

    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);
    }