我如何使用 java 中的以下详细信息连接到 LDAP 服务器?

How Can i connect to an LDAP server using following details from java?

我正在执行 java 代码,但出现错误 提供的详细信息

password="some"; domain name=ABF.ADDAS.com user name=SADFA.com\username or SADFA\username

Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://ip:389");
            // 
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, ""); 
            env.put(Context.SECURITY_CREDENTIALS, "password");

只需使用您的参数调用 InitialLdapContext:

env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, domainName + "\" + username);
env.put(Context.SECURITY_CREDENTIALS, password);

try{
    return new InitialLdapContext(env, null);
}
catch(javax.naming.CommunicationException e){
    throw new NamingException("Failed to connect to " + domainName + ((serverName==null)? "" : " through " + serverName));
}
catch(NamingException e){
    throw new NamingException("Failed to authenticate " + username + "@" + domainName + ((serverName==null)? "" : " through " + serverName));
}