我已经使用JNDI编写了用于在AD中使用DirContext
创建用户的代码。
创建用户后,我将无法使用这些凭据登录。当我在AD中手动重置该用户的密码时,我可以登录。
在这里,我已经放置了代码供您参考,
Hashtable<String, String> ldapenv = new Hashtable<>();
ldapenv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapenv.put(Context.PROVIDER_URL, "ldap://10.95.144.139:389");
ldapenv.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapenv.put(Context.SECURITY_PRINCIPAL, "CN=Administrator,CN=Users,dc=Merck,dc=local");
ldapenv.put(Context.SECURITY_CREDENTIALS, "Merck2017");
DirContext context = new InitialDirContext(ldapenv);
Attributes attributes = new BasicAttributes();
// Create the objectclass to add
Attribute objClasses = new BasicAttribute("objectClass");
objClasses.add("top");
objClasses.add("person");
objClasses.add("organizationalPerson");
objClasses.add("user");
// Assign the username, first name, and last name
String cnValue = new StringBuffer(user.getFirstName()).append(" ").append(user.getLastName()).toString();
Attribute cn = new BasicAttribute("cn", cnValue);
Attribute sAMAccountName = new BasicAttribute("sAMAccountName", user.getUserName());
Attribute principalName = new BasicAttribute("userPrincipalName", user.getUserName()
+ "@" + "merck.local");
Attribute givenName = new BasicAttribute("givenName", user.getFirstName());
Attribute sn = new BasicAttribute("sn", user.getLastName());
Attribute uid = new BasicAttribute("uid", user.getUserName());
// Add password
Attribute userPassword = new BasicAttribute("userPassword", user.getPassword());
Attribute pwdAge = new BasicAttribute("pwdLastSet","-1");
Attribute userAccountControl = new BasicAttribute("userAccountControl", "544");
// Add these to the container
attributes.put(objClasses);
attributes.put(sAMAccountName);
attributes.put(principalName);
attributes.put(cn);
attributes.put(sn);
attributes.put(givenName);
attributes.put(uid);
attributes.put(userPassword);
attributes.put(userAccountControl);
attributes.put(pwdAge);
// Create the entry
try {
context.createSubcontext(getUserDN(cnValue,"Merck-Users"), attributes);
System.out.println("success === ");
} catch (Exception e) {
System.out.println("Error --- "+e.getMessage());
}
请帮助我解决以下问题:
userAccountControl
设置为66048
?谢谢。
答案 0 :(得分:0)
我还没有所有答案,但这应该可以帮助您入门:
密码只能通过安全通道设置,例如LDAPS(基于SSL的LDAP)。由于您连接的是端口389,因此不是SSL,AD不允许您设置密码。您必须连接到LDAPS端口:636。您可能会遇到信任SSL证书的问题。由于我不是Java开发人员,所以在这里无济于事,但是有一个示例here。
第二个和第三个问题的答案是相同的:没有密码的帐户始终被禁用。由于您没有正确设置密码,因此该帐户将被禁用。确定密码设置方式后,您还可以根据需要设置userAccountControl
。
您正在正确禁用“用户必须更改密码”选项:通过将pwdLastSet
设置为-1
。这是正确的方法。但是您可能必须首先解决其他问题。
另一个重要的事情:我已经在.NET中创建了AD帐户,发现必须先创建该帐户,然后再返回并设置密码,然后再设置userAccountControl
属性。您可能必须这样做。