2012-03-16 119 views
1

EDIT4:得到了我的應用程序寫入用戶的活動目錄,但是當我試圖使用戶將用戶添加到AD LDS(ADAM)與Java和LDAP

enter image description here

活動目錄抱怨

以前的訊息


我試圖通過全光照將用戶添加到我的本地Active Directory(與AD LDS) g Java(1.4)和LDAP。 不過,我不斷收到以下錯誤:

javax.naming.directory.SchemaViolationException: [LDAP: error code 65 - 0000207B : UpdErr: DSID-030511CF, problem 6002 (OBJ_CLASS_VIOLATION), data 0 ]; remaining > name 'CN=Test user,OU=Accounts,DC=PORTAL,DC=COMPANY,DC=BE'

我的代碼:

public static void main(String[] args) { 
     try { 
      DirContext ctx = new InitialDirContext(X_Ldap.getEnvironment()); 
      user usr = new user("Test user", "FALSE"); 

      ctx.bind(
        "CN=Test user,OU=Accounts,DC=PORTAL,DC=COMPANY,DC=BE",      usr); 

      // X_Ldap.checkIfUserExists("Test User"); 
      ctx.close(); 
     } catch (NamingException e) { 
      e.printStackTrace(); 
     } 
} 

public class user implements DirContext { 
    String type; 

    /** 
    * 
    * @param isDisabled 
    *   TRUE or FALSE (literally) 
    */ 
    public user(String username, String isDisabled) { 
     String type = username; 

     Attributes attr = new BasicAttributes(true); 
     Attribute oc = new BasicAttribute("objectclass"); 
     oc.add("top"); 
     oc.add("person"); 
     oc.add("organizationalPerson"); 
     oc.add("user"); 
     Attribute memberOf = new BasicAttribute("memberOf"); 
     memberOf.add("CN=Users,CN=Roles,DC=PORTAL,DC=COMPANY,DC=BE"); 

     attr.put(oc); 
     attr.put("msDS-UserAccountDisabled", isDisabled); 
     attr.put(memberOf); 

     attr.put("comment", username); 
    } 

    public String toString() { 
      return type; 
    } 
} 

編輯 我查了我的用戶對象之一爲強制屬性,但我不確定我應該填寫什麼其中:

cn: Jane Doe -- Unicode string
instanceType: 0x4 = (WRITE) -- Integer
objectCategory: CN=Person,CN=Schema,CN=Configuration,CN={EDBEACA1-6F60-413C-80F2-6C5CE265F22F} -- Distinguished Name
objectClass: top; person; organizationalPerson; user -- Object Identifier
objectSid: S-1-372665300-2234744891-519896106-1336725265-1748609191-3385095770 -- SID


EDIT2:我當前的代碼:

public class newuser { 
    public static void main(String[] args) { 

     String userName = "cn=Albert Einstein,ou=Accounts,DC=PORTAL,DC=COMPANY,DC=BE"; 
     // String groupName = 
     // "cn=Users,cn=Roles,DC=PORTAL,DC=COMPANY,DC=BE"; 

     try { 

      // Create the initial directory context 
      System.out.println("Creating initial directory context..."); 
      LdapContext ctx = new InitialLdapContext(X_Ldap.getEnvironment(), 
        null); 

      // Create attributes to be associated with the new user 
      Attributes attrs = new BasicAttributes(true); 

      // some useful constants from lmaccess.h 
      int UF_ACCOUNTDISABLE = 0x0002; 
      int UF_PASSWD_NOTREQD = 0x0020; 
      int UF_PASSWD_CANT_CHANGE = 0x0040; 
      int UF_NORMAL_ACCOUNT = 0x0200; 
      int UF_DONT_EXPIRE_PASSWD = 0x10000; 
      int UF_PASSWORD_EXPIRED = 0x800000; 


      attrs.put("objectClass", "user"); 
      attrs.put("cn", "Albert Einstein"); 

      // These are some optional (but useful) attributes 
      attrs.put("givenName", "Albert"); 
      attrs.put("sn", "Einstein"); 
      attrs.put("displayName", "Albert Einstein"); 
      attrs.put("description", "Research Scientist"); 
      attrs.put("userPrincipalName", "[email protected]"); 
      attrs.put("mail", "[email protected]"); 
      attrs.put("telephoneNumber", "999 123 4567"); 
      String newQuotedPassword = "\"Pass123\""; 
      byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16"); 
      attrs.put("unicodePwd", newUnicodePassword); 
      attrs.put("msDS-User-Account-Control-Computed", 
      Integer.toString(UF_NORMAL_ACCOUNT + UF_DONT_EXPIRE_PASSWD)); 

      // Create the context 
      System.out.println("Creating context..."); 
      Context result = ctx.createSubcontext(userName, attrs); 
      System.out.println("Created disabled account for: " + userName); 

      ctx.close(); 

      System.out.println("Successfully created User: " + userName); 

     } catch (NamingException e) { 
      System.err.println("Problem creating object: " + e); 
     } 

     catch (IOException e) { 
      System.err.println("Problem creating object: " + e); 
     } 


    } 
} 

還有以下問題:

String newQuotedPassword = "\"Pass123\""; 
     byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16"); 
     attrs.put("unicodePwd", newUnicodePassword); 

給了我以下異常:

Creating initial directory context... Problem creating object: java.io.UnsupportedEncodingException: UTF16LE

注:我禁用了要求對SSL更改密碼

編輯3:顯然是「用戶帳戶控制」不是AD LDS支持,在許多不同的屬性被分割。

回答

2

你也許可以看看Using JAVA code with Active Directory especialy Creating new users & demystifying userAccountControl

對我來說,你忘了「CN」屬性。

+0

第二個鏈接幫助我進一步了一點,但是現在在創建上下文之前我收到以下錯誤:創建對象的問題:javax.naming.NameNotFoundException:[LDAP:error code 32 - 0000208D:NameErr:DSID-0310020A,problem 2001 (NO_OBJECT),數據0,最好匹配:'DC = PORTAL,DC = COMPANY,DC = BE'];剩餘的名字'CN =阿爾伯特愛因斯坦,CN =賬號,DC = PORTAL,DC = COMPANY,DC = BE' – Andreas 2012-03-19 14:12:08

+1

什麼是CN =目錄中的賬號?你能描述你的層次嗎?你可以再次發佈你的代碼嗎? – JPBlanc 2012-03-19 15:48:31

+0

已經找到它了,CN必須是OU。目前正在尋找密碼來工作 – Andreas 2012-03-19 15:53:58

1

檢查您的模式文檔允許哪些屬性以及person,userorganizationalPerson對象類所需的屬性。確保代碼試圖添加的條目具有所需的所有屬性,並且只有允許或需要的屬性。

+0

已更新問題,包含對象用戶所需的屬性;我知道我應該爲CN和objectclass添加什麼,但我不確定我應該如何處理其他三個。 – Andreas 2012-03-19 09:00:29

+0

你確定'memberOf'是一個允許的屬性嗎?在某些目錄服務器中,'memberOf'是一個虛擬屬性,也就是說,'memberOf'是根據請求生成的。 – 2012-03-19 09:14:02

+0

我試着用對象類和CN(它是必需的屬性)運行它,但仍然得到相同的錯誤 – Andreas 2012-03-19 09:22:24

0

對象類架構違例意味着您嘗試創建的對象中缺少一個或多個必需屬性。因此,您需要查看top,person,organizationalPerson和user的模式,並確保您設置了所有必需的屬性。

+0

已更新的問題與對象用戶所需的屬性;我知道我應該爲CN和objectclass添加什麼,但我不確定我應該如何處理其他三個。 – Andreas 2012-03-19 09:01:40

1

以下是我在開發用戶帳戶管理應用程序(ASP)時學到的一些知識。NET)爲Active Directory 2008:直到你爲它設置密碼

  1. 應填寫sAMAccountName賦或通過UserPrincipalName

  2. 帳戶保持禁用根據域密碼策略

  3. 任何密碼相關的操作需要使用安全連接完成

  4. 創建帳戶時,打開OU的上下文以創建用戶對象。然後調用添加它的方法

閱讀本文件:http://msdn.microsoft.com/en-us/magazine/cc135979.aspx

(我知道,是.NET,但它是非常非常類似於Java LDAP API)

希望這有助於你