1. 认证信息:
package com.royal.jldap; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; /** * @Description JAVA LDAP 认证信息 * * @author 萧_瑟 * @version 1.0 创建时间 2013-1-22 * */ public class LDAPTest { private static String ldapURL = "ldap://192.168.xx.xx:389/"; public static void main(String[] args) { rootCredentials(); xsCredentials(); } // database bdb // suffix "dc=gzis,dc=ac.cn" // checkpoint 1024 15 // rootdn "cn=Manager,dc=gzis,dc=ac.cn" // rootpw secret public static void rootCredentials() { String root = "cn=Manager,dc=gzis,dc=ac.cn";// root Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, root); env.put(Context.SECURITY_CREDENTIALS, "secret"); DirContext ctx = null; try { ctx = new InitialDirContext(env); System.out.println("root认证成功"); } catch (NamingException e) { e.printStackTrace(); System.out.println("root认证失败"); } catch (Exception e) { System.out.println("root认证出错:"); e.printStackTrace(); } if (ctx != null) { try { ctx.close(); } catch (NamingException e) { e.printStackTrace(); } } } // dn: cn=xiao se,ou=people,dc=gzis,dc=ac.cn // objectClass: inetOrgPerson // cn: xiao se // sn: xiao se // uid: xs // userPassword: 123456 // description: 高级工程师 public static void xsCredentials() { String root = "cn=xiao se,ou=people,dc=gzis,dc=ac.cn"; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, root); env.put(Context.SECURITY_CREDENTIALS, "123456"); DirContext ctx = null; try { ctx = new InitialDirContext(env); System.out.println("xiao se认证成功"); } catch (NamingException e) { e.printStackTrace(); System.out.println("xiao se认证失败"); } catch (Exception e) { System.out.println("xiao se认证出错:"); e.printStackTrace(); } if (ctx != null) { try { ctx.close(); } catch (NamingException e) { e.printStackTrace(); } } } }
2. 增加信息:
package com.royal.jldap; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; /** * @Description JAVA LDAP 创建/添加数据 * * @author 萧_瑟 * @version 1.0 创建时间 2013-1-23 * */ public class LDAPAdd { private static String ldapURL = "ldap://192.168.xx.xx:389/"; public static void main(String[] args) { String account = "Manager";// 操作LDAP的帐户。默认就是Manager。 String password = "secret";// 帐户Manager的密码。 String root = "dc=gzis,dc=ac.cn"; // LDAP的根节点的DC // 添加失败,无添加权限,运行报错 // String account = "xiao se"; // String password = "123456"; // String root = "ou=people,dc=gzis,dc=ac.cn"; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root); env.put(Context.SECURITY_CREDENTIALS, password); DirContext ctx = null; try { // 初始化上下文 ctx = new InitialDirContext(env); System.out.println("root认证成功"); // 创建一个组 // String ou = "testGroup"; // BasicAttributes attrs = new BasicAttributes(); // BasicAttribute objclassSet = new BasicAttribute("objectClass"); // objclassSet.add("organizationalUnit"); // attrs.put(objclassSet); // attrs.put("ou", ou); // ctx.createSubcontext("ou=" + ou + "," + root, attrs); //添加一个用户 BasicAttributes attrs = new BasicAttributes(); BasicAttribute objclassSet = new BasicAttribute("objectClass"); objclassSet.add("inetOrgPerson"); attrs.put(objclassSet); attrs.put("cn", "admin@gziscas"); attrs.put("userPassword", "123456"); attrs.put("uid", "admin@gziscas"); attrs.put("mail", "xx@xxx.com"); attrs.put("sn", "admin"); attrs.put("homePhone", "110xxxxxx"); attrs.put("description", "租户管理员"); ctx.createSubcontext("cn=admin@gziscas,ou=people," + root, attrs); } catch (Exception e) { e.printStackTrace(); } if (ctx != null) { try { ctx.close(); } catch (NamingException e) { e.printStackTrace(); } } } }
3. 删除信息:
package com.royal.jldap; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; /** * @Description JAVA LDAP 删除数据 * * @author 萧_瑟 * @version 1.0 创建时间 2013-1-23 * */ public class LDAPDelete { private static String ldapURL = "ldap://192.168.xx.xx:389/"; public static void main(String[] args) { String account = "Manager";// 操作LDAP的帐户。默认就是Manager。 String password = "secret";// 帐户Manager的密码。 String root = "dc=gzis,dc=ac.cn"; // LDAP的根节点的DC // 删除失败,无删除权限,运行不报错 // String account = "xiao se"; // String password = "123456"; // String root = "ou=people,dc=gzis,dc=ac.cn"; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root); env.put(Context.SECURITY_CREDENTIALS, password); DirContext ctx = null; try { // 初始化上下文 ctx = new InitialDirContext(env); System.out.println("root认证成功"); // 删除 ctx.destroySubcontext("cn=admin@gziscas,ou=people," + root); } catch (Exception e) { e.printStackTrace(); } if (ctx != null) { try { ctx.close(); } catch (NamingException e) { e.printStackTrace(); } } } }
4. 查询信息:
package com.royal.jldap; import java.util.Enumeration; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; /** * @Description JAVA LDAP 数据查询 * * @author 萧_瑟 * @version 1.0 创建时间 2013-1-23 * */ public class LDAPSearch { private static String ldapURL = "ldap://192.168.xx.xx:389/"; public static void main(String[] args) { String account = "Manager";// 操作LDAP的帐户。默认就是Manager。 String password = "secret";// 帐户Manager的密码。 String root = "dc=gzis,dc=ac.cn"; // LDAP的根节点的DC Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root); env.put(Context.SECURITY_CREDENTIALS, password); DirContext ctx = null; try { //初始化上下文 ctx = new InitialDirContext(env); System.out.println("root认证成功"); //查询 SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); // constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE); // 查询所有用户 // NamingEnumeration en = ctx.search(root, "uid=*", constraints); NamingEnumeration en = ctx.search(root, "objectclass=*", constraints); // NamingEnumeration en = ctx.search(root, "ou=*", constraints); // NamingEnumeration en = ctx.search(root, "description=*", constraints); while (en != null && en.hasMoreElements()) { Object obj = en.nextElement(); if (obj instanceof SearchResult) { SearchResult si = (SearchResult) obj; System.out.println("name:" + si.getName()); Attributes attrs = si.getAttributes(); if (attrs == null) { System.out.println("No attributes "); } else { for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();) { Attribute attr = (Attribute) ae.next(); String attrId = attr.getID(); for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) { System.out.print(attrId + ": "); Object o = vals.nextElement(); if (o instanceof byte[]) { System.out.println(new String((byte[]) o)); } else { System.out.println(o); } } } } } else { System.out.println(obj); } System.out.println(); } } catch (NamingException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } if (ctx != null) { try { ctx.close(); } catch (NamingException e) { e.printStackTrace(); } } } }
5. 修改信息:
package com.royal.jldap; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.BasicAttribute; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.ModificationItem; /** * @Description JAVA LDAP 修改数据 * * @author 萧_瑟 * @version 1.0 创建时间 2013-1-23 * */ public class LDAPModify { private static String ldapURL = "ldap://192.168.xx.xx:389/"; public static void main(String[] args) { String account = "Manager";// 操作LDAP的帐户。默认就是Manager。 String password = "secret";// 帐户Manager的密码。 String root = "dc=gzis,dc=ac.cn"; // LDAP的根节点的DC // 修改失败,无修改权限,运行报错 // String account = "xiao se"; // String password = "123456"; // String root = "ou=people,dc=gzis,dc=ac.cn"; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root); env.put(Context.SECURITY_CREDENTIALS, password); DirContext ctx = null; try { // 初始化上下文 ctx = new InitialDirContext(env); System.out.println("root认证成功"); // 修改 String description = "屌丝逆袭"; String userPassword = "123456"; ModificationItem modificationItem[] = new ModificationItem[2]; modificationItem[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("description", description)); modificationItem[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userPassword", userPassword)); ctx.modifyAttributes("cn=admin@gziscas,ou=people," + root, modificationItem); } catch (Exception e) { e.printStackTrace(); } if (ctx != null) { try { ctx.close(); } catch (NamingException e) { e.printStackTrace(); } } } }
demo前提:已经配置好了LDAP服务器。
参考:
http://blog.sina.com.cn/s/blog_63533db30100mf05.html
http://spiritfrog.iteye.com/blog/390994
http://blog.163.com/lang_zi_ming/blog/static/1140161762012470537349/
相关推荐
java操作Ldap实现增删改查
基于springldap的增删改查,以及通过ldap的用户名、密码进行验证
springldap增删改查的源码 适合初学者下载学习 写的很简答 注释中加入了自己写代码遇到的问题解决方案和心得
用C#写的对数据库进行操作,包含了对数据库的增删改查等功能。是一个用C#对数据库进行访问最好的,最易懂的例子,希望学习C#操作数据库的朋友多参考交流
本例较简单的实现了LDAP的增删改查功能,分类关于java接口和c语言接口都有说明,适用于初学者
java Ldap增删改查代码。ldap样例数据,可用来导入。
不错的JAVA操作LDAP实例,有源码.LDAP 验证、新增、修改、查询用户
ldap的增删改查的源码 以及批量增加批量删除的代码 随便带的有自己练习的一点心得注释
具体的JNDI对openLDAP的操作,java对ldapd增删改查
SpringLDAP和JNDI增删改查的入门级例子 能够通过编译和运行 已经包含了需要的包,用eclipse导入即可
项目中使用spring boot框架集成了ldap协议的方法,调用了增删改查的API,基本实现了spring boot对于ldap的所有方法,只要搭建好了ldap服务器,改一下配置文件即可运行。
JAVA操作LDAP的增删改查 包括连接已经属性的更新操作
spring-ldap1.3.1,一种框架技术用来实现对ldap服务器增删改查数据的工具包。
JAVA通过LDAP+SSL(证书)实现用户和组织(部门)增删改查(Java代码部分),里面包含源码和文档,以便大家测试。
物超所值,也是突然接了这么个东西,无文档,无经验,网上介绍也少。然后硬着头皮做了,包含组织的增删改,用户的增删改还有特定用户的查询。保证能用。报53代号的请确保自己的服务器设置正确。
Ldap简易客户端.zip
ldap开发测试程序,代码程序包括基本的ldap数据的增删改查操作。
rar包:一个Web工程, 主要有,利用Spring-ldap对LDAP的基本操作(查询,增删改);Extjs实现的对Ldap的树状结构的显示,结构有点类似Softerra LDAP;一个测试类。 pdf:spring-ldap-reference.pdf Extjs.pdf ...
LDAP学习资料,里面含有ldap的增、删、改、查操作,是基于spring的。
使用spring-ldap-core测试链接ldap com.ldap.test.service包下为Junit测试类