SSL Ldap连接(ldaps)

时间:2013-08-29 09:54:07

标签: ssl connection ldap

我想使用truststore文件通过SSL连接到ldap。 我正在使用以下代码:

    private DirContext ctxtDir = null;
    Attributes attributes = null;
    ldap_server_url = "ldaps://" + getLdapHostName() + ":"
            + getPort() + "/";
    ldap_base_dn = getBaseDn();
    ldap_user = getLogin();
    ldap_password = getPwd();
    ldap_trust_store_file = "C:\\truststore.jks";
    ldap_trust_store_pwd = getStoreJKSPwd();

    // Set the parameters
    env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, ldap_context_factory);
    env.put(Context.PROVIDER_URL, ldap_server_url);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, ldap_user);
    env.put(Context.SECURITY_CREDENTIALS, ldap_password);
    env.put(Context.SECURITY_PROTOCOL, "SSL");

    // Set SSL parameters for Ldaps connection
    System.setProperty("javax.net.ssl.trustStore", ldap_trust_store_file);
    System.setProperty("javax.net.ssl.trustStorePassword",
            ldap_trust_store_pwd);
            // Try to establish the connection
    try {
        // create initial context
        ctxtDir = new InitialDirContext(env);
        attributes = getLdapattributes(ldap_base_dn);
        if (null != attributes) {
            isAvailable = true;
        }
    } catch (Exception e) {
        isAvailable = false;

    }

问题是我不想使用信任库文件的位置,我想使用输入流(文件内容),有没有办法做到这一点?比如使用SSLContext来建立一个https连接。

1 个答案:

答案 0 :(得分:0)

Unbound Ldap SDK是最新的LDAP API。它还提供SSLSocketFactory来建立SSL连接。

TrustAllTrustManager manager = new TrustAllTrustManager();
        SSLUtil sslUtil = new SSLUtil(manager);
        SSLSocketFactory socketFactory;
        try {
            socketFactory = sslUtil.createSSLSocketFactory("TLSv1");
        }
        catch (GeneralSecurityException e) {
            throw new LDAPException(ResultCode.CANCELED, "An error occured while creating SSL socket factory.");
        }

并将此socketFactory用作

new RoundRobinServerSet(addressesArray, portsArray, socketFactory);