I'm trying to figure out how Spring LDAP (not the Spring security thing) works by setting up the most basic working program, but it seems that the actual authentication breaks.
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();
}
});
}
}
}
Spring LDAP is a library to simplify LDAP programming in Java, built on the same principles as Spring Jdbc. The LdapTemplate class encapsulates all the plumbing work involved in traditional LDAP programming, such as creating, looping through NamingEnumerations, handling Exceptions and cleaning up resources.
The Lightweight Directory Access Protocol (LDAP) defines a way for clients to send requests and receive responses from directory services. We call a directory service using this protocol an LDAP server. The data served by an LDAP server is stored in an information model based on X. 500.
LDAP (Lightweight Directory Access Protocol) It is an open application protocol for maintaining and accessing distributed directory information services over an Internet Protocol.
I had very a similar Problem - also with NullPointerException
.
What solved my Problem was a call of 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);
// ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With