dam*_*amd 6 java spring spring-ldap
我试图通过设置最基本的工作程序来弄清楚Spring LDAP(不是 Spring安全事件)是如何工作的,但似乎实际的身份验证中断了.
这是我得到的错误:
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());
Run Code Online (Sandbox Code Playgroud)
所以我似乎需要在应用程序上下文中设置身份验证源?我真的迷路了.
这是我的代码:
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();
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
oll*_*llo 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);
// ...
Run Code Online (Sandbox Code Playgroud)
表面上看起来很正常。有一件事,您的 userDn 并不是一个真正正确的专有名称。它的格式应该是“ CN=<...>, DC=bar, DC=test, DC=foo”。由于您没有提供有关您正在使用的 LDAP 服务器或目录结构的外观(OU 结构等)的详细信息,因此很难更准确。
| 归档时间: |
|
| 查看次数: |
11497 次 |
| 最近记录: |