在ssl(ldaps)的支持下连接活动目录

时间:2012-05-16 07:20:18

标签: java ssl active-directory keystore truststore

我正在尝试使用ssl。

的支持来连接活动目录

我尝试了以下网站的步骤。

http://confluence.atlassian.com/display/CROWD/Configuring+an+SSL+Certificate+for+Microsoft+Active+Directory

当我尝试从java代码连接活动目录时,它会出现以下错误。

Exception in thread "main" javax.naming.CommunicationException: simple bind fail
ed: 172.16.12.4:636 [Root exception is java.net.SocketException: Connection rese
t]
        at com.sun.jndi.ldap.LdapClient.authenticate(Unknown Source)
        at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
        at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
        at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
        at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
        at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
        at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
        at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
        at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
        at javax.naming.InitialContext.init(Unknown Source)
        at javax.naming.InitialContext.<init>(Unknown Source)
        at javax.naming.directory.InitialDirContext.<init>(Unknown Source)
        at ConnectActiveDirectory.main(ConnectActiveDirectory.java:39)
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at com.sun.net.ssl.internal.ssl.InputRecord.readFully(Unknown Source)
        at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)

        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.waitForClose(Unknown Sourc
e)
        at com.sun.net.ssl.internal.ssl.HandshakeOutStream.flush(Unknown Source)

        at com.sun.net.ssl.internal.ssl.Handshaker.kickstart(Unknown Source)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.kickstartHandshake(Unknown
 Source)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Un
known Source)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source
)
        at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source)
        at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
        at java.io.BufferedOutputStream.flush(Unknown Source)
        at com.sun.jndi.ldap.Connection.writeRequest(Unknown Source)
        at com.sun.jndi.ldap.Connection.writeRequest(Unknown Source)
        at com.sun.jndi.ldap.LdapClient.ldapBind(Unknown Source)
        ... 13 more

正在使用的代码是

import java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.*;

public class ConnectActiveDirectory {

    public static void main(String[] args) throws NamingException {
        Hashtable env = new Hashtable();
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL,"Administrator@mysite.com");
        env.put(Context.SECURITY_CREDENTIALS, "password");
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldaps://172.16.12.4:636/cn=Users,dc=mysite,dc=com");
        try{
            java.io.InputStream in = new java.io.FileInputStream("C:\\client.crt");
            java.security.cert.Certificate c = java.security.cert.CertificateFactory.getInstance("X.509").generateCertificate(in);
            java.security.KeyStore ks = java.security.KeyStore.getInstance("JKS");
            ks.load(null);
            if (!ks.containsAlias("alias ldap")) {
                ks.setCertificateEntry("alias ldap", c);
            }
            java.io.OutputStream out = new java.io.FileOutputStream("C:\\keystorefile.jks");
            char[] kspass = "changeit".toCharArray();
            ks.store(out, kspass);
            out.close();        
        }catch(Exception e){
            e.printStackTrace();
        }
        System.setProperty("javax.net.ssl.trustStore", "C:\\keystorefile.jks");
        DirContext ctx = new InitialDirContext(env);
        NamingEnumeration enm = ctx.list("");
        while (enm.hasMore()) {
            System.out.println(enm.next());
        }
        ctx.close();
    }
}

我有没有做错?

在哪里可以获得与Active Directory进行ssl连接的好教程?

http://confluence.atlassian.com/display/CROWD/Configuring+an+SSL+Certificate+for+Microsoft+Active+Directory网站是否有正确的步骤来创建和连接活动目录与ssl?

可以随时帮助我。

2 个答案:

答案 0 :(得分:0)

我的AD域名重命名后,我遇到了类似的问题。重新安装证书服务后,您需要删除并重新颁发颁发给域控制器的证书。步骤进行:

  1. 打开MMC
  2. 添加管理单元&gt;证书&gt;计算机&gt;本地计算机
  3. 导航至个人&gt;证书
  4. 删除颁发给此计算机的所有旧证书(在我的情况下,这些是由旧CA颁发的)
  5. 右键单击“证书”文件夹,单击“申请新证书”。
  6. 按照步骤向域控制器颁发新证书。
  7. 重新启动(不确定是否有必要,但在重新启动之前重新启动)

答案 1 :(得分:-1)

我使用Atlassian Crowd和Active Directory通过SSL获得了相同的错误消息。它不适用于这个特定问题,但当我试图找出发生了什么时,这个帖子是第一个Google搜索命中,所以我会在这里写下来。

在我的情况下,我首先测试没有SSL,然后更改为SSL。结果我忘了更改Crowd Connector设置中使用的协议。

之前:ldap://:389 之后:ldaps://:636

无意中使用ldap://:636给了我“连接重置”错误。