Using ActiveDirectoryLdapDnsProvider
- Attention
- Contrary to previous statements, this library must reside in the system classpath for this provider to work properly and requires at least Java 8u272, Java 11.0.9, or Java 12+. It is a multi-release JAR because of changing interfaces between Java releases.
Usage Examples
No configuration is necessary, it will be autodiscovered through Java's ServiceLoader
. For the ease of use for these examples use my DirContextSource
, but you can go with plain JNDI if you prefer.
With Java
In few lines you have a usable DirContextSource
:
import net.sf.michaelo.dirctxsrc.DirContextSource; import javax.naming.directory.DirContext; […] DirContextSource.Builder builder = new DirContextSource.Builder("ldap://domain-name"); // Set the Active Directory site builder.additionalProperty("net.sf.michaelo.activedirectory.site", "debln-01"); // Make it fail fast builder.additionalProperty("dns.com.sun.jndi.dns.timeout.initial", "200") .additionalProperty("dns.com.sun.jndi.dns.timeout.retries", "2"); DirContextSource contextSource = builder.build(); // try and catch block omitted for the sake of brevity, // handle NamingException appropriately DirContext context = contextSource.getDirContext(); // Perform operations context.close(); […]
With a Servlet Container (Apache Tomcat)
Navigate in your server.xml
to /Server/GlobalNamingResources
and add the following element:
[…] <GlobalNamingResources> <!-- Add this --> <Resource name="ldap/name" type="net.sf.michaelo.dirctxsrc.DirContextSource" factory="net.sf.michaelo.dirctxsrc.DirContextSourceFactory" urls="ldap://domain-name" additionalProperties="net.sf.michaelo.activedirectory.site=debln-01;dns.com.sun.jndi.dns.timeout.initial=200;dns.com.sun.jndi.dns.timeout.retries=2" /> </GlobalNamingResources> […]
This resource still needs to be linked to your application. Open or create your app's context.xml
and add:
<Context> […] <!-- Add this --> <ResourceLink global="ldap/name" name="ldap/localName" type="net.sf.michaelo.dirctxsrc.DirContextSource" /> […] </Context>
Now you have successfully linked a global resource to your webapp. It is now ready to use.