'Connecting to LDAP Using Java
I tried connecting to LDAP Server using below Java code, but code throws error:
can't create context error. The LDAP Server host is running seamlessly on Prod Server.
Is there anything that I need to correct here?
The stack trace is as follows:
Caused by: java.net.UnknownHostException: glue.systems.sa.hsbc
Analysis done:
- When i ping the same hostname from ping command, i get packets successfully.
ping glue.systems.sa.hsbc returns packets successfully
I Also converted hostname to ipaddress using socket api and connected. But still the same Exception(Unknown Host Exception)
public static void main(String[]args) { Hashtable<String, String> environment = new Hashtable<String, String>(); environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environment.put(Context.PROVIDER_URL, "ldap://glue.systems.sa.hsbc:3269"); environment.put(Context.SECURITY_AUTHENTICATION, "SIMPLE"); environment.put(Context.SECURITY_PRINCIPAL, "CN=HBSA-SVC-SIDEX,OU=Service Accounts,DC=InfoDir,DC=Prod,DC=HSBC"); environment.put(Context.SECURITY_CREDENTIALS, "LXltp9RrY98FVYkr3S/emg=="); environment.put(Context.SECURITY_PROTOCOL, "SSL"); try { DirContext context = new InitialDirContext(environment); System.out.println("Connected.."); System.out.println(context.getEnvironment()); context.close(); } catch (AuthenticationNotSupportedException exception) { System.out.println("The authentication is not supported by the server"); } catch (AuthenticationException exception) { System.out.println("Incorrect password or username"); } catch (NamingException exception) { System.out.println("Error when trying to create the context"); } }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
