我正在嘗試使用返回 LdapContext 并采用用戶名、密碼、域名和服務器參數的函數在 Java 中建立 LDAP 連接。不清楚這些參數應該是什么樣子。我正在嘗試連接到這個只讀 LDAP 測試服務器。 http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/我正在使用的 getConnection 方法派生自我在此處找到的 Active Directory 類。 http://www.javaxt.com/wiki/Tutorials/Windows/How_to_Authenticate_Users_with_Active_Directory目前,我正在嘗試 getConnection("tesla", "password", "cn=read-only-admin,dc=example,dc=com", "ldap.forumsys.com:389"),但這是行不通的。我試過切換域和服務器,并嘗試使用“read-only-admin.example.com”而不是“cn=...”。getConnection函數public static LdapContext getConnection(String username, String password, String domainName, String serverName) throws NamingException { if (domainName==null){ try{ String fqdn = java.net.InetAddress.getLocalHost().getCanonicalHostName(); if (fqdn.split("\\.").length>1) domainName = fqdn.substring(fqdn.indexOf(".")+1); } catch(java.net.UnknownHostException e){} } //System.out.println("Authenticating " + username + "@" + domainName + " through " + serverName); if (password!=null){ password = password.trim(); if (password.length()==0) password = null; } //bind by using the specified username/password Hashtable props = new Hashtable(); String principalName = username + "@" + domainName; props.put(Context.SECURITY_PRINCIPAL, principalName); if (password!=null) props.put(Context.SECURITY_CREDENTIALS, password); String ldapURL = "ldap://" + ((serverName==null)? domainName : serverName + "." + domainName) + '/'; props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); props.put(Context.PROVIDER_URL, ldapURL); try{ return new InitialLdapContext(props, null); }
1 回答

胡子哥哥
TA貢獻1825條經驗 獲得超6個贊
問題是您正在嘗試使用非標準用戶名進行身份驗證(適用于 AD 但不適用于 OpenLDAP)。
String principalName = username + "@" + domainName; props.put(Context.SECURITY_PRINCIPAL, principalName);
使用 OpenLDAP 并如教程中所示,principalName 應該是uid=tesla,dc=example,dc=com
添加回答
舉報
0/150
提交
取消