A very light weight library to provide LDAP authentication.
Inspired by npm ldapauth package.
Intended to provide a much simpler API to lookup user details and authenticate user against a LDAP directory.
If you nee a more comprehensive library to deal with LDAP, have a look at Apache Directory or UboundID LDAP SDK.
<dependency> <groupId>net.hackergarten</groupId> <artifactId>ldapauth</artifactId> <version>1.0.2</version> <type>pom</type> </dependency>
To authenticate a user against your LDAP, you need to know your LDAP ServerName and the search base the users you want to allow to authenticate are located in your LDAP structure.
To do the actual authentication, you also need the full DN for the user to authenticate.
To get this from a single identifying attribute, you need to do a search first.
You can use setUidProperty
to define the property to be used for the lookup. Given this, the resulting code would look like:
LdapAuthenticator ldapAuthenticator = new LdapAuthenticator("dc=example,dc=com", "ldap://ldap.forumsys.com:389/");
ldapAuthenticator.setUidProperty("cn");
String dn = ldapAuthenticator.getDn("read-only-admin");
boolean authenticated = ldapAuthenticator.testBind(dn, "password");
To read some more information about the user, you can use the search
method:
link:src/test/java/net/hackergarten/ldapauth/LdapAuthenticator.java[role=include]
Please have a look at the unit tests (LdapAuthenticatorTest.java) for an example on how to use this library.