2014-05-15 142 views
3

我是新来的LDAP和我尝试了我认为是一个简单的例子来测试与LDAP例如,有人就已经春天LDAP模块设置测试。关于我使用的可以在这里找到LDAP实例javax.naming.AuthenticationException:LDAP:错误代码49 - 无效凭证]

详情: http://blog.stuartlewis.com/2008/07/07/test-ldap-service/comment-page-3/

我使用LDAP浏览器/管理工具(Softerra的LDAP管理),并没有任何问题,我可以访问该目录。

当我尝试它使用Java和弹簧LDAP(2.0.1),我得到上面提到的身份验证例外。在设置了我自己的LDAP实例,试图解决此进一步我想在这里检查的情况下,更有经验的人可以指出一些明显的,我错过了。

下面是我使用的代码:

import org.springframework.ldap.core.LdapTemplate; 
import org.springframework.ldap.core.support.LdapContextSource; 

import java.util.List; 

public class LdapTest { 


public List<String> getListing() { 

    LdapTemplate template = getTemplate(); 

    List<String> children = template.list("dc=testathon,dc=net"); 

    return children; 
} 


private LdapTemplate getTemplate(){ 

    LdapContextSource contextSource = new LdapContextSource(); 
    contextSource.setUrl("ldap://ldap.testathon.net:389"); 
    contextSource.setUserDn("cn=john"); 
    contextSource.setPassword("john"); 

    try { 
     contextSource.afterPropertiesSet(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 


    LdapTemplate template = new LdapTemplate(); 

    template.setContextSource(contextSource); 

    return template; 

} 


public static void main(String[] args){ 


    LdapTest sClient = new LdapTest(); 
    List<String> children = sClient.getListing(); 

    for (String child :children) { 
     System.out.println(child); 
    } 

} 

} 

堆栈跟踪:

Exception in thread "main" org.springframework.ldap.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]; nested exception is javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials] 
at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:191) 
at org.springframework.ldap.core.support.AbstractContextSource.createContext(AbstractContextSource.java:356) 
at org.springframework.ldap.core.support.AbstractContextSource.doGetContext(AbstractContextSource.java:140) 
+0

使用Spring对你来说是必须的吗? – vkg

回答

6

原来我只是需要以包括专有名称(包括组织单元)的一切。使用

contextSource.setBase(...); 

由于某种原因没有工作。进行纠正后,一切都很好。

contextSource.setUserDn("cn=john,ou=Users,dc=testathon,dc=net");