Java创建AD(Active Directory)域控制器用户 (未测)
import java.util.Hashtable;
import javax.naming.ldap.*;
import javax.naming.directory.*;
import javax.naming.*;
import javax.net.ssl.*;
import java.io.*; public class NewUser{ /**
* @param args
*/
public static void main (String[] args)
{ Hashtable env = new Hashtable();
String adminName = "CN=Administrator,CN=Users,DC=antipodes,DC=com";
String adminPassword = "XXXXXXX";
String userName = "CN=Albert Einstein,OU=Research,DC=antipodes,DC=com";
String groupName = "CN=All Research,OU=Research,DC=antipodes,DC=com"; env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
//set security credentials, note using simple cleartext authentication
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,adminName);
env.put(Context.SECURITY_CREDENTIALS,adminPassword);
//connect to my domain controller
env.put(Context.PROVIDER_URL, "ldap://mydc.antipodes.com:389"); try { // Create the initial directory context
LdapContext ctx = new InitialLdapContext(env,null); // Create attributes to be associated with the new user
Attributes attrs = new BasicAttributes(true); //These are the mandatory attributes for a user object
//Note that Win2K3 will automagically create a random
//samAccountName if it is not present. (Win2K does not)
attrs.put("objectClass","user");
attrs.put("samAccountName","AlbertE");
attrs.put("cn","Albert Einstein"); //These are some optional (but useful) attributes
attrs.put("giveName","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"); //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; //Note that you need to create the user object before you can
//set the password. Therefore as the user is created with no
//password, user AccountControl must be set to the following
//otherwise the Win2K3 password filter will return error 53
//unwilling to perform. attrs.put("userAccountControl",Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD + UF_PASSWORD_EXPIRED+ UF_ACCOUNTDISABLE)); // Create the context
Context result = ctx.createSubcontext(userName, attrs);
System.out.println("Created disabled account for: " + userName); //now that we've created the user object, we can set the
//password and change the userAccountControl
//and because password can only be set using SSL/TLS
//lets use StartTLS StartTlsResponse tls = (StartTlsResponse)ctx.extendedOperation(new StartTlsRequest());
tls.negotiate(); //set password is a ldap modfy operation
//and we'll update the userAccountControl
//enabling the acount and force the user to update ther password
//the first time they login
ModificationItem[] mods = new ModificationItem[]; //Replace the "unicdodePwd" attribute with a new value
//Password must be both Unicode and a quoted string
String newQuotedPassword = "\"Password2000\"";
byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE"); mods[] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodePwd", newUnicodePassword));
mods[] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl",Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWORD_EXPIRED))); // Perform the update
ctx.modifyAttributes(userName, mods);
System.out.println("Set password & updated userccountControl"); //now add the user to a group. try{
ModificationItem member[] = new ModificationItem[];
member[]= 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);
}
//Could have put tls.close() prior to the group modification
//but it seems to screw up the connection or context ?
tls.close();
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);
}
}
}
Java创建AD(Active Directory)域控制器用户 (未测)的更多相关文章
- cmd 执行Dcpromo错误:在该 SKU 上不支持 Active Directory 域服务安装向导,Windows Server 2008 R2 Enterprise 配置AD(Active Directory)域控制器
今天,要安装AD域控制器,运行dcpromo结果提示:在该 SKU 上不支持 Active Directory 域服务安装向导. 以前弄的时候直接就通过了,这次咋回事?终于搞了大半天搞定了. 主要原因 ...
- Windows Server 2008 R2 配置AD(Active Directory)域控制器
实施过程: 一.安装Windows Server2008 R2操作系统 (过程略) 二.安装域控制器 1. 修改电脑名称 2.修改电脑DNS 三.配置AD 1.在"服务器管理器"- ...
- Windows Server 2008 R2 配置AD(Active Directory)域控制器 -zhai zi wangluo
http://files.cnblogs.com/zhongweiv/Windows_Server_2008_R2_%E9%85%8D%E7%BD%AEActive_Directory%E5%9F%9 ...
- install Active Directory域控制器
设置Active Directory域控制器 正如我们在网络与系统配置专题文章中所提到的那样,我们已将两部服务器设置为对应于内部域“intdomain.com”的Active Directory域控制 ...
- 您真的会修改Active Directory域控制器计算机名称吗
从我开始做微软这行开始,就经常听说某某公司由于什么原因需要修改Active Directory域控制器计算机名称,但发现好多公司都是直接修改,导致了各种奇葩的问题,今天就给大家推荐一个修改Active ...
- C# AD(Active Directory)域信息同步,组织单位、用户等信息查询
示例准备 打开上一篇文章配置好的AD域控制器 开始菜单-->管理工具-->Active Directory 用户和计算机 新建组织单位和用户 新建层次关系如下: 知识了解 我们要用C# ...
- 将Samba设置为Active Directory域控制器
一 简介 从版本4.0开始,samba可以作为Active Directory(AD)域控制器(DC)运行,如果在生产环境中安装samba,建议运行两个或者多个DC用于故障转移 本文介绍如何让将一个S ...
- 无法与域Active Directory域控制器(AD DC)连接(虚机加域出错问题)
今天建了两台虚机用的VMWARE,一台做域控,一台做应用服务器,但是部署好域控要把应用服务器加入域时候报错 虚机网卡设置桥接并设置好IP使两台虚机在同一个局域网内,通过ip地址互ping能ping通, ...
- 客户端无法加入域,报错:“无法与域‘xxx.com’的Active Directory域控制器(AD DC)链接” 请确保键入的域名正确
1.客户端能不能解析到域名? nslookup 一下域名看看解析到的IP的地址 2.客户端的DNS要指向DC 3.客户端的相关服务,workstation,TCP/IP NetBios Helper, ...
- Active Directory域
引言 在 Microsoft® Windows® 2000 Server 操作系统的诸多增强功能中,Microsoft Active Directory™ 功能的引入意义最为重大,但也最常引起困惑.与 ...
随机推荐
- nginx location proxy pass
nginx: 192.168.1.23作为nginx反向代理机器 目标机器192.168.1.5上部署一个8090端口的nginx [root@localhost conf.d]# cat test. ...
- MIPI DBI\DPI\DSI简介【转】
本文转载自:http://blog.csdn.net/longxiaowu/article/details/24249971 (1)DBI接口 A,也就是通常所讲的MCU借口,俗称80 system接 ...
- centOS最小化安装后网络连接问题
编辑配置文件 vi /etc/sysconfig/network-script/ifcfg-eth0 修改此行重启后即可 ONBOOT="yes" #修改为 ...
- iostream与iostream.h的区别
简单来说: .h的是标准C的头文件,没有.h的是标准C++的头文件,两种都是头文件. 造成这两种形式不同的原因,是C++的发展历史决定的,刚才正好有别的人也问这个问题,这里我再回答一下(注意vs200 ...
- freemarker空值的处理
FreeMarker的变量必须赋值,否则就会抛出异常.而对于FreeMarker来说,null值和不存在的变量是完全一样的,因为FreeMarker无法理解null值.FreeMarker提供两个运算 ...
- LeetCode——Construct Binary Tree from Preorder and Inorder Traversal
Question Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as ...
- JavaWeb -- JSP+JavaBean模式
SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. JSP+JavaBean模式适合开发业务 ...
- djang-分页
分页 views from django.shortcuts import render,HttpResponse # Create your views here. from app01.model ...
- IDEA运行debug为灰色无法运行
这种情况一般在使用他人项目的时候产生. 检查几个问题,本地环境是否配置好 然后进入项目的入口右键运行是否可以正常运行 如果不可以,就在 修改一下src文件 一般就可以正常使用了
- 一个简单客户端获取IP,国家,城市,省份的代码
<html><head> <script src="js/jquery-1.6.2.min.js" type="text/javascri ...