使用LDAP / Java启用Active Directory用户

我正在尝试使用LDAP和
Java(1.4)在Active Directory中启用用户.

但是我一直收到以下错误:

java.lang.NullPointerException at
com.sun.jndi.ldap.LdapCtx.c_modifyAttributes(LdapCtx.java:1432) at
com.sun.jndi.toolkit.ctx.ComponentDirContext.p_modifyAttributes(ComponentDir
Context.java:255) at
com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.modifyAttributes(Partial
CompositeDirContext.java:172) at
com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.modifyAttributes(Partial
CompositeDirContext.java:161) at
javax.naming.directory.InitialDirContext.modifyAttributes(InitialDirContext.
java:146) at be.ideal.LDAP.newuser.main(newuser.java:61) Exception in
thread “main”

我已经确认我的用户有密码,我似乎无法将其状态更改为活动状态

我的代码:

 public static void main(String[] args) {
        String userName = "cn=Albert Einstein,ou=Accounts,DC=PORTAL,DC=COMPANY,DC=BE";
        String groupName = "cn=Administrators,cn=Roles,DC=PORTAL,DC=COMPANY,DC=BE";
        boolean isDisabled = false;

        try {

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

            Attributes attrs = new BasicAttributes(true);

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

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

            attrs.put(new BasicAttribute("msDS-UserAccountDisabled", "FALSE"));

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

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

            try {
                ModificationItem member[] = new ModificationItem[1];
                member[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                        new BasicAttribute("member", userName));

                ctx.modifyAttributes(groupName, member);
                System.out.println("Added user to group: " + groupName);

            } catch (NamingException e) {
                System.err.println("Problem adding user to group: " + e);
            }

            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);
        }
    }

PS:我正在使用AD LDS作为我的活动目录

最佳答案 找到了:

需要使用DirContext.ADD_ATTRIBUTE而不是DirContext.REPLACE_ATTRIBUTE

点赞