java.net.ConnectException:连接被拒绝:连接到Ldap时

时间:2019-05-14 18:05:14

标签: java ldap

我正在尝试通过Java代码连接LDAP,但收到以下异常。谁能告诉我我想念的东西吗?这是我的代码-

 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 个答案:

答案 0 :(得分:1)

我稍微更改了您的代码,它在我的代码中有效。

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();}
    }
}

别忘了先启动您的ldap服务器。