以编程方式连接到AD的问题

时间:2011-05-27 09:20:32

标签: java active-directory

我正在开发一个使用AD来帮助登录的Java EE 6应用程序。我的身份验证器很简单,看起来像这样:

@Singleton
@TransactionManagement(TransactionManagementType.BEAN)
public class ADAuthenticator{
    private static final String ldapHost = "ldap://domainname.mycompany.com:389";
    private static final String domain = "domainname";

    public ADAuthenticator() {
    }

    public void authenticate(String user, String pass) throws NamingException{
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, ldapHost);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, user + "@" + domain);
        env.put(Context.SECURITY_CREDENTIALS, pass);
        LdapContext ctxGC = new InitialLdapContext(env, null);
    }
}

这已经有一段时间了,但是最近我得到了以下异常(除了神奇地工作的一些时间):

javax.naming.CommunicationException: mydomain.mycompany.com:389 [Root exception is java.net.ConnectException: Connection timed out: connect]
at com.sun.jndi.ldap.Connection.<init>(Connection.java:210)
at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:118)
at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1580)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2652)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:293)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134)
at com.xdin.competence.util.ADAuthenticator.authenticate(ADAuthenticator.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at <ejb stuff omitted> 

Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at com.sun.jndi.ldap.Connection.createSocket(Connection.java:352)
at com.sun.jndi.ldap.Connection.<init>(Connection.java:187)
... 101 more

代码没有任何变化,根据IT支持,AD的配置也没有改变。

我将相关代码分离到一个简单的控制台应用程序中,它与完全相同的设置完美配合。所以问题似乎与Java EE或Web服务器有关。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

TCP / IP连接的数据包没有及时到达。这可能有多种原因,但根据您的描述,这可能只是您的Java代码和LDAP服务器之间的网络拥塞,从而导致程序包丢失。可能还有防火墙阻止该端口。

您需要了解pingtraceroutetelnet host port工具才能对网络层进行问题排查。我建议你和一个来自IT支持的网络人员合作,并很好地帮助他找出你的包装丢失的位置。