亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Java 建立 LDAP 連接

使用 Java 建立 LDAP 連接

慕絲7291255 2023-03-17 15:11:30
我正在嘗試使用返回 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


查看完整回答
反對 回復 2023-03-17
  • 1 回答
  • 0 關注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號