JLDAP

参考文档:

http://developer.novell.com/documentation/samplecode/jldap_sample/index.htm

 

package com.ebizserve.cp.jldap;

import com.novell.ldap.*;

public class TestJLDAP {

	public static void main(String[] args) throws LDAPException {

		LDAPConnection lc = new LDAPConnection();

		try {
			lc.connect( ldapHost, ldapPort );
            lc.bind( ldapVersion, loginDN, password.getBytes("UTF8"));
			// addOrg(lc);
			add(lc);
			search(lc);
			delete(lc);
		} catch (LDAPException e) {
			System.err.print("连接异常!   ");
			e.printStackTrace();
			throw e;
		}

		if (lc.isConnected()) {
			lc.disconnect();
		}

	}

	public static void add(LDAPConnection lc) throws LDAPException {
		LDAPAttributeSet attributeSet = new LDAPAttributeSet();
		attributeSet.add(new LDAPAttribute("objectclass", new String[] { "top",
				"posixAccount", "person", "inetOrgPerson",
				"organizationalPerson" }));

		attributeSet.add(new LDAPAttribute("cn", "a"));
		attributeSet.add(new LDAPAttribute("sn", "b"));
		attributeSet.add(new LDAPAttribute("uidNumber", "1"));
		attributeSet.add(new LDAPAttribute("gidNumber", "1"));
		attributeSet.add(new LDAPAttribute("homeDirectory", "1"));
		attributeSet.add(new LDAPAttribute("givenName", "测试用人员"));
		LDAPEntry entry = new LDAPEntry("uid=ab,ou=test,dc=test,dc=com",
				attributeSet);

		lc.add(entry);
		System.out.println("成功的添加了ab!");

	}

	public static void delete(LDAPConnection lc) throws LDAPException {
		lc.delete("uid=ab,ou=test,dc=test,dc=com");
		System.out.println("成功的删除了ab!");

	}

	public static void search(LDAPConnection lc) throws LDAPException {

		LDAPSearchResults rs = lc.search("ou=test,dc=test,dc=com",
				LDAPConnection.SCOPE_SUB, "objectClass=posixAccount", null,
				false);

		while (rs.hasMore()) {
			LDAPEntry entry = rs.next();
			System.out.println(entry.getAttribute("uid").getStringValue()
					+ entry.getAttribute("givenName").getStringValue() + " "
					+ entry.getAttribute("sn").getStringValue());
		}
	}

	public static void addOrg(LDAPConnection lc) throws LDAPException {
		LDAPAttributeSet attributeSet = new LDAPAttributeSet();
		attributeSet
				.add(new LDAPAttribute("objectclass", "organizationalUnit"));

		attributeSet.add(new LDAPAttribute("ou", "test"));

		LDAPEntry entry = new LDAPEntry("ou=test,dc=test,dc=com",
				attributeSet);

		lc.add(entry);
		System.out.println("成功的添加了org!");
	}

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值