java Spring LDAP 基本用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12297044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring LDAP basic usage
提问by damd
I'm trying to figure out how Spring LDAP (notthe Spring security thing) works by setting up the most basic working program, but it seems that the actual authentication breaks.
我试图通过设置最基本的工作程序来弄清楚 Spring LDAP(不是Spring 安全性的东西)是如何工作的,但似乎实际的身份验证中断了。
This is the error I get:
这是我得到的错误:
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)
The code that's executed in the method that's throwing the exception is:
在抛出异常的方法中执行的代码是:
return getContext(authenticationSource.getPrincipal(),
authenticationSource.getCredentials());
So it seems like I need to set up an authentication source in the application context? I'm really lost.
所以看起来我需要在应用程序上下文中设置一个身份验证源?我真的很失落。
Here's my code:
这是我的代码:
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();
}
});
}
}
}
采纳答案by pap
It looks proper, on the surface. One thing, your userDn is not really a proper distinguished name. It ought to be on the format "CN=<...>, DC=bar, DC=test, DC=foo
". Since you give no details on which LDAP server you are using or how your directory structure looks (OU structure etc) it's hard to be more precise.
从表面上看,它看起来不错。一件事,您的 userDn 并不是真正的专有名称。它应该是格式“ CN=<...>, DC=bar, DC=test, DC=foo
”。由于您没有提供有关您正在使用的 LDAP 服务器或目录结构的外观(OU 结构等)的详细信息,因此很难更准确。
回答by ollo
I had very a similar Problem - also with NullPointerException
.
我有一个非常相似的问题 - 也与NullPointerException
.
What solved my Problem was a call of afterPropertiesSet():
解决我的问题的是调用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);
// ...