搭建LDAP服务器并使用JNDI_百度文库_第1页
搭建LDAP服务器并使用JNDI_百度文库_第2页
搭建LDAP服务器并使用JNDI_百度文库_第3页
搭建LDAP服务器并使用JNDI_百度文库_第4页
搭建LDAP服务器并使用JNDI_百度文库_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Windows 下搭建LDAP服务器并使用JNDI访问一、OpenLDAP安装和配置安装还是比较简单的,一直next就好。这里记得把上面2个都选上,将LDAP注册为系统的一个服务,默认安装位置:C:Program FilesOpenLDAP,进入安装目录,编辑slapd.conf 文件:找到ucdata-path ./ucdatainclude ./schema/core.schema在下面加入:(注意你的系统路径,可能随安装位置不同而稍有差异 )include ./schema/core.schemainclude ./schema/corba.schemainclude ./schema/

2、dyngroup.schemainclude ./schema/java.schemainclude ./schema/misc.schemainclude ./schema/cosine.schemainclude ./schema/nis.schemainclude ./schema/inetorgperson.schemainclude ./schema/openldap.schema这个搞定以后,在同一文件后面的(大概65-66行,修改)suffix “o=anotherbug,c=com”rootdn “cn=manager,o=anotherbug,c=com”还有第70行的位置

3、: rootpw secret,这里要修改为加密后的密码。具体操作:打开命令行,定位到安装目录下,输入:slappasswd -h MD5 s “替换为你想要设置的密码,无引号”将生成的MD5密文:MD5Xr4ilOzQ4PCOq3aQ0qbuaQ=填入原来secret位置。OK至此配置已经搞定,可以测试一下服务了。打开命令行转到安装目录下输入:sldapd -d 1二、建立条目(Entry) ,导入 ldif 后缀名文件ldif:LDAP Data Interchange Format,基于文本。有两种类型的 LDIF 文件:第一种是描述 Directory 条目数据的,第二种是描述更新条目

4、的。我们主要看怎么描述条目的。打开编辑器(如Editplus,UltraEdit等),新建test.ldif内容如下:dn: o=anotherbug,c=comobjectClass: dcObjectobjectClass: organizationo: anotherbugdc: comdn: uid=mousepoato, o=anotherbug,c=comuid: mousepoatoobjectClass: inetOrgPersonmail: vicky.chenuserPassword: admin sn: Licn: test注意ldif文件对格式的要求非常严格,属性要以冒

5、号和空格与值隔开 ,并且其他地方不允许有空格 。否则当你导入ldif文件时,会提示出现“ldap_add: Invalid syntax (21 ”等诸多错误,另外在我机器上测试,ldif对中文支持也还不好 ,比如我将最后的cn: test改为 cn: 鼠标土豆,导入就会报错。写完保存到安装目录下。在命令行输入:ldapadd -c -x -D “cn=manager,o=anotherbug,c=com” -w “刚才替换secret出的密码明文” -f test.ldif运行命令后结果如下:注意我们在ldapadd后面加上了 ”c “ 参数,他会一直运行不会因错误而终止,比如对系统已经存在

6、的entry命令会提示但不会中止。三、LDAP查看工具可能大家看了这么多感觉还是很抽象,我们需要一个GUI看看LDAP到底是个什么东东。这里推荐两个浏览工具1、LdapBrowser这是个Java 开发的 LDAP Browser/Editor 工具,不但跨平台(Windows, Unix-like,而且功能非常完整,速度又快。运行起来的界面时这个样子的。2、Softrra LDAP Administrator 2009这是一个比较强大和专业的客户端,涵盖了大多数企业的LDAP服务类型。一直下一步安装成功后,它的配置也是比较简单的:新建一个profile,命名为Local_LDAP配置连接信息

7、这是完整配置好后的效果四、通过 JNDI api操作LDAP例子参考代码:importimportimportimportimportimportimportpublic class LdapTest public static void main(String args / LDAP连接对象LDAPConnection ld = null;LDAPEntry findEntry = null;int status = -1;try ld = new LDAPConnection(;/ 连接LDAP服务器的IP及端口号final String MY_HOST = "localhost

8、"final int MY_PORT = 389;/ 连接LDAP服务器ld.connect(MY_HOST, MY_PORT;/ 欲查询的条目final String ENTRYDN = "cn=vicky,dc=sino,dc=com"/ 条目的属性final String attrNames = "sn", "telephonenumber", "name", "mail"/ LDAP查询操作结果集合LDAPSearchResults res = ld.search(ENTRYD

9、N,LDAPConnection.SCOPE_BASE,"objectclass=*",attrNames,false;/ 循环遍历集合,获取属性名,只能遍历一次while (res.hasMoreElements(tryfindEntry = res.next(;catch (LDAPException eSystem.out.println("Error: " + e.toString(;continue;/ 条目属性名集合LDAPAttributeSet findAttrs = findEntry.getAttributeSet(;/ 属性名列表E

10、numeration enumAttrs = findAttrs.getAttributes(;/ 循环遍历属性名列表以获取每个属性名while (enumAttrs.hasMoreElements(LDAPAttribute anAttr = (LDAPAttribute enumAttrs.nextElement(;String attrName = anAttr.getName(;if (attrName.equals("cn"System.out.println("Full name:"else if (attrName.equals("

11、;sn"System.out.println("Last name (surname:"/ 断开与LDAP服务器的连接if (ld != null && ld.isConnected(tryld.disconnect(;catch (LDAPException eSystem.out.println("Error: " + e.toString(;System.exit(status; catch(Exception eSystem.out.println("=成功!"五、通过SpringLDAP访问LDAP

12、服务的例子参考代码:1、spring配置文件代码xml version="1.0" encoding="UTF-8"?>DOCTYPE beans PUBLIC "-/SPRING/DTD BEAN/EN" ><beans><bean id="ladpContextSource"class><property name="url" value="ldap:/localhost:389" /> <property name=

13、"base" value="dc=sino,dc=com"> property> <property name="userDn" value="cn=vicky,dc=sino,dc=com" /><property name="password" value="123456" />bean><bean id="ldapTemplate"class><property name="con

14、textSource" ref="ladpContextSource" />bean><bean id="userDao" class><property name="ldapTemplate"><ref bean="ldapTemplate" />property>bean>beans>2、访问LDAP服务的java代码importimportimportimportimport/* 2011-03-04* author vicky*/p

15、ublic class UserDaoImpl private LdapTemplate ldapTemplate;public void setLdapTemplate(LdapTemplate ldapTemplate this.ldapTemplate = ldapTemplate;public List getAllContactNames( return ldapTemplate.search("", "(objectclass=person",new AttributesMapper(public Object mapFromAttributes(Attributes attrsthrows NamingExceptionreturn attrs.get("cn".get(;3、测试代码importimportimportimportpublic class Test public static void main(String args ApplicationCon

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论