今天终于把添加,删除,修改节点名,属性,遍历节点都弄出来了,先把代码贴出来吧
/**
*
* @author
*/
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();
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhangjunfangkaixin/archive/2009/03/04/3957198.aspx
分享到:
相关推荐
java代码操作linux上的openldap目录服务器,进行增删改查操作
Openldap在Ubuntu中的安装部署
openldap ,这个压缩包十分完整,带浏览器
对openldap进行增加、删除、修改、查询、用户进行密码验证
windows版的openldap,适合于本地开发测试用
openldap浏览器的Java代码实现
openldap安装配置 java操作ldap
windows下openldap的安装与java操作测试 windows下openldap的安装与java操作测试
openldap在windows安装配置.pdf
openssh-ldap-publickey, 用于在OpenLDAP条目中存储 public 密钥的OpenSSH的包装器 openssh-ldap-publickey用于在OpenLDAP条目中存储 public 密钥的OpenSSH的包装器。:它是如何工作的?从OpenLdap为用户创建条目,...
OpenLDAP文档.pptx
OpenLdap搭建配置日志索引,自己开发过程中踩的坑记录下来的。包好使。
主讲OpenLdap安装及配置,OpenLdap是配置ldap的服务器。
站点管理工具2.1软件环境: Linux + php + OpenLDAP +ProFTPD 硬件环境: IA-32 开发工具: Dreamweaver 项目描述: Site Manager...OpenLDAP 管理工具,可实现对dn的:浏览、过滤、添加、修改、删除 。支持GB18030
附近为整理好的openldap工程,可以直接在VC9上编译openldap
openldap 开启TLS全步骤
openldap-2.5.4
OpenLDAP2.4管理员指南 OpenLDAP2.4管理员指南
部署OpenLDAP是在服务器上安装和配置OpenLDAP软件的过程,以搭建一个LDAP(轻型目录访问协议)服务器。OpenLDAP是一个开源的、跨平台的LDAP实现,可用于存储和管理用户、组和其他目录相关的信息。 以下是部署...
openLDAP 管理员手册