I need to use multiple LDAP provider. How can I check LDAP server availability?

瘦欲@ 提交于 2020-06-15 21:22:27

问题


We have multiple LDAP/domain servers.(ex. LDAP://server1.com:389/DC=server1,DC=COM, LDAP://server2.com:389/DC=server2,DC=COM) I need to use one of them by checking availabilty.

try {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "LDAP://server1.com:389/DC=server1,DC=COM");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, username);
    env.put(Context.SECURITY_CREDENTIALS, password);

    DirContext ctx = new InitialDirContext(env);
} catch(NamingException ex) {
}

回答1:


You can perform a simple anonymous search to see if the LDAP server is up and running. If you get a Connection refused exception, then the server would be down and you can switch to the next one in your list, perform the same operation again.

Usually there would be load balancers sitting in front of a string of domain controllers, the requests are routed to the load balancer which would identify the right DC and proxies the request to it. The LB takes care of periodically checking the availability of DCs in its list, remove them if they went down, check them back again when they are on etc.,. You may want to check with your IT department about existence of such an LB in your environment.




回答2:


You can just use multiple ldap server URLs in the PROVIDER_URL environment property like this:

Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.jndi.ldap.LdapCtxFactory");

// Specify list of space-separated URLs
env.put(Context.PROVIDER_URL, 
    "ldap://notthere:389/o=JNDITutorial " +
    "ldap://localhost:389/o=JNDITutorial " + 
    "ldap://remotehost/o=JNDITutorial " +
    "ldap://thirdhost:389/o=JNDITutorial");

// Create initial context
DirContext ctx = new InitialDirContext(env);

// See which server was used
System.out.println(ctx.getEnvironment().get(Context.PROVIDER_URL));

// do something useful with ctx
....

Whichever URL is successful, that will be used in the context



来源:https://stackoverflow.com/questions/14459280/i-need-to-use-multiple-ldap-provider-how-can-i-check-ldap-server-availability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!