OpenLDAP在 JAVA中添加,删除
今天终于把添加,删除,修改节点名,属性,遍历节点都弄出来了,先把代码贴出来吧
/**
*
* @author chenyi
*/
import java.util.Hashtable;
import javax.naming.directory.*;
import java.util.*;
import javax.naming.*;
public class ChenYi {
DirContext dc = null;
String account = "Manager";//操作LDAP的帐户。默认就是Manager。
String password = "secret";//帐户Manager的密码。
String root = "dc=example,dc=com"; //LDAP的根节点的DC
public ChenYi() {
init();
//add();//添加节点
//delete("ou=hi,dc=example,dc=com");//删除"ou=hi,dc=example,dc=com"节点
//modifyInformation("ou=hi,dc=example,dc=com");//修改"ou=hi,dc=example,dc=com"属性
//renameEntry("ou=new,o=neworganization,dc=example,dc=com","ou=neworganizationalUnit,o=neworganization,dc=example,dc=com");//重命名节点"ou=new,o=neworganization,dc=example,dc=com"
searchInformation("dc=example,dc=com", "", "(objectclass=*)");//遍历所有根节点
//searchInformation("o=neworganization,dc=example,dc=com","","(objectclass=*)");//遍历指定节点的分节点
close();
}
public void init() {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.100.221:389/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
dc = new InitialDirContext(env);//初始化上下文
System.out.println("认证成功");//这里可以改成异常抛出。
} catch (javax.naming.AuthenticationException e) {
System.out.println("认证失败");
} catch (Exception e) {
System.out.println("认证出错:" + e);
}
}
public void close() {
if (dc != null) {
try {
dc.close();
} catch (NamingException e) {
System.out.println("NamingException in close():" + e);
}
}
}
public void add() {
try {
String newUserName = "hi";
BasicAttributes attrs = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectClass");
objclassSet.add("top");
objclassSet.add("organizationalUnit");
attrs.put(objclassSet);
attrs.put("ou", newUserName);
dc.createSubcontext("ou=" + newUserName + "," + root, attrs);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception in add():" + e);
}
}
public void delete(String dn) {
try {
dc.destroySubcontext(dn);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception in delete():" + e);
}
}
public boolean modifyInformation(String dn) {
try {
ModificationItem[] mods = new ModificationItem[1];
/*添加属性*/
// Attribute attr0 = new BasicAttribute("description",
// "测试");
// mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,attr0);
/*修改属性*/
// Attribute attr0 = new BasicAttribute("description", "陈轶");
// mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
// attr0);
/*删除属性*/
Attribute attr0 = new BasicAttribute("description",
"陈轶");
mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
attr0);
dc.modifyAttributes(dn, mods);
return true;
} catch (NamingException ne) {
ne.printStackTrace();
System.err.println("Error: " + ne.getMessage());
return false;
}
}
/**
* @param base :根节点(在这里是"dc=example,dc=com")
* @param scope :搜索范围,分为"base"(本节点),"one"(单层),""(遍历)
* @param filter :指定子节点(格式为"(objectclass=*)",*是指全部,你也可以指定某一特定类型的树节点)
*/
public void searchInformation(String base, String scope, String filter) {
SearchControls sc = new SearchControls();
if (scope.equals("base")) {
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
} else if (scope.equals("one")) {
sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
} else {
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
}
NamingEnumeration ne = null;
try {
ne = dc.search(base, filter, sc);
// Use the NamingEnumeration object to cycle through
// the result set.
while (ne.hasMore()) {
System.out.println();
SearchResult sr = (SearchResult) ne.next();
String name = sr.getName();
if (base != null && !base.equals("")) {
System.out.println("entry: " + name + "," + base);
} else {
System.out.println("entry: " + name);
}
Attributes at = sr.getAttributes();
NamingEnumeration ane = at.getAll();
while (ane.hasMore()) {
Attribute attr = (Attribute) ane.next();
String attrType = attr.getID();
NamingEnumeration values = attr.getAll();
Vector vals = new Vector();
// Another NamingEnumeration object, this time
// to iterate through attribute values.
while (values.hasMore()) {
Object oneVal = values.nextElement();
if (oneVal instanceof String) {
System.out.println(attrType + ": " + (String) oneVal);
} else {
System.out.println(attrType + ": " + new String((byte[]) oneVal));
}
}
}
}
} catch (Exception nex) {
System.err.println("Error: " + nex.getMessage());
nex.printStackTrace();
}
}
public boolean renameEntry(String oldDN, String newDN) {
try {
dc.rename(oldDN, newDN);
return true;
} catch (NamingException ne) {
System.err.println("Error: " + ne.getMessage());
return false;
}
}
public static void main(String[] args) {
new ChenYi();
}
}
OpenLDAP在 JAVA中添加,删除相关推荐
- java注释中添加删除线_如何在Word中添加和删除注释
java注释中添加删除线 Leaving comments in a Word document is a good way to leave feedback on a piece you're r ...
- java创建的窗口无法关闭_在Java中添加canvas后无法关闭窗口(Can't close window after adding canvas in Java)...
在Java中添加canvas后无法关闭窗口(Can't close window after adding canvas in Java) public class Screen extends Ca ...
- Java中List 删除元素方法參考
Java中List 删除元素 Java中List 删除元素会使集合大小减少,合理删除的一些方法. 1:删除后的元素i-1 删除一个元素后,后边的元素左移1位,此时i-1,保证了下次循环能访问到左移了1 ...
- java soap 头_如何在Java中添加Soap标头
我有一个来自oracle的NO.net Web服务,要访问,我需要添加soap标头.如何在Java中添加soap标头? Authenticator.setDefault(new ProxyAuthen ...
- Java中遍历删除ArrayList中多个元素
Java中遍历删除ArrayList多个元素 Iterator itr = list.iterator(); while(itr.hasNext()) {int i = itr.next();if ( ...
- redis cluster中添加删除重分配节点例子
redis cluster中添加删除重分配节点例子 作者:用户 来源:互联网 时间:2016-05-05 10:22:27 摘要: 本文讲的是redis cluster中添加删除重分配节点例子, re ...
- Java 中添加背景音乐
Java 中添加背景音乐 GUI 中添加背景音乐 添加循环播放的背景音乐 背景音乐音量调节 好了,以上主要是多放几个 关键词,方便能让需要的人都能搜到 以下就是可直接使用的方法, 之后按提示导包就 o ...
- java中如何删除文件或清除文件夹下的所有文件
java中如何删除文件或清除文件夹下的所有文件呢?现实中如果一个java项目中涉及非常多的删除文件操作,我们最好单独写一个函数实现文件的删除操作,以下是函数的实现: // 删除文件 或者 清除文件夹下 ...
- 在浏览器中添加删除页面元素
在浏览器中添加删除页面元素 一.添加元素 确定添加元素位置,并找到容器元素. 新建一个填写内容步骤,添加填写项目,获取容器元素,填写属性为添加子元素addchild,填写内容为新元素的html代码. ...
最新文章
- 如何查找基因在发表研究中的表达
- word图片自动换行不亮_Word自动换行版式不变形
- JS、CSS中的相对路径
- android AtomicBoolean类的使用
- windows nt service 框架
- powershell 遍历json_使用PowerShell处理JSON字符串
- Android版CCLabelTTF在setstring时出现黑块
- Android png模拟svg,Android 中使用svg图片
- 【Docker】OCI runtime exec failed: exec failed: container_linux.go:348: starting container process cau
- Python并发编程之多进程(实战)
- C#最简单的NetMeeting
- Unity Shader (二)
- oracle remap schema,oracle 10g DATA PUMP 的REMAP_SCHEMA和REMAP_TABLESPA
- 支付宝AR实景红包寻宝攻略
- 软考java题目_软件工程软考题目总结
- 腾讯云服务器评测:配置 8核 16G 18M 带宽
- 【java】drools入门之LHS和RHS语法
- 英语语法总结--名词
- 年仅19岁!西班牙最危险黑客被捕
- 组策略添加受信任站点