java ldap 操作_JAVA操作LDAP的详解(JLDAP)

最近两周由于要学习测试LDAP,所以对于用脚本操作LDAP很感兴趣,所以就做了一些脚本,都是比较简单的脚本吧。

废话不多说了哈。直接上教程

首先声明:我使用的是JLDAP操作LDAP,所以需要从官网下载一个JAR包,这里你们自己百度一下就能找到了。

第一步,一切的开端!!!!链接LDAP

import com.novell.ldap.LDAPAttribute;

import com.novell.ldap.LDAPConnection;

import com.novell.ldap.LDAPException;

import com.novell.ldap.LDAPModification;

private static String ldapHost = "xxxx";

private static int ldapPort = 389;

private static String ldapBindDN = "xxx";;

private static String ldapPassword = "password";

private static int ldapVersion = LDAPConnection.LDAP_V3;

@SuppressWarnings("deprecation")

public static void connection() throws LDAPException

{

String modifyDn="cn=Staff,ou=People,dc=lisys,dc=cn";

LDAPConnection lc = new LDAPConnection();

lc.connect(ldapHost, ldapPort);

lc.bind(ldapVersion, ldapBindDN, ldapPassword);

}

对于LDAP的密码有时候需要转换一下格式。那么就用这个代码试一下password.getBytes("UTF8")

第二:对于LDAP的添加操作

LDAPAttributeSet attributeSet = new LDAPAttributeSet();

attributeSet.add(new LDAPAttribute("objectclass", new String(

"inetOrgPerson")));

attributeSet.add(new LDAPAttribute("cn", "wuwo"));

attributeSet.add(new LDAPAttribute("sn", "Sun"));

attributeSet.add(new LDAPAttribute("mail", "837955487@qq.com"));

attributeSet.add(new LDAPAttribute("Blog",

""));

attributeSet.add(new LDAPAttribute("userPassword", "111111"));

attributeSet.add(new LDAPAttribute("uid", "addnew"));

String dn = "uid=wuwo,ou=Developer,"+containerName;

/**就是一个DN路径,以上为添加属性。自己可以进行删减之类的。objectclass注意一下,查看LDAP手册之类的看都能规定什么值*/

LDAPEntry newEntry = new LDAPEntry(dn, attributeSet);

lc.connect(ldapHost, ldapPort);

lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));

System.out.println("login ldap server successfully.");

lc.add(newEntry);

System.out.println("Added object: " + dn + " successfully.");

第三:修改属性,包括添加属性,修改属性,删除属性。

LDAPConnection lc = new LDAPConnection();

List modList = new ArrayList();

// Add a new value to the description attribute

String desc = "This object was modified at " + new Date();

LDAPAttribute attribute = new LDAPAttribute("description", desc);

modList.add(new LDAPModification(LDAPModification.ADD, attribute));

attribute = new LDAPAttribute("telephoneNumber", "180-8888-xxxx");

modList.add(new LDAPModification(LDAPModification.ADD, attribute));

// Replace the Blog address with a new value

attribute = new LDAPAttribute("Blog", "http://www.cnblogs.com/demonwuwo/");

modList.add(new LDAPModification(LDAPModification.REPLACE, attribute));

// delete the email attribute

attribute = new LDAPAttribute("mail");

modList.add(new LDAPModification(LDAPModification.DELETE, attribute));

LDAPModification[] mods = new LDAPModification[modList.size()];

mods = (LDAPModification[]) modList.toArray(mods);

lc.connect(ldapHost, ldapPort);

lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));

lc.modify(modifyDN, mods);

System.out.println("LDAPAttribute add、replace、delete all successful.");

第四:删除某一个树

LDAPConnection lc = new LDAPConnection();

lc.connect(ldapHost, ldapPort);

lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));

lc.delete(“需要删除的树的DN”);

System.out.println(" delete Entry: " + deleteDN + " success.");

lc.disconnect();

反正基本上的操作也就这样。希望能够跟大家交流~~

PS:在删除里如果一个树下有很多相同名字的属性,那么你也可以删除其中的一个,只要删除的时候,

attribute = new LDAPAttribute("mail",content);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值