1

I am trying to connect LDAP through java code but getting the following Exception. Can anybody tell me what I am missing? Here is my code-

 initialProperties = new Properties();
        initialProperties.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
        initialProperties.put(Context.PROVIDER_URL, providerURL);
        initialProperties.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
        initialProperties.put(Context.SECURITY_CREDENTIALS, securityCredentials);
        initialProperties.put(Context.SECURITY_AUTHENTICATION, authentication);
        try {
        context = new InitialDirContext(initialProperties);
        }
        catch(Exception e)
        {e.printStackTrace();}
1
  • The JavaDoc for java.net.ConnectException should be your first clue: Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely Commented May 15, 2019 at 9:44

1 Answer 1

1

I changed your code a little bit, and it works in mine.

public class LdapTest {

public static void main(String[] args) {

    Properties initialProperties = new Properties();
    initialProperties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    initialProperties.put(Context.PROVIDER_URL, "ldap://192.168.0.179:389");
    initialProperties.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
    initialProperties.put(Context.SECURITY_CREDENTIALS, "dirmanager");
    initialProperties.put(Context.SECURITY_AUTHENTICATION, "simple");

    try {
    InitialDirContext context = new InitialDirContext(initialProperties);
    System.out.println(context);
    }
    catch(Exception e)
    {e.printStackTrace();}
    }
}

Don't forget to start your ldap server first.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.