OpenLDAP implementation of the Lightweight Directory Access Protocol. http://www.openldap.org/
Windows 下安装 LDAP:
1、下载下面附件中的 openldap-2.2.29-db-4.3.29-openssl-0.9.8a-win32_Setup.rar 解压后安装,安装的时候一直点击“下一步”即可。
2、配置LDAP,假设我们安装到了“C:\Program Files\OpenLDAP”,在该目录中找到 slapd.conf 文件,修改文件内容。在 include ./schema/core.schema 后面添加如下内容:
include ./schema/cosine.schema include ./schema/inetorgperson.schema include ./schema/corba.schema include ./schema/dyngroup.schema include ./schema/java.schema include ./schema/misc.schema include ./schema/nis.schema include ./schema/openldap.schema
找到
suffix "dc=my-domain,dc=com"
rootdn "cn=Manager,dc=my-domain,dc=com"
将如上两行修改为
suffix "o=tcl,c=cn"
rootdn "cn=Manager,o=tcl,c=cn"
下面的
rootpw secret
是Manager的明文密码。
3、启动 OpenLDAP ,CMD切换到“C:\Program Files\OpenLDAP” 然后执行如下命令
slapd -d 1
4、建立新的条目
创建一个文本文件 item.ldif 其内容如下:
dn: o=tcl,c=cn objectClass: dcObject objectClass: organization o: tcl dc: com dn: uid=Unmi, o=tcl,c=cn uid: Unmi objectClass: inetOrgPerson mail: [email protected] userPassword:: MTIzNDU2 labeledURI: http://unmi.blogcn.com sn: Qiu cn:: 6ZqU5Y+26buE6I66
执行命令:ldapadd -x -D "cn=manager,o=tcl,c=cn" -w secret -f item.ldif ,将内容导入到ldap中。
5、查看LDAP中的条目
下载下面附件 "LdapAdmin.rar" ,将其解压无需安装。配置连接信息:
连接成功之后,查看刚刚创建的条目:
6、Java读取条目
package com.neusoft.util; 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; public class LDAPTest { public static void main(String[] args) { LDAPTest LDAPTest1 = new LDAPTest(); String root = "o=tcl,c=cn" ; //root Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" ); env.put(Context.PROVIDER_URL, "ldap://localhost/" + root); env.put(Context.SECURITY_AUTHENTICATION, "simple" ); env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,o=tcl,c=cn" ); env.put(Context.SECURITY_CREDENTIALS, "secret" ); DirContext ctx = null ; try { ctx = new InitialDirContext(env); System.out.println( "Ldap连接成功" ); SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration en = ctx.search("", "(uid=Unmi)", constraints); while(en!=null && en.hasMoreElements()){ Object obj = en.nextElement() ; if (obj instanceof SearchResult) { SearchResult result = (SearchResult) obj; result.getName(); Attributes attrs = result.getAttributes(); System.out.println(attrs.toString()); Attribute attr = attrs.get("userPassword"); String str = new String((byte[])attr.get()); System.out.println(str); } } } catch (javax.naming.AuthenticationException e) { e.printStackTrace(); System.out.println( "Ldap连接失败" ); } catch (Exception e) { System.out.println( "认证出错:" ); e.printStackTrace(); } if (ctx != null ) { try { ctx.close(); } catch (NamingException e) { //ignore } } } }