Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6092341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:25:52+00:00 2026-05-23T12:25:52+00:00

How to get List of LDAP servers given a domain name (using java +

  • 0

How to get List of LDAP servers given a domain name (using java + acitvedirectory) , and authenticate it based on username and password ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:25:52+00:00Added an answer on May 23, 2026 at 12:25 pm

    Here are the steps to do it

    GET Server List

    1. Hit DNS server to get SRV records
    2. Sort SRV records
    3. Filter server record based on regexp pattern
    4. Construct LDAP urls based on if we need ssl or not. Note that srv record can return only one port so dont rely on port returned from srv record. See srv record RFC

    Authentication

    1. iterate through list of servers
      1. Construct Hashtable with ldap environments. note that need to append domain name with username separated by \, and add ldap URL to it
      2. try to create InitialDirContext
      3. success : close context and return
      4. failure1 : if naming exception is AuthenticationException and contains messge “[LDAP: error code 49” translate and throw a readable exception ! see error code mapping
      5. failure2 : if not failure1 continue with next ldap URL

    And Here is the full fledged working code

    If you are using spring use following conf to invoke init method

    <bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass">
            <value>org.bhate.ldap.LdapUtil</value>
        </property>
        <property name="targetMethod" value="init" />
        <property name="arguments">
            <list>
                <value>${ldap.dnsServer}</value>
                <value>${ldap.domainName}</value>
                <value>${ldap.filter.regexp}</value>
                <value>${ldap.ssl}</value>
            </list>         
        </property>
    

    And Following properties

    ldap.dnsServer=uk.mydomain.com
    ldap.domainName=DOMAINNAME
    ldap.filter.regexp=serv10.*|server.*
    ldap.ssl=true
    

    else call init during startup

    LdapUtil.init("uk.mydomain.com", "DOMAINNAME", "serv10.*|server.*",true);
    

    LdapUtil.java

    package org.bhate.ldap;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Hashtable;
    import java.util.LinkedHashSet;
    import java.util.Map;
    import java.util.TreeSet;
    import java.util.regex.Pattern;
    
    import javax.naming.AuthenticationException;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import javax.naming.directory.Attribute;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.DirContext;
    import javax.naming.directory.InitialDirContext;
    import javax.naming.spi.NamingManager;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.core.NestedExceptionUtils;
    import org.springframework.security.BadCredentialsException;
    
    
    public class LdapUtil {
    
        private static final Logger LOGGER = LoggerFactory
                .getLogger(LdapUtil.class);
    
        @SuppressWarnings("serial")
        final static Map<String, String> errorCodesMap = new HashMap<String, String>() {
            {
                put("525", "user not found");
                put("52e", "invalid credentials");
                put("530", "not permitted to logon at this time");
                put("531", "not permitted to logon at this workstation");
                put("532", "password expired");
                put("533", "account disabled");
                put("701", "account expired");
                put("773", "user must reset password");
                put("775", "user account locked");
            }
        };
    
        private static Collection<String> ldapServers;
        private static String domainName;
        private static Pattern PATTERN;
    
        public static DirContext getDirContext(String url, String domainName,
                String userName, String password) throws NamingException {
            Hashtable<String, Object> env = getEnv(url, domainName, userName,
                    password);
            return new InitialDirContext(env);
        }
    
        public static Collection<String> getLdapServers(String ldapDomain,
                boolean useSsl) throws NamingException {
            Collection<String> serverRecords = getSRVRecords(ldapDomain);
            serverRecords = reOrder(serverRecords);
            Collection<String> serverNames = new LinkedHashSet<String>();
            String protocol = "ldap" + (useSsl ? 's' : "");
            for (String s : serverRecords) {
                String hostName = s.substring(s.lastIndexOf(' ') + 1,
                        s.length() - 1);
                serverNames.add(protocol + "://" + hostName);
            }
            return serverNames;
        }
    
        private static Collection<String> reOrder(Collection<String> serverRecords) {
            return serverRecords;
        }
    
        private static Collection<String> getSRVRecords(String ldapDomain)
                throws NamingException {
            DirContext context = (DirContext) NamingManager.getURLContext("dns",
                    new Hashtable<String, Object>());
            String ldapDNSUrl = "dns:///_ldap._tcp." + ldapDomain;
            String[] attrIds = { "SRV" };
            Attributes attributes = context.getAttributes(ldapDNSUrl, attrIds);
            Attribute servers = attributes.get("SRV");
            int L = servers.size();
            Collection<String> serverRecords = new TreeSet<String>();
            for (int i = 0; i < L; i++) {
                String s = (String) servers.get(i);
                if (PATTERN.matcher(s).find())
                    serverRecords.add(s);
            }
            return serverRecords;
        }
    
        public static void authenticate(String userName, String password) {
            String msg = "Unable authenticate user {} with {}";
            for (String url : ldapServers) {
                DirContext ctx = null;
                try {
                    ctx = getDirContext(url, domainName, userName, password);
                    LOGGER
                            .info("Authenticated user {} on server {}", userName,
                                    url);
                    return;
                } catch (NamingException e) {
                    LOGGER.error(msg, userName, url);
                    String m = NestedExceptionUtils.buildMessage(e.getMessage(), e
                            .getCause());
                    LOGGER.error(m, e);
                    if (e instanceof AuthenticationException
                            && e.getMessage().startsWith("[LDAP: error code 49"))
                        throwMeanigfulEx(userName, url, e);
                } finally {
                    close(ctx);
                }
            }
            LOGGER.error(msg, userName, "any available server");
            throw new BadCredentialsException(
                    "Unable to authenticate : Please contact application support team");
        }
    
        public static void init(String dnsServerName, String domainName,
                String serverFilter, boolean useSsl) {
            PATTERN = Pattern.compile(serverFilter);
            try {
                LdapUtil.domainName=domainName;
                ldapServers = getLdapServers(dnsServerName, useSsl);
    
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(
                            "LDAP servers available in domain {} to connect {}",
                            dnsServerName, ldapServers);
                }
            } catch (NamingException e) {
                throw new RuntimeException("Unable retrieve ldapServers for "
                        + dnsServerName, e);
            }
        }
    
        private static void throwMeanigfulEx(String userName, String url,
                NamingException e) {
            String separator = ", data ";
            String m = e.getMessage();
            int strt = m.lastIndexOf(separator) + separator.length();
            int end = m.lastIndexOf(", vece");
            String code = m.substring(strt, end);
            throw new BadCredentialsException("Unable to authenticate : "
                    + errorCodesMap.get(code));
        }
    
        private static void close(DirContext ctx) {
            if (ctx != null)
                try {
                    ctx.close();
                } catch (NamingException e) {
                    LOGGER.error("Unable to close context", e);
                }
        }
    
        private static Hashtable<String, Object> getEnv(String url,
                String domainName, String userName, String password) {
            Hashtable<String, Object> env = new Hashtable<String, Object>();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                    "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.REFERRAL, "follow");
            env.put(Context.PROVIDER_URL, url);
            env.put("com.sun.jndi.ldap.connect.timeout", "2000");
            // env.put("com.sun.jndi.ldap.trace.ber", System.err);
            // env.put("javax.net.ssl.trustStoreType", "JKS");
            // env.put(Context.SECURITY_PROTOCOL, "ssl");
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, getPrincipal(domainName, userName));
            env.put(Context.SECURITY_CREDENTIALS, password);
            return env;
        }
    
        private static Object getPrincipal(String domainName, String userName) {
            return domainName + "\\" + userName;
        }
    
    
        public static void main(String[] args) throws NamingException {
            init("uk.mydomain.com", "DOMAINNAME", "serv10.*|server.*",true);
            System.out.println(ldapServers.size());
            System.out.println(ldapServers);
            if (args.length == 2)
                authenticate(args[0], args[1]);
        }
    
    
        //
        // public static InitialLdapContext getLdapContext(String userName,
        // String domainName, String password, String url)
        // throws NamingException {
        // Hashtable<String, Object> env = getEnv(url, domainName, userName,
        // password);
        // Control[] connCtls = new Control[] { new Control() {
        // private static final long serialVersionUID = 1L;
        //
        // public byte[] getEncodedValue() {
        // return null;
        // }
        //
        // public String getID() {
        // return "1.2.840.113556.1.4.1781";
        // }
        //
        // public boolean isCritical() {
        // return true;
        // }
        // } };
        // return new InitialLdapContext(env, connCtls);
        // }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get list of Domino Servers on the network, and accordingly login
I have performed an LDAP:// query to get a list of computers within a
I am attempting to authenticate users against an existing LDAP server (Active-Directory) using the
I am using python-ldap to try to authenticate against an existing Active Directory, and
How can I get a list of users within an LDAP group, even if
I'm using readlines method from python to get list of all data lines. Now
I have a code to get the list of OUs within a domain. Now
How can I get list of all tables in SQL Server database using query.
I want to get list of Sites and Lists from Sharepoint using Web Services.
We're using LDAP to authenticate users against a large Active Directory setup. We want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.