Windows下安装使用OpenLDAP

LDAP:轻量级目录访问协议,Lightweight Directory Access Protocol)它是基于 X.500标准的,但是简单多了并且可以根据需要定制。与X.500不同,LDAP支持TCP/IP,这对访问Internet是必须的。

目 录是一个为查询、浏览和搜索而优化的专业分布式数据库,它成树状结构组织数据,就好象Linux/Unix系统中的文件目录一样。目录数据库和关系数据库 不同,它有优异的读性能,但写性能差,并且没有事务处理、回滚等复杂功能,数据修改使用简单的锁定机制实现All-or-Nothing,不适于存储修改 频繁的数据。所以目录天生是用来查询的,就好象它的名字一样。现在国际上的目录服务标准有两个,一个是较早的X.500标准,一个是较新的LDAP标准。
LDAP诞生的目标是快速响应和大容量查询并且提供多目录服务器的信息复制功能,它为读密集型的操作进行专门的优化。因此,当从LDAP服务器中读取数据的时候会比从专门为OLTP优化的关系型数据库中读取数据快一个数量级。

LDAP常用术语解释:
DN:
distinguished name。在LDAP目录中的所有记录项都有一个唯一的DN
CN,OU,DC都是LDAP连接服务器的端字符串中的区别名称;
LDAP连接服务器的连接字串格式为:ldap://servername/DN  
其中DN有三个属性,分别是CN,OU,DC  
LDAP是一种通讯协议,如同HTTP是一种协议一样的!在LDAP目录中。
uid    (User ID)
CN     (Common    Name)           
DC     (Domain    Component           
OU     (Organizational    Unit
  
SN    (surname
  
An LDAP 目录类似于文件系统目录.     下列目录:      
DC=redmond,DC=wa,DC=microsoft,DC=com      
如果我们类比文件系统的话,可被看作如下文件路径:   
Com\Microsoft\Wa\Redmond  

例如:CN=test,OU=developer,DC=domainname,DC=com
在上面的代码中cn=test代表一个用户名,ou=developer代表一个active directory中的组织单位。
这句话的含义是test这个对象处在domainname.com域的developer组织单元中。

OpenLDAP安装及试用
现在市场上有关LDAP的产品已有很多,各大软件公司都在他们的产品中集成了LDAP服务,如Microsoft的 ActiveDirectory、iPlanet的iPlanet Directory Server(现已更名为:Sun Java System Directory Server)、Lotus的Domino Directory、IBM的WebSphere中也集成了LDAP服务。OpenLDAP则是LDAP的开源实现,它比商业产品一点也不差,而且源码开放。
1. 下载安装 openldap for windows版本2.2.29
下载地址:http://download.bergmans.us/openldap/openldap-2.2.29/openldap-2.2.29-db-4.3.29-openssl-0.9.8a-win32_Setup.exe
安装很简单,一路 next 即可,假设我们安装在 D:\openldap

2. 配置 openldap, 编辑 slapd.conf 文件
1)、打开 D:\openldap\slapd.conf,找到ucdata-path    ./ucdata
include  ./schema/core.schema,在它后面添加
include  ./schema/cosine.schema
include  ./schema/inetorgperson.schema

2)、下面我们做一个示例:在中国(cn)的tt公司添加一个系统管理员sa.
需要在 slapd.conf 配置文件中,找到
suffix  "dc=my-domain,dc=com"
rootdn  "cn=Manager,dc=my-domain,dc=com"
把这两行改为
suffix "o=tt,c=cn"
rootdn "cn=Manager,o=tt,c=cn"

suffix 就是看自己如何定义了,后面步骤的 ldif 文件就必须与它定义了。还要注意到这个配置文件中有一个 rootpw  secret,这个 secret 是 cn=Manager 的密码,以后会用到,不过这里是明文密码,你可以用命令: slappasswd -h {MD5} -s mysecret (mysecret"是自定义的密码,可以随便设置)算出加密的密码 {MD5}BsIZ5byDePOoo/g7S35GSQ== 替换配置中的 secret。


3. 启动 OpenLDAP
    CMD 进入到 D:\openldap 下,
1)、启动OpenLDAP-slapd服务
    slapd install OpenLDAP-slapd "OpenLDAP Directory Service" auto
net start OpenLDAP-slapd
NOTE: the "slapd install" is only needed if you didn't choose the "create NT service" option during installation.
2)、启动OpenLDAP服务器
slapd -d 1

启动成功可以看到控制台下打印一片信息,如下图

openldap 默认是用的 Berkeley DB 数据库存储目录数据的。


4. 建立条目,编辑导入 ldif 文件
1) 新建一个 ldif(LDAP Data Interchanged Format) 文件(纯文本格式),例如 test.ldif,文件内容如下:

dn: o=tt,c=cn
objectClass: dcObject
objectClass: organization
o: tt
dc: com

dn: uid=sa, o=tt,c=cn
uid: sa
objectClass: inetOrgPerson
mail: xhy@nfschina.com
userPassword: intec
labeledURI: http://www.nfschina.com
sn: sa-sn
cn: sa-cn

2) 执行命令:ldapadd -x -D "cn=manager,o=tt,c=cn" -w mysecret -f test.ldif
参数含义可以运行 ldapadd /? 命令来查看,这样我们已经成功导入组织tt和一个用户-系统管理员(uid=sa)



导入成功,会如上图所显示。如果出现“ldap_bind: Invalid credentials (49)”这个错误,请检查配置文件是否配置正确,执行的命令(ldapadd -x -D "cn=manager,o=tt,c=cn" -w mysecret -f test.ldif)是否正确,是不是-w后面所跟的密码跟我们步骤2-2)所设置的不同等等,仔细检查一下即可。   另外如果想删除我们导入的数据,可以先ctrl+c终止OpenLDAP,然后运行net stop OpenLDAP-slapd停止服务,删除data子文件夹下的内容即可。


5. LdapBrowser 浏览
可点击链接 http://www.blogjava.net/Files/Unmi/LdapBrowser282.rar 下载。解压并双击lbe.bat批处理文件,弹出LDAP Browser窗口中默认会有一个OpenLdap_Localhost的链接,选edit,



点击 Fetch DNs 按钮会匹配出我们前面设置的DN,password那填写我们步骤2-2)所设置的密码mysecret,SAVE--->>>Connect--->>>我们会看到相关信息~~


6. Java 连接 openldap
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public   class  test {  
  public   static   void  main(String[] args) {  
    
String root =  "o=tt,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=tt,c=cn" );  
    env.put(Context.SECURITY_CREDENTIALS,  "mysecret" );  
    DirContext ctx =  null ;  
    try  {  
      ctx =  new  InitialDirContext(env);  
      System.out.println( "认证成功" );  
    }  
    catch  (javax.naming.AuthenticationException e) {  
      e.printStackTrace();  
      System.out.println( "认证失败" );  
    }  
    catch  (Exception e) {  
      System.out.println( "认证出错:" );  
      e.printStackTrace();  
    }  

    if  (ctx !=  null ) {  
      try  {  
        ctx.close();  
      }  
      catch  (NamingException e) {  
        //ignore  
      }   
    }  
  }  

}



资源:
http://www.openldap.org/  http://baike.baidu.com/view/159263.htm  http://blog.chinaunix.net/u/13366/showart_130825.html   - LDAP详解
http://blog.chinaunix.net/u/21863/showart_141286.html  http://fanqiang.chinaunix.net/app/ldap/2005-03-23/2979.shtml  扩展文献:
http://docs.sun.com/source/816-4121/contents.htm  http://technet.microsoft.com/zh-cn/library/cc782657%28WS.10%29.aspx  Active Directory

  

转自:http://hi.baidu.com/chinagenius/blog/item/28c92d037c7087e009fa9342.html

posted @ 2021-02-23 10:15  lclc  阅读(2382)  评论(0编辑  收藏  举报