JAVA通过SSL证书创建MS AD账户及设置密码
近期由于工作需要整理一下自动化的东西,因为公司去年上线了OA,所以公司的入职系统会提交用户的信息到IT部门,最早的做法是入职到了,IT部门收集用户信息在AD中创建对应的用户信息,所以为了提高管理员的工作效率,所以准备实施自动创建AD账户,当OA流程到IT人员审批节点后,IT人员审批后根据人员信息自动创建AD账户,所以整理了一些JAVA创建AD人员信息的信息,但是我们需要注意点的是,对于JAVA语言操作MS AD的一些普通操作是不需要SSL的,但是对于用户密码的重置操作必须使用SSL,当然之前看网上有说可以跳过的,但是没有试验成功,所以还是按照标准的配置来通过SSL对用户的AD密码进行操作,废话不多说了,今天我们主要介绍使用JAVA通过SSL方式创建MS AD账户,因为要对创建的用户设置密码,所以需要使用SSL证书,既然需要SSL证书,目的就是为了让JAVA信任LDAP,所以我们需要从AD中导出受信任的证书,然后导入到JAVA运行环境中的JRE下的cacert证书文件中。我们既然说到了OA,其实OA中就可以通过系统自带的功能进行证书申请及证书导入,这样比较简单;当然如果没有OA环境的,我们可以通过JAVA运行环境中的JDK中的keytool进行证书导入工作,我们下面都会介绍到;
我们首先使用OA中的功能进行证书导入;我们OA中的证书路劲在/OAFS/WEAVER/jdk1.8.0_101/jre/lib/security/cacerts
确认OA环境中的JDK路劲后,我们接下来就是证书申请及导入了;
我们访问OA的地址,然后路劲增加/integration/ldapcert.jsp路劲访问即可;如果没有后面的JAVA文件可以找OA供应商要;或者在附件下载;
我们首先下载附件中的文件,下载后,附件中有三个文件;classbean、
我们首先进入classbean文件夹内的内容拷贝到对应的OA服务器的对应目录;
ecology\classbean\weaver\ldap
2.然后将解压文件中的文件夹integration文件中的以下文件拷贝到OA的对应的服务器目录下:
ecology\integration
3.因为解压后有三个文件夹,第三个文件夹src为源码,我们就不用管了
按照以上方法走完后,我们就可以通过以下链接来配置了
http://192.168.6.101/integration/ldapcert.jsp
访问后,我们再LDAP IP输入环境的AD DC服务器地址,系统会默认填写LDAP端口636,及证书路劲,这些信息系统会自动补全;我们需要手动设置证书密码,一般我们都会设置成changeit,设置好这些信息后,我们导入证书,会提示下面的导入信息;
导入完成
然后我们需要在证书路劲下载证书到本地的JRE环境进行测试了
接着我们看看第二种方式的证书申请;
我们需要从DC上导入域的根证书
mmc---增加---证书---计算机---个人---选择根证书----导出
不需要导出私钥
使用默认的DRE编码
保存
我们按照同样的方式,将另外一张也导出来
然后我们需要在本地的JDK环境中导入该根证书到JDK环境中的证书中;
我本地的JDK环境路劲D:\Development_Environment\java\jdk\jre\lib\security
然后运行命令将刚才导出的根证书导入到该路劲的cacert证书文件中;
我们首先要cd到jdk路劲
cd D:\Development_Environment\java\jdk\jre\bin
然后将刚才导出的根证书保存到D盘下,通过以下命令导入
keytool keytool -import -keystore D:\Development_Environment\java\jdk\jre\lib\security\cacerts -storepass changeit -keypass changeit -alias CA -file d:\ADroot.cer
输入Y受信任
然后我们就可以通过
接着就是看看ADDS环境了
换进准备好,我们就可以上代码了;
我们设置好证书路劲,及LDAP验证信息,及需要注册的用户名
账户注册成功
上代码:
package com.ixmsoft.oa.util;
import java.util.Properties;
import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;
/**
* @author Keven Chen
* @version $Revision 1.0 $
*
*/
public class AddAdUser {
private static final String SUN_JNDI_PROVIDER = "com.sun.jndi.ldap.LdapCtxFactory";
public static void main(String[] args) throws Exception {
String keystore = "D:\\Development_Environment\\java\\jdk\\jre\\lib\\security\\cacerts";
System.setProperty("javax.net.ssl.trustStore", keystore);
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, SUN_JNDI_PROVIDER);// java.naming.factory.initial
env.put(Context.PROVIDER_URL, "ldap://192.168.5.20:636");// java.naming.provider.url
env.put(Context.SECURITY_AUTHENTICATION, "simple");// java.naming.security.authentication
env.put(Context.SECURITY_PRINCIPAL,
"cn=Administrator,cn=Users,dc=ixmsoft,dc=com");// java.naming.security.principal
env.put(Context.SECURITY_CREDENTIALS, "123");// java.naming.security.credentials
env.put(Context.SECURITY_PROTOCOL, "ssl");
String userName = "CN=gaowenlong,OU=IXM Adm,OU=IMXSOFT Users,DC=ixmsoft,DC=com";
String groupName = "CN=Domain Admins,CN=Users,DC=ixmsoft,DC=com";
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", "gaowenlong");
attrs.put("cn", "gaowenlong");
// These are some optional (but useful) attributes
attrs.put("sn", "gaowenlong");
attrs.put("displayName", "gaowenlong");
attrs.put("description", "gaowenlong");
attrs.put("userPrincipalName", "gaowenlong@ixmsoft.com");
attrs.put("mail", "gaowenlong@ixmsoft.com");
attrs.put("telephoneNumber", "1234568999");
// 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);
ModificationItem[] mods = new ModificationItem[2];
// 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[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("unicodePwd", newUnicodePassword));
mods[1] = 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[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);
}
// Could have put tls.close() prior to the group modification
// but it seems to screw up the connection or context ?
ctx.close();
System.out.println("Successfully created User: " + userName);
}
}
我们查看
查看账户属性
然后查看属性
我们将java文件上传到附件中,如果加在eclipse中有报错,请根据错误提示,右击导入ldap相关的包即可,
转载于:https://blog.51cto.com/gaowenlong/1969585