簡體   English   中英

使用Java連接到LDAP Active Directory,無需用戶名和密碼

[英]Connect to the LDAP Active Directory in Java without username and password

我有一個用C#編寫的簡單應用程序。 該應用程序適用於Windows機器和登錄到Active Directory的用戶帳戶。

在該應用程序中,我從LDAP Active Directory中檢索一些信息。

在C#中,我以這種方式與LDAP建立連接:

DirectoryEntry de = new DirectoryEntry("LDAP://OU=places,DC=system,DC=org");

然后我檢索所需的數據(例如):

DirectorySearcher searcher = new DirectorySearcher(de);
searcher.Filter = "(telephone=111222333)";
SearchResultCollection resultCollection = searcher.FindAll();

正如您在該應用程序中看到的那樣,我不需要使用用戶名和密碼登錄LDAP,但只需要輸入正確的路徑:

DirectoryEntry的構造函數如下所示:

public DirectoryEntry(
    string path
)

當然有一個帶有用戶名和密碼的構造函數,但正如我所說,我不需要使用它:

public DirectoryEntry(
    string path,
    string username,
    string password
)

我試圖在Java中檢索相同的信息,我使用了類似的代碼:

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://ad.host:389");
env.put(Context.SECURITY_PRINCIPAL, "username");
env.put(Context.SECURITY_CREDENTIALS, "password");

DirContext ldap = new InitialDirContext(env);

然后我檢索所需的數據(例如):

SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration tel = ldap.search("OU=places,DC=system,DC=org", "(telephone=111222333)", searchControls);

但是有一個區別。 如您所願,如果我想連接到該LDAP Active Directory,我需要在Java中輸入用戶名和密碼。

如果我沒有輸入用戶名和密碼,我將看到以下錯誤:

Exception in thread "main" javax.naming.NamingException:[LDAP: error code 1 - 000004DC: LdapErr: DSID-0C0906E8, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v1db1;] remaining name 'OU=places,DC=system,DC=org'

我的問題是,如果我可以從Java中的同一台機器登錄到同一個LDAP Active Directory而無需輸入用戶名和密碼?

在C#中是否有類似於在Java中執行此操作(不添加用戶名和密碼)?

如果使用env.put(Context.SECURITY_AUTHENTICATION, "none"); 沒有用,那么你的AD環境可能不支持匿名身份驗證,這是可以理解的。 默認情況下禁用匿名身份驗證。

當您使用new DirectoryEntry(...); 在C#中,您可能看起來沒有使用任何憑據,但它確實使用了當前登錄用戶的憑據。 所以它借用你自己的憑證來調用AD。

Java不會這樣做。 事實上,從我剛才所做的簡短的谷歌搜索來看,似乎要做到這一點很難,如果這就是你想做的事情。

這里有一個關於如何做到這一點的問題: 使用Windows上登錄的用戶在Java中查詢Active Directory

那里的評論給了一些產品來研究。

但是,如果您不想使用任何可能使事情復雜化的第三方產品,您只需提供用戶名和密碼即可。

如果要在C#中測試匿名身份驗證,可以使用以下內容:

new DirectoryEntry(ldapPath, null, null, AuthenticationTypes.Anonymous)

我找到了一個解決方案來創建一個進程並運行powershell腳本。 在powershell腳本中,您可以使用“System.DirectoryServices”進行搜索,而無需傳遞用戶名和密碼。

請參閱查詢Active Directory

我有一個用C#編寫的簡單應用程序。 該應用程序適用於Windows機器和登錄到Active Directory的用戶帳戶。

我猜你的C#應用​​程序只是使用一個Windows API,它通過引擎蓋自動驗證Windows會話的Kerberos票證。

如果您想使用其他編程語言執行此操作,則必須使用SASL mech GSSAPI執行LDAP SASL綁定請求。 但是,例如基於Java的LDAP客戶端也必須使用Windows憑據緩存。

另請參閱: Java平台,標准版安全性開發人員指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM