首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LDAPConnection (org.apache.directory.ldap.client.api.LdapConnection)在ssl上失败

LDAPConnection (org.apache.directory.ldap.client.api.LdapConnection)在ssl上失败
EN

Stack Overflow用户
提问于 2014-08-26 19:49:33
回答 1查看 10.3K关注 0票数 3

我在尝试通过636连接到我的服务器并启用了ssl时出现错误。

我使用apache directory studio浏览Active directory,并通过端口636和ssl (ldaps://....)进行连接。

现在我得到了以下代码:

代码语言:javascript
复制
LdapConnection connection = new LdapNetworkConnection("172.16.1.8", 636, true);

但这并不起作用:

代码语言:javascript
复制
org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException: PROTOCOL_ERROR: The server will disconnect!
at org.apache.directory.api.ldap.model.message.ResultCodeEnum.processResponse(ResultCodeEnum.java:2163)
at org.apache.directory.ldap.client.api.AbstractLdapConnection.bind(AbstractLdapConnection.java:129)
at org.apache.directory.ldap.client.api.AbstractLdapConnection.bind(AbstractLdapConnection.java:112)
at ch.berufsbildungscenter.notiztool.control.Account.login(Account.java:123)
at ch.berufsbildungscenter.notiztool.control.Account.login(Account.java:100)
at ch.berufsbildungscenter.notiztool.gui.control.LoginController$2.run(LoginController.java:53)

有人知道为什么不这样做吗?

以下是登录函数:

代码语言:javascript
复制
/**
 * Checks the pw with the pw on the Active Directory.
 * 
 * @param username 
 * @param pw
 * @param b
 * 
 * @return true if login was successful, false if not.
 */
private static boolean login(String username, String pw, Berufsbildner b) {
    if(b == null)
        return false;
    String cn = b.getNachname() + " " + b.getVorname();
    //Create connection to the LDAP server
    @SuppressWarnings("resource")
    LdapConnection connection = new LdapNetworkConnection("172.16.1.8", 636, true);
    //try to bind with the login data
    try {
        //------------------ Here's the exception
        connection.bind("CN="+ cn +",OU=Ausbilder,OU=Informatiker,OU=Ascom Bern,OU=Berufsbildungscenter,DC=bbcnet,DC=ch", pw);
        loggedin = true;
        currentAccount = b;
    } catch (LdapException e) {
        e.printStackTrace();
        loggedin = false;
        return false;
    }
    return true;

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-27 19:42:25

使用此行设置SSL协议:

代码语言:javascript
复制
connection.setSslProtocol("SSLv3");

并将信任管理器设置为以下行:

代码语言:javascript
复制
connection.setTrustManagers(new CustomTtrustManager());

CutomTrustManager是通过实现X509TrustManager或任何类型的信任管理器来定义的信任管理器。例如:

代码语言:javascript
复制
public class CustomTtrustManager implements X509TrustManager
{
    public boolean isClientTrusted(X509Certificate[] cert)
    {
        return true;
    }

    public boolean isServerTrusted(X509Certificate[] cert)
    {
        try
        {
            cert[0].checkValidity();
            return true;
        }
        catch (CertificateExpiredException e)
        {
            return false;
        }
        catch (CertificateNotYetValidException e)
        {
            return false;
        }
    }

    public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
        throws CertificateException
    {
        // Do nothing for now.
    }

    public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
        throws CertificateException
    {
        // Do nothing for now.
    }

    public X509Certificate[] getAcceptedIssuers()
    {
        return new X509Certificate[0];
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25505141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档