[英]Get LDAP-Attributes from an dirContext in Java with the “sAMAccountName” instead of “CN”
我正在使用Java連接到Active Directory,現在我喜歡使用DirContext
的getAttributes
方法來查找所有屬性而不進行搜索。
我發現這個例子http://www.java2s.com/Code/Java/JNDI-LDAP/howtoretrieveallattributesofanamedobject.htm並且它工作正常 - 但它需要對象的CN,此時我沒有CN。
這是我的代碼:
Hashtable<String, Object> env = new Hashtable<String, Object>(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.18.106:389/OU=journaldesigner,DC=dataplan,DC=intern");
env.put(Context.SECURITY_PRINCIPAL, "kfriese@dataplan.intern" );
env.put(Context.SECURITY_CREDENTIALS, "fridolin" );
env.put(Context.SECURITY_AUTHENTICATION, "simple" );
DirContext ctx = new InitialDirContext(env);
Attributes answer = ctx.getAttributes("cn=Klaus Friese");
如果我替換這一行:
Attributes answer = ctx.getAttributes("cn=Klaus Friese");
有了這個
Attributes answer = ctx.getAttributes("sAMAccountName=kfriese");
我收到一個錯誤:
javax.naming.NameNotFoundException: [LDAP: error code 32 - 0000208D: NameErr: DSID-0310020A, problem 2001 (NO_OBJECT), data 0, best match of:
'OU=JournalDesigner,DC=dataplan,DC=intern'
我現在搜索了一段時間,但我沒有發現任何相關內容 - 我可以使用sAMAccountName獲取屬性嗎? 我需要用CN嗎? AD Server是Microsoft Active Directory。
謝謝克勞斯
方法getAttributes()
是一種專門的搜索,只接受搜索過濾器的名稱。 您不能使用sAMAccountName=kfriese
因為sAMAccountName不是名稱。 因此,要讀取條目的所有屬性,您必須知道其名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.