2013-02-15 32 views
1

首先感謝Sotirios Delimanolis幫助我解決第一個問題(第一部分是訪問活動目錄)。Java eclipse - 活動目錄,修改屬性#2

所以現在我的代碼是:

 DirContext ctx = null; 

     Hashtable<String, Object> env = new Hashtable<String, Object>(); 
     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
     env.put(Context.PROVIDER_URL, "ldap://"+serverAddress+":389"); 

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

     try { 


      // Create the initial context 
      ctx = new InitialDirContext(env); 

      Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case 
      matchAttrs.put(new BasicAttribute("mail", "[email protected]")); 
      matchAttrs.put(new BasicAttribute("cn")); 

      // Search for objects that have those matching attributes 
      NamingEnumeration<SearchResult> answer = ctx.search("ou=People", matchAttrs); 

      while (answer.hasMore()) { 
       SearchResult sr = (SearchResult)answer.next(); 
       System.out.println(">>>" + sr.getName()); 
      } 

我有錯誤: Failed to bind to LDAP/get account information: javax.naming.NamingException: [LDAP: error code 1 - 000020D6: SvcErr: DSID-03100754, problem 5012 (DIR_ERROR), data 0 ; remaining name 'ou=People'

我發現這個代碼(即跟蹤)在http://docs.oracle.com/javase/jndi/tutorial/basics/directory/basicsearch.html

// Specify the attributes to match 
// Ask for objects that has a surname ("sn") attribute with 
// the value "Geisel" and the "mail" attribute 
Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case 
matchAttrs.put(new BasicAttribute("sn", "Geisel")); 
matchAttrs.put(new BasicAttribute("mail")); 

// Search for objects that have those matching attributes 
NamingEnumeration answer = ctx.search("ou=People", matchAttrs); 
You can then print the results as follows. 
while (answer.hasMore()) { 
    SearchResult sr = (SearchResult)answer.next(); 
    System.out.println(">>>" + sr.getName()); 
    printAttrs(sr.getAttributes()); 
} 

所以我想知道他是否針對上下文「ou = People」特定於每個活動目錄,或者其對於「ou」和「People」始終是相同的:http://www.kouti.com/tables/userattributes.htm

非常感謝!

回答

1

Active Directory是一個LDAP服務器。還有其他LDAP服務器(想到OpenLDAP)。其中每個都有自己或類似的對象類和屬性組成你的目錄模式。您可以查看this Microsoft link下的所有Active Directory對象類和屬性。

在你的榜樣snmail,並且,ou是代表surnamemailorganizational unit,分別爲不同的屬性名稱。這些屬性是名稱 - 值對,因此ou=People表示具有值爲Peopleorganizational unit屬性的對象。

您使用的搜索功能:

ctx.search("ou=People", matchAttrs) 

是在尋找的ou=People匹配你傳遞的那些屬性的上下文。

參數ou=People並非特定於每個Active Directory。 People只是他們決定使用的名稱。我的目錄使用Users,另一個可能使用Accounts。但是,ou通常是用於唯一標識對象的屬性。

我已閱讀並且可以推薦的一個很好的資源是Building Java Enterprise Applications Volume I - Architecture。該鏈接包含一個pdf版本。它有一節介紹如何使用LDAP來驗證您的應用程序用戶,但對於我認爲您會覺得有用的LDAP服務器條目的組織做了很多說明。