如何通过Java客户端在Active Directory中创建新用户并将其添加到现有组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何通过Java客户端在Active Directory中创建新用户并将其添加到现有组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3352字,纯文字阅读大概需要5分钟。
内容图文
![如何通过Java客户端在Active Directory中创建新用户并将其添加到现有组](/upload/InfoBanner/zyjiaocheng/770/4b0a33ad588544c88fe1c1d0c819f355.jpg)
我是初学者,我尝试用Java实现Active Directory客户端.到目前为止,我编写了以下代码:
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class NewUser {
public static void main(String[] args) {
NewUser user = new NewUser("aaa", "bbb", "ccc", "orgunit");
try {
System.out.print(user.addUser());
} catch (NamingException e) {
e.printStackTrace();
}
}
private static final String DOMAIN_NAME = "whatever";
private static final String DOMAIN_ROOT = "dc=xyz"; // ?
private static final String ADMIN_NAME = "CN=Administrator,CN=Users,DC=xyz,DC=xyz";
private static final String ADMIN_PASS = "xxxxxxx";
private static final String DOMAIN_URL = "ldap://xxx.xxx.xx.xx:389";
private String userName, firstName, lastName, organisationUnit;
private LdapContext context;
public NewUser(String userName, String firstName, String lastName, String organisationUnit) {
this.userName = userName;
this.firstName = firstName;
this.lastName = lastName;
this.organisationUnit = organisationUnit;
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, ADMIN_NAME);
env.put(Context.SECURITY_CREDENTIALS, ADMIN_PASS);
env.put(Context.PROVIDER_URL, DOMAIN_URL);
try {
this.context = new InitialLdapContext(env, null);
} catch (NamingException e) {
System.err.println("Problem creating object: ");
e.printStackTrace();
}
}
public boolean addUser() throws NamingException {
Attributes container = new BasicAttributes();
Attribute objClasses = new BasicAttribute("objectClass");
objClasses.add("top");
objClasses.add("person");
objClasses.add("organizationalPerson");
objClasses.add("user");
String cnValue = new StringBuffer(firstName).append(" ").append(lastName).toString();
Attribute cn = new BasicAttribute("cn", cnValue);
Attribute sAMAccountName = new BasicAttribute("sAMAccountName", userName);
Attribute principalName = new BasicAttribute("userPrincipalName", userName
+ "@" + DOMAIN_NAME);
Attribute givenName = new BasicAttribute("givenName", firstName);
Attribute sn = new BasicAttribute("sn", lastName);
Attribute uid = new BasicAttribute("uid", userName);
container.put(objClasses);
container.put(sAMAccountName);
container.put(principalName);
container.put(cn);
container.put(sn);
container.put(givenName);
container.put(uid);
try {
context.createSubcontext(getUserDN(cnValue, organisationUnit), container);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static String getUserDN(String aUsername, String aOU) {
return "cn=" + aUsername + ",ou=" + aOU + "," + DOMAIN_ROOT;
}
}
我需要的只是创建和添加用户.
我有以下错误:
javax.naming.PartialResultException: [LDAP: error code 10 – 0000202B: RefErr: DSID 031007F3, data 0, 1 access points
ref 1: ‘xyz’
]; remaining name ‘cn=bbb ccc,ou=orgunit,dc=xyz’
解决方法:
您需要知道要在哪里创建用户以及它存在的FDN.
我建议你获取一个LDAP浏览器,ldapwiki.willeke.com / wiki / LDAP浏览器,这样你就可以看到你的AD从LDAP看起来像什么.
您可能还会发现这有用:ldapwiki.willeke.com/wiki/确定FDN
内容总结
以上是互联网集市为您收集整理的如何通过Java客户端在Active Directory中创建新用户并将其添加到现有组全部内容,希望文章能够帮你解决如何通过Java客户端在Active Directory中创建新用户并将其添加到现有组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。