2012-09-06 24 views
5

我想弄清楚Spring LDAP(不是 Spring安全事物)是如何工作的,通过设置最基本的工作程序,但似乎实际的认证中断了。春季LDAP基本用法

这是错误我得到:

 
Exception in thread "main" java.lang.NullPointerException 
    at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:125) 
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:287) 
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:237) 
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:588) 
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:546) 
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:401) 
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:421) 
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:441) 

是在这的投掷异常的方法执行的代码是:

return getContext(authenticationSource.getPrincipal(), 
        authenticationSource.getCredentials()); 

所以好像我需要建立一个认证源在应用程序上下文中?我真的迷失了。

这里是我的代码:

package se.test.connector.ldap; 

import java.util.List; 
import javax.naming.NamingException; 
import javax.naming.directory.Attributes; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.ldap.core.AttributesMapper; 
import org.springframework.ldap.core.DistinguishedName; 
import org.springframework.ldap.core.LdapTemplate; 
import org.springframework.ldap.core.support.LdapContextSource; 
import org.springframework.ldap.filter.EqualsFilter; 

public class LdapTest { 

    public static void main(String[] args) { 
     LdapContextSource ctxSrc = new LdapContextSource(); 
     ctxSrc.setUrl("ldap://<ldapUrl>:389"); 
     ctxSrc.setBase("DC=bar,DC=test,DC=foo"); 
     ctxSrc.setUserDn("<username>@bar.test.foo"); 
     ctxSrc.setPassword("<password>"); 

     LdapTemplate tmpl = new LdapTemplate(ctxSrc); 

     PersonDao dao = new PersonDao(tmpl); 
     dao.getAllPersonNames(); 
    } 

    public static class PersonDao { 

     private LdapTemplate ldapTemplate; 

     public PersonDao(LdapTemplate ldapTemplate) { 
      this.ldapTemplate = ldapTemplate; 
     } 

     public void setLdapTemplate(LdapTemplate ldapTemplate) { 
      this.ldapTemplate = ldapTemplate; 
     } 

     public List getAllPersonNames() { 
      EqualsFilter filter = new EqualsFilter("objectclass", "person"); 
      return ldapTemplate.search(DistinguishedName.EMPTY_PATH, 
        filter.encode(), 
        new AttributesMapper() { 

         public Object mapFromAttributes(Attributes attrs) throws NamingException { 
          return attrs.get("cn").get(); 
         } 
        }); 
     } 
    } 
} 
+1

给予好评,因为你的榜样让我有类似的问题,谢谢! :-) – ollo

回答

2

它看起来正确的,在表面上。有一件事,你的userDn并不是一个真正的专有名称。它应该是格式“CN=<...>, DC=bar, DC=test, DC=foo”。既然你没有提供关于你正在使用哪个LDAP服务器的细节,或者你的目录结构如何看起来(OU结构等),那么很难做到更精确。

+0

简介,我从来没有试图单独设置用户“基地”和uid。 尝试不设置基准并使用完全限定的DN设置UserDN。 –

21

我有一个非常类似的问题 - 也与NullPointerException

什么解决我的问题是的afterPropertiesSet()的电话:

// ... 

LdapContextSource ctxSrc = new LdapContextSource(); 
ctxSrc.setUrl("ldap://<ldapUrl>:389"); 
ctxSrc.setBase("DC=bar,DC=test,DC=foo"); 
ctxSrc.setUserDn("<username>@bar.test.foo"); 
ctxSrc.setPassword("<password>"); 

ctxSrc.afterPropertiesSet(); /* ! */ 

LdapTemplate tmpl = new LdapTemplate(ctxSrc); 

// ... 
+0

afterPropertiesSet()调用解决了我的问题,而我有相同的空指针异常 – waterazu

+1

这是真正的答案,如果你有一个NPE引起的authenticationSource == null – Kooki

+0

谢谢。我也忘了afterPropertiesSet()... – Peter