0

我想使用信任库文件通过 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 连接时一样。

4

1 回答 1

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);
于 2015-06-11T09:13:00.127 回答