javaldap操作_JAVA操作LDAP的详解(JLDAP)最近两周由于要学习测试LDAP,所以对于⽤脚本操作LDAP很感兴趣,所以就做了⼀些脚本,都是⽐较简单的脚本吧。
废话不多说了哈。直接上教程
⾸先声明:我使⽤的是JLDAP操作LDAP,所以需要从官⽹下载⼀个JAR包,这⾥你们⾃⼰百度⼀下就能找到了。
第⼀步,⼀切的开端链接LDAP
vell.ldap.LDAPAttribute;
vell.ldap.LDAPConnection;
vell.ldap.LDAPException;
vell.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.bind(ldapVersion, ldapBindDN, ldapPassword);
}
对于LDAP的密码有时候需要转换⼀下格式。那么就⽤这个代码试⼀下Bytes("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", ""));
attributeSet.add(new LDAPAttribute("Blog",
""));
attributeSet.add(new LDAPAttribute("urPassword", "111111"));
attributeSet.add(new LDAPAttribute("uid", "addnew"));
String dn = "uid=wuwo,ou=Developer,"+containerName;
/**就是⼀个DN路径,以上为添加属性。⾃⼰可以进⾏删减之类的。objectclass注意⼀下,查看LDAP⼿册之类的看都能规定什么值*/ LDAPEntry newEntry = new LDAPEntry(dn, attributeSet);
呼叫转移怎么取消lc.bind(ldapVersion, loginDN, Bytes("UTF8"));
System.out.println("login ldap rver 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", "/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[]) Array(mods);
lc.bind(ldapVersion, loginDN, Bytes("UTF8"));
出纳员的岗位职责
简谱知识System.out.println("LDAPAttribute add、replace、delete all successful.");
第四:删除某⼀个树
LDAPConnection lc = new LDAPConnection();
小幽默笑话lc.connect(ldapHost, ldapPort);
lc.bind(ldapVersion, loginDN, Bytes("UTF8"));
lc.delete(“需要删除的树的DN”);
System.out.println(" delete Entry: " + deleteDN + " success.");
lc.disconnect();
反正基本上的操作也就这样。希望能够跟⼤家交流~~
PS:在删除⾥如果⼀个树下有很多相同名字的属性,那么你也可以删除其中的⼀个,只要删除的时候,attribute = new LDAPAttribute("mail",content);