簡體   English   中英

使用Java和LDAP將用戶添加到AD LDS(ADAM)

[英]Adding a user to AD LDS (ADAM) with Java and LDAP

EDIT4:我的應用程序將用戶寫入活動目錄,但當我嘗試啟用用戶時,活動目錄會抱怨

在此輸入圖像描述


以前的消息


我正在嘗試使用Java(1.4)和LDAP將用戶添加到我的本地Active Directory(使用AD LDS)。 但是,我不斷收到以下錯誤:

javax.naming.directory.SchemaViolationException:[LDAP:錯誤代碼65 - 0000207B:UpdErr:DSID-030511CF,問題6002(OBJ_CLASS_VIOLATION),數據0]; 剩余>名稱'CN =測試用戶,OU =帳戶,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字符串
instanceType:0x4 =(WRITE) - 整數
objectCategory:CN = Person,CN = Schema,CN = Configuration,CN = {EDBEACA1-6F60-413C-80F2-6C5CE265F22F} - 專有名稱
objectClass:top; 人; organizationalPerson; user - 對象標識符
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", "AlbertE@antipodes.com");
            attrs.put("mail", "relativity@antipodes.com");
            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);

給了我以下例外:

創建初始目錄上下文...問題創建對象:java.io.UnsupportedEncodingException:UTF16LE

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

編輯3:顯然AD LDS不支持“用戶帳戶控制”,並且分為許多不同的屬性。

您可能可以查看在Active Directory中使用JAVA代碼,特別是創建新用戶和揭開userAccountControl的神秘面紗

對我來說,你忘記了“ CN ”屬性。

檢查架構文檔,了解personuserorganizationalPerson對象類允許和需要哪些屬性。 確保代碼嘗試添加的條目具有所需的所有屬性,並且只包含允許或必需的屬性。

以下是我在為Active Directory 2008開發用戶帳戶管理應用程序(ASP .NET)時學到的一些知識:

  1. 您應該填寫sAMAccountName或userPrincipalName

  2. 在根據域密碼策略為其設置密碼之前,帳戶將保持禁用狀態

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

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

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

(我知道,適用於.NET,但它與Java LDAP api非常相似)

希望這對你有所幫助

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

暫無
暫無

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

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