封装JNDI操作LDAP服务器的工具类(5)

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

目标:使用者只需要会使用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操作大多数要使用的方法。所有方法在实际工作中都使用过。

对于各位有什么更好的想法,或需要的操作,没有提供到,请与我联系,共同讨论。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航