6

最も基本的な動作プログラムを設定することで、Spring LDAP(Springのセキュリティではない)がどのように機能するかを理解しようとしていますが、実際の認証は機能しないようです。

これは私が得るエラーです:

スレッド「メイン」の例外java.lang.NullPointerException
    org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:125)で
    org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:287)で
    org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:237)で
    org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:588)で
    org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:546)で
    org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:401)で
    org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:421)で
    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();
                        }
                    });
        }
    }
}
4

2 に答える 2

24

私は非常によく似た問題を抱えていました-これも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);

// ...
于 2012-12-05T17:24:09.870 に答える
2

表面上は適切に見えます。1 つ、あなたの userDn は実際には適切な識別名ではありません。" " の形式である必要がありますCN=<...>, DC=bar, DC=test, DC=foo。使用している LDAP サーバーや、ディレクトリ構造の外観 (OU 構造など) についての詳細を提供していないため、より正確にすることは困難です。

于 2012-09-06T11:35:18.600 に答える