2012-09-06 78 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