SSL Ldap 连接 (ldaps)

标签 ssl connection ldap

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

最佳答案

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

关于SSL Ldap 连接 (ldaps),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18507708/

相关文章:

ios - NSGenericException,原因 : [AVCapturePhotoOutput capturePhotoWithSettings:delegate:] No active and enabled video connection

asp.net - 为什么此代码会导致错误?

php - 跨域认证

java - 在 java 中使用 Ldap 获取组

ssl - 在 openssh 中是否可以明智地编辑 ssh/ssh_config 或 sshd_config 文件密码?

http - 如何在 Artifactory 中禁用 HTTP

java - 在 Java 中使用 keystore 进行 SSL 握手的正确设置是什么

node.js - 访问托管在不同端口的 WebSocket 服务器时出现混合内容错误

php - 从存在 id(来自另一个表)的一个表中选择

authentication - 如何告诉具有多个证书的 LDAP SSL 服务器返回我需要的证书?