目标:使用者只需要会使用list,map 数据结构,将对ldap的操作进行封装
类:主要有三个类
1 env类 包含ldap的连接信息
2 ldapconnectionfactory类 ldap连接工厂,提供初始化及获取ldap连接的方法
3 ldapoperutils ldap的处理工具类,提供了各种操作ldap的方法。
如何使用封装jndi操作ldap服务器的工具类
下面是一个例子
测试类的功能,向windows active directory 增加一个域用户 lwf2_count,并激活该帐户
public class testoper {
public static void main(string args[]) throws baseexception,
namingexception, unsupportedencodingexception {
// 连接active directory 信息
env env = new env();
env.factory = "com.sun.jndi.ldap.ldapctxfactory";
env.url = "ldap://10.110.179.175:389";
env.adminuid = "cn=administrator,cn=users,dc=securitytest,dc=boco";
env.adminpwd = "ba88736612";
dircontext dircontext = ldapconnectionfactory.getdircontext(env);
// 增加一个active directory 用户需要的属性
list list1 = new arraylist();
map attmap = new hashmap();
list1.add("top");
list1.add("person");
list1.add("organizationalperson");
list1.add("user");
attmap.put("objectclass", list1);
attmap.put("cn","lwf2_count"); // active directory 的name
ttmap.put("sn","liao"); // active directory 的 姓
attmap.put("givenname","wufeng"); // active directory 的 名
attmap.put("displayname","liaowufeng"); // active directory 的 显示名
attmap.put("userprincipalname","lwf2_name@securitytest.boco"); // active directory 的 用户登录名
attmap.put("samaccountname","lwf2_name"); // active directory 的 用户登录名 (widnows 2000 以前版本)
string newpassword = "ba123456";
attmap.put("userpassword",newpassword); // 用户密码
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;
// 激活帐号
attmap.put("useraccountcontrol", integer.tostring(uf_dont_expire_passwd));
ldapoperutils.addcontext(dircontext,"cn=lwf2_count,cn=users,dc=securitytest,dc=boco", attmap);
// 关闭dircontext
ldapconnectionfactory.closedircontext(dircontext);
}
}
好了,就写到这了,ldapoperutils这个类提供了ldap操作大多数要使用的方法。所有方法在实际工作中都使用过。
对于各位有什么更好的想法,或需要的操作,没有提供到,请与我联系,共同讨论。
闽公网安备 35060202000074号