哈喽小伙伴们,我又来啦!你们上篇博客的步骤都成功了吗?有疑问的请在评论处写出,我会尽最大努力帮助你们。咱们接着上篇博客来说,接下来是修改和删除操作啦!
上篇博客地址:https://blog.csdn.net/IT_0417/article/details/83897748
下面是修改代码:
package com.spring.ldap;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPModification;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author jianning
* @description 修改操作示例
* @date 2018/11/8
* @time 14:52
*/
public class LDAPModifyAttrs {
public static void main(String[] args) {
String ldapHost = "localhost";
String loginDN = "cn=Manager,dc=maxcrc,dc=com";
String password = "secret";
//要修改的用户的entryDN,uid不可修改
String modifyDN = "uid=test,ou=Employee,dc=maxcrc,dc=com";
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
LDAPConnection lc = new LDAPConnection();
List<LDAPModification> modList = new ArrayList<LDAPModification>();
// 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 labeledURI address with a new value
attribute = new LDAPAttribute("labeledURI", "www.micmiu.com");
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);
try {
lc.connect(ldapHost, ldapPort);
lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
lc.modify(modifyDN, mods);
System.out.println("LDAPAttribute add、replace、delete all successful.");
} catch (LDAPException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
System.out.println("Error: " + e.toString());
} finally {
try {
if (lc.isConnected()) {
lc.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
下面我贴出修改前后的对比,你们就更加理解啦!(不知道修改前的属性可以翻看我上一篇博客~~)
接下来就是最残忍的一步啦,我们要将创建、修改好的用户给pass掉,下面是代码:
package com.spring.ldap;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import java.io.UnsupportedEncodingException;
/**
* @author jianning
* @description 删除条目的示例
* @date 2018/11/8
* @time 14:44
*/
public class LDAPDeleteEntry {
public static void main(String[] args) {
String ldapHost = "localhost";
String loginDN = "cn=Manager,dc=maxcrc,dc=com"; //要执行ou的creatorsName
String password = "secret";
String deleteDN = "uid=test,ou=Employee,dc=maxcrc,dc=com";//要删除的用户entryDN
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
LDAPConnection lc = new LDAPConnection();
try {
lc.connect(ldapHost, ldapPort);
lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
lc.delete(deleteDN);
System.out.println(" delete Entry: " + deleteDN + " success.");
lc.disconnect();
} catch (LDAPException e) {
if (e.getResultCode() == LDAPException.NO_SUCH_OBJECT) {
System.err.println("Error: No such object");
} else if (e.getResultCode() == LDAPException.INSUFFICIENT_ACCESS_RIGHTS) {
System.err.println("Error: Insufficient rights");
} else {
System.err.println("Error: " + e.toString());
}
} catch (UnsupportedEncodingException e) {
System.out.println("Error: " + e.toString());
} finally {
try {
if (lc.isConnected()) {
lc.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
右键Run,刷新ldap工具就可以看到,我们的test用户不见啦。
不知道小伙伴们都成功了没有,我有几点要说明一下:
1.上篇博客中有写到需求,还需要做到ldap和mysql数据动态同步,但是我今天没有整理好代码,所以就在下篇博客中写出吧~
2.代码中的loginDN是要执行ou的creatorsName;要操作的DN是用户的entryDN。代码中我都加了注释,如果没有找到这些属性,我建议大家不要急于求成,还是多看一些ldap的博客吧!
好啦,今天是周五,写篇博客也算是对我一周的总结。前两天看了一篇文章,一名程序员30岁之前的路:程序员->架构师->Team Leader(Project Manager)->技术总监,看完之后想了想自己还像白纸一样神马都不会啊,压力山大!当然了,我认为不论从事什么行业,都需要我们付出很大努力,没有人的成功是轻轻松松的,祝大家在程序员的道路越走越远~~