OpenLdap安装与测试
????? OpenLDAP是轻型目录访问协议(Lightweight Directory Access Protocol,LDAP)的自由和开源的实现,在其OpenLDAP许可证下发行,并已经被包含在众多流行的Linux发行版中。
它主要包括下述4个部分:
slapd - 独立LDAP守护服务
slurpd - 独立的LDAP更新复制守护服务
实现LDAP协议的库
工具软件和示例客户端
## 定义DIT(directory information tree 目录信息树)的 ROOT/BASE/SUFFIX###### replace example and com as necessary below## or for experimentation leave as is## dcObject is an AUXILLIARY objectclass and MUST## have a STRUCTURAL objectclass (organization in this case)# this is an ENTRY sequence and is preceded by a BLANK linedn: dc=lcl,dc=comdc: lcldescription: test lcl descriptionobjectClass: dcObjectobjectClass: organizationo: lcl, Inc.## FIRST Level hierarchy – users ## uses mixed upper and lower case for objectclass# this is an ENTRY sequence and is preceded by a BLANK linedn: ou=users, dc=lcl,dc=comou: usersdescription: All users in organisationobjectclass: organizationalunit## SECOND Level hierarchy## ADD a single entry under FIRST (people) level# this is an ENTRY sequence and is preceded by a BLANK line# the ou: Human Resources is the department namedn: cn=Robert Smith,ou=users,dc=lcl,dc=comobjectclass: inetOrgPersoncn: Robert Smithcn: Robert J Smithcn: bob smithsn: smithuid: rjsmithuserpassword: rJsmitHcarlicense: HISCAR 123homephone: 555-111-2222mail: r.smith@example.commail: rsmith@example.commail: bob.smith@example.comdescription: swell guyou: Human Resources?通过命令C:\OpenLDAP>ldapadd.exe -x -D "cn=admin,dc=lcl,dc=com" -f e:\step-1.ldif -w 1234abcd@导入ldif中的数据
package test;import java.util.Hashtable;import javax.naming.Context;import javax.naming.NamingEnumeration;import javax.naming.NamingException;import javax.naming.directory.DirContext;import javax.naming.directory.InitialDirContext;import javax.naming.directory.SearchControls;import javax.naming.directory.SearchResult;public class TestJndiLdapSearch { public static void main(String[] args) { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=lcl,dc=com"); env.put(Context.SECURITY_CREDENTIALS, "1234abcd@"); String root = "dc=lcl,dc=com"; try { DirContext context = new InitialDirContext(env); // 使用方式见jdk1.6的InitialDirContext的search // Specify the ids of the attributes to return String[] attrIDs = { "sn", "telephonenumber", "name", "mail" }; // Create the default search controls SearchControls ctls = new SearchControls(); ctls.setReturningAttributes(attrIDs); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(&(cn=*))"; // Search for objects that have those matching attributes NamingEnumeration enum = context.search(root, filter, ctls); while (enum.hasMore()) { SearchResult result = (SearchResult) enum.next(); System.out.println("name : " + result.getName()); } } catch (NamingException e) { e.printStackTrace(); } }}? ?