Thoughts on systems, software, and what actually matters.
Java: Sample Active Directory authentication code

Java: Sample Active Directory authentication code

Here is a sample Java code to authenticate against Windows Active Directory server. The code finds all available active directory servers in your network. It uses one of the available active directory server for authentication. If an active directory server is down then it starts using next available server if available.

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.util.Hashtable;

public class ActiveDirectoryAuth {
    
    public boolean authenticate(String username, String password) {
        Hashtable env = new Hashtable<>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, 
                "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://your-ad-server:389");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, username);
        env.put(Context.SECURITY_CREDENTIALS, password);
        
        try {
            DirContext ctx = new InitialDirContext(env);
            ctx.close();
            return true;
        } catch (NamingException e) {
            return false;
        }
    }
}

This is a basic example. In production, you should use SSL and handle multiple AD servers for failover.