Windows下安装使用OpenLDAP完整版.docx

上传人:b****6 文档编号:4487902 上传时间:2022-12-01 格式:DOCX 页数:8 大小:104.84KB
下载 相关 举报
Windows下安装使用OpenLDAP完整版.docx_第1页
第1页 / 共8页
Windows下安装使用OpenLDAP完整版.docx_第2页
第2页 / 共8页
Windows下安装使用OpenLDAP完整版.docx_第3页
第3页 / 共8页
Windows下安装使用OpenLDAP完整版.docx_第4页
第4页 / 共8页
Windows下安装使用OpenLDAP完整版.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

Windows下安装使用OpenLDAP完整版.docx

《Windows下安装使用OpenLDAP完整版.docx》由会员分享,可在线阅读,更多相关《Windows下安装使用OpenLDAP完整版.docx(8页珍藏版)》请在冰豆网上搜索。

Windows下安装使用OpenLDAP完整版.docx

Windows下安装使用OpenLDAP完整版

LDAP:

(轻量级目录访问协议,LightweightDirectoryAccessProtocol)它是基于X.500标准的,但是简单多了并且可以根据需要定制。

与X.500不同,LDAP支持TCP/IP,这对访问Internet是必须的。

目录是一个为查询、浏览和搜索而优化的专业分布式数据库,它成树状结构组织数据,就好象Linux/Unix系统中的文件目录一样。

目录数据库和关系数据库不同,它有优异的读性能,但写性能差,并且没有事务处理、回滚等复杂功能,数据修改使用简单的锁定机制实现All-or-Nothing,不适于存储修改频繁的数据。

所以目录天生是用来查询的,就好象它的名字一样。

现在国际上的目录服务标准有两个,一个是较早的X.500标准,一个是较新的LDAP标准。

LDAP诞生的目标是快速响应和大容量查询并且提供多目录服务器的信息复制功能,它为读密集型的操作进行专门的优化。

因此,当从LDAP服务器中读取数据的时候会比从专门为OLTP优化的关系型数据库中读取数据快一个数量级。

LDAP常用术语解释:

DN:

distinguishedname。

在LDAP目录中的所有记录项都有一个唯一的DN

CN,OU,DC都是LDAP连接服务器的端字符串中的区别名称;

LDAP连接服务器的连接字串格式为:

ldap:

//servername/DN  

其中DN有三个属性,分别是CN,OU,DC  

LDAP是一种通讯协议,如同HTTP是一种协议一样的!

在LDAP目录中。

uid    (UserID)

CN    (Common   Name)           

DC    (Domain   Component)           

OU    (Organizational   Unit)   

SN   (surname)   

AnLDAP目录类似于文件系统目录.    下列目录:

      

DC=redmond,DC=wa,DC=microsoft,DC=com      

如果我们类比文件系统的话,可被看作如下文件路径:

   

Com/Microsoft/Wa/Redmond  

例如:

CN=test,OU=developer,DC=domainname,DC=com

在上面的代码中cn=test代表一个用户名,ou=developer代表一个activedirectory中的组织单位。

这句话的含义是test这个对象处在域的developer组织单元中。

OpenLDAP安装及试用

现在市场上有关LDAP的产品已有很多,各大软件公司都在他们的产品中集成了LDAP服务,如Microsoft的ActiveDirectory、iPlanet的iPlanetDirectoryServer(现已更名为:

SunJavaSystemDirectoryServer)、Lotus的DominoDirectory、IBM的WebSphere中也集成了LDAP服务。

OpenLDAP则是LDAP的开源实现,它比商业产品一点也不差,而且源码开放。

1.下载安装openldapforwindows,版本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

接下来的例子只需要用到以上三个schema,当然,如果你觉得需要的话,你可以把其他的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

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}-smysecret (“mysecret“是自定义的密码,可以随便设置)算出加密的密码{MD5}BsIZ5byDePOoo/g7S35GSQ==替换配置中的secret。

3.启动OpenLDAP

   CMD进入到D:

/openldap下,

1)、启动OpenLDAP-slapd服务

   slapdinstallOpenLDAP-slapd“OpenLDAPDirectoryService”auto

netstartOpenLDAP-slapd

NOTE:

the“slapdinstall”isonlyneededifyoudidn’tchoosethe“createNTservice”optionduringinstallation.

2)、启动OpenLDAP服务器

slapd-d1

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

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

4.在安装目录下建立条目,编辑导入ldif文件

1)新建一个ldif(LDAPDataInterchangedFormat)文件(纯文本格式),例如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@

userPassword:

intec

labeledURI:

sn:

sa-sn

cn:

sa-cn

2)打开一个新的cmd执行命令:

ldapadd-x-D“cn=manager,o=tt,c=cn”-wmysecret-ftest.ldif

参数含义可以运行ldapadd/?

命令来查看,这样我们已经成功导入组织tt和一个用户-系统管理员(uid=sa)

导入成功,会如上图所显示。

如果出现“ldap_bind:

Invalidcredentials(49)”这个错误,请检查配置文件是否配置正确,执行的命令(ldapadd-x-D“cn=manager,o=tt,c=cn”-wmysecret-ftest.ldif)是否正确,是不是-w后面所跟的密码跟我们步骤2-2)所设置的不同等等,仔细检查一下即可。

  另外如果想删除我们导入的数据,可以先ctrl+c终止OpenLDAP,然后运行netstopOpenLDAP-slapd停止服务,删除data子文件夹下的内容即可。

5.LdapBrowser浏览

可点击链接下载。

解压并双击lbe.jar批处理文件,弹出LDAPBrowser窗口中默认会有一个OpenLdap_Localhost的链接,选edit,

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

6.Java连接openldap

?

[Copytoclipboard]ViewCodeJAVA

 packageappDate;

importjava.io.ObjectInputStream.GetField;

importjava.sql.Connection;

importjava.sql.ResultSet;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.Hashtable;

importjava.util.List;

importjava.util.Map;

importjavax.naming.Context;

importjavax.naming.NamingEnumeration;

importjavax.naming.NamingException;

importjavax.naming.directory.Attribute;

importjavax.naming.directory.Attributes;

importjavax.naming.directory.DirContext;

importjavax.naming.directory.InitialDirContext;

importjavax.naming.directory.SearchControls;

importjavax.naming.directory.SearchResult;

importjavax.naming.ldap.InitialLdapContext;

importjavax.naming.ldap.LdapContext;

importjavax.xml.crypto.dsig.spec.XPathType.Filter;

importpojo.Tuser;

publicclassTest{

 privateDataBaseUtildb=newDataBaseUtil();

 /**

 *@paramargs

 *@throwsException

 */

 publicstaticvoidmain(String[]args)throwsException{

  //test2();

  newTest().test1();

  //newTest().getAllUser();

 }

 publicvoidtest1(){

  Listlist=newArrayList();

  Hashtableenv=newHashtable();

  env.put(Context.INITIAL_CONTEXT_FACTORY,

    "com.sun.jndi.ldap.LdapCtxFactory");

  env.put(Context.PROVIDER_URL,"ldap:

//localhost/");

  env.put(Context.SECURITY_AUTHENTICATION,"simple");

  env.put(Context.SECURITY_PRINCIPAL,"cn=Manager,o=tt,c=cn");

  env.put(Context.SECURITY_CREDENTIALS,"mysecret");

  LdapContextctx=null;

  try{

   ctx=newInitialLdapContext(env,null);

   //System.out.println("认证成功");

  }catch(javax.naming.AuthenticationExceptione){

   e.printStackTrace();

  }catch(Exceptione){

   e.printStackTrace();

  }

  if(ctx!

=null){

   try{

    SearchControlsconstraints=newSearchControls();

    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

    StringsearchFilter="(uid=*)";

    StringsearchBase="o=tt,c=cn";

    inttotalResults=0;

    StringreturnedAtts[]={"sn","cn"};

    constraints.setReturningAttributes(returnedAtts);

    NamingEnumerationanswer=ctx.search(searchBase,searchFilter,

      constraints);

    while(answer.hasMoreElements()){

     SearchResultsr=(SearchResult)answer.next();

     Attributesattrs=sr.getAttributes();

     ListlistStr=newArrayList();

     if(attrs!

=null){

      try{

       for(NamingEnumerationae=attrs.getAll();ae

         .hasMore();){

        Attributeattr=(Attribute)ae.next();

        //System.out.println("AttributeID:

"+

        //attr.getID());

        //System.out.println(ae.next());

        for(NamingEnumeratione=attr.getAll();e

          .hasMore();totalResults++){

         listStr.add(e.next().toString());

         //System.out.println(e.next());

        }

       }

       //查询LDAP所有的信息

       Tusert=newTuser();

       t.setUserid(listStr.get(0));

       t.setName(listStr.get

(1));

       list.add(t);

      }catch(NamingExceptione){

       e.printStackTrace();

       System.err.println("Problemlistingmembership:

"

         +e);

      }

     }

    }

    ctx.close();

   }catch(NamingExceptione){

    e.printStackTrace();

   }

  }

  ListuserList=newTest().getAllUser();

  for(intj=0;j

   isExists(userList,list.get(j),list.get(j).getUserid());

  }

 }

 //如果存在该用户则不用同步数据,否则往本地数据库insert数据

 publicvoidisExists(ListuserList,Tusert,Stringuid){

  booleanfolg=false;

  for(inti=0;i

   //System.out.println(userList.get(i).getUserid());

   //System.out.println(uid);

   if(userList.get(i).getUserid().equals(uid)){

    folg=true;

    continue;

   }

  }

  if(!

folg){

   //插入本地数据库

   Stringsql="insertintotusertestvalues(?

?

)";

   db.update(sql,t.getUserid(),t.getName());

  }

 }

 //查询现有库的数据

 publicListgetAllUser(){

  Stringsql="selectuserid,namefromtusertest";

  Listlist=newArrayList();

  ResultSetset=null;

  try{

   set=db.getResultSet(sql);

   if(set!

=null){

    while(set.next()){

     Tusert=newTuser(set.getString("userid"),

       set.getString("name"));

     list.add(t);

    }

   }

  }catch(Exceptione){

   e.printStackTrace();

  }finally{

   db.release(set);

  }

  returnlist;

 }

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 环境科学食品科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1