内容概览:
1.JNDI对目录服务的一些基本操作
2.解决一个小问题(错误描述):
javax.naming.directory.SchemaViolationException: [LDAP: error code 65 - object class 'javaContainer' requires attribute 'cn']; remaining name 'o=jndiTest'
--------------------------------------------------------------------------------------------------------------------------------
准备:
1.接上一篇 (windows下搭建并配置OpenLDAP服务器 )的环境
2.LDAP查看工具 LdapBrowser 下载见附件
--------------------------------------------------------------------------------------------------------------------------------
提前解决一个错误(错误描述):
javax.naming.directory.SchemaViolationException: [LDAP: error code 65 - object class 'javaContainer' requires attribute 'cn']; remaining name 'o=jndiTest'
在上一篇 中,在安装目录下的slapd.conf文件中有如下配置:
ucdata-path ./ucdata 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
可以看到有
include ./schema/java.schema
这一项,定位到该文件,安装目录/schema/java.schema,用editplus或其它软件打开
找到
objectclass ( 1.3.6.1.4.1.42.2.27.4.2.1 NAME 'javaContainer' DESC 'Container for a Java object' SUP top STRUCTURAL MUST cn)
修改为
objectclass ( 1.3.6.1.4.1.42.2.27.4.2.1 NAME 'javaContainer' DESC 'Container for a Java object' SUP top STRUCTURAL MAY(o$cn))
这样在做以下操作的时候就不会报上文提到的错误了。
--------------------------------------------------------------------------------------------------------------------------------
JNDI对LDAP的基本操作:
1.启动OpenLDAP,命令行,定位到OpenLDAP的安装目录下
slapd -d 1
2.启动 LdapBrowser,解压LdapBrowser附件,进入文件夹,双击lbe.jar
如果是被压缩软件打开的话,就只有用命令行启动了,命令行,定位到该文件夹下
java -jar lbe.jar
启动后界面如下:
选择Quick Connect:
填入ldap相关信息:
注意:在选择Base DN的时候,先点击一下Fetch DNs按钮,密码就是自己设置的密码:secret
点击Connect后:
3.新建java测试类,测试方法如下:
@Test public void testMakeRoot() { String ldapServerName = "localhost"; String rootdn = "cn=Manager,o=jndiTest"; String rootpass = "secret"; String rootContext = "o=jndiTest"; // set up environment to access the server Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://" + ldapServerName + ":389/"); env.put(Context.SECURITY_PRINCIPAL, rootdn); env.put(Context.SECURITY_CREDENTIALS, rootpass); try { // obtain initial directory context using the environment DirContext ctx = new InitialDirContext(env); // now, create the root context, which is just a subcontext // of this initial directory context. ctx.createSubcontext(rootContext); } catch (NameAlreadyBoundException nabe) { System.err.println(rootContext + " has already been bound!"); } catch (Exception e) { System.err.println(e); } }
运行后,查看ldap broswer,如下图:
再添加一个测试方法:
@Test public void testLdap() { String ldapServerName = "localhost"; String rootdn = "cn=Manager,o=jndiTest"; String rootpass = "secret"; String rootContext = "o=jndiTest"; // set up environment to access the server Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://" + ldapServerName + ":389/" + rootContext); env.put(Context.SECURITY_PRINCIPAL, rootdn); env.put(Context.SECURITY_CREDENTIALS, rootpass); try { // obtain initial directory context using the environment DirContext ctx = new InitialDirContext(env); // create some random number to add to the directory Integer i = new Integer( 28420 ); System.out.println( "Adding " + i + " to directory..." ); ctx.bind( "cn=myRandomInt", i ); i = new Integer( 98765 ); System.out.println( "i is now: " + i ); i = (Integer) ctx.lookup( "cn=myRandomInt" ); System.out.println( "Retrieved i from directory with value: " + i ); } catch (NameAlreadyBoundException nabe) { System.err.println(rootContext + " has already been bound!"); } catch (Exception e) { System.err.println(e); } }
运行后,结果如下:
Adding 28420 to directory... i is now: 98765 Retrieved i from directory with value: 28420
查看ldap broswer,如下图:
--------------------------------------------------------------------------------------------------------------------------------
总结:
1.初步认识了ldap
--------------------------------------------------------------------------------------------------------------------------------
参考网站:
http://www.cris.com/~adhawan/tutorial/
http://hi.baidu.com/talenian/blog/item/ebd29d26bd049326d407428b.html
http://mybeautiful.iteye.com/blog/1218806
--------------------------------------------------------------------------------------------------------------------------------
相关推荐
基于Java JNDI 操纵LDAP的基础实现,包括LDAP连接相关生命周期,认证的两种策略demo,条目的操作,schema的基础操作包括(attributeDefinition\objectDefinition\)但未包含相关syntaxDefinition的操纵. 没有资源分的朋友...
Windows_7_下搭建LDAP服务器并使用JNDI Windows_7_下搭建LDAP服务器并使用JNDI Windows_7_下搭建LDAP服务器并使用JNDI
import com.sun.jndi.ldap.ctl.VirtualListViewControl;
JNDI 操作 LDAP的几个例子源码 包括连接、添加、删除、修改、搜寻
NULL 博文链接:https://zzqrj.iteye.com/blog/1279219
封装JNDI的LDAP服务器所要使用的类的详细介绍。 封装JNDI操作LDAP服务器的工具类.doc
maven稀缺jar资源
提供了使用fsContext服务器的unbind bind list lookup rename的相关例子,及ldap 连接的一个例子 jndi,就是命名服务(n:naming)和目录服务(d:directory). 命名服务:把对象映射到方便记忆的名字的机制.可以通过域名系统...
主要介绍了封装JNDI操作LDAP服务器的工具类,使用者只需要会使用List,Map 数据结构,大家参考使用吧
AD ldap java集成
rmi-jndi-ldap-jrmp-jmx-jms rmi,jndi,ldap,jrmp,jmx,jms一些演示测试
集成配置与集成AD以及LDAP
027-JNDI之初探 LDAP.pdf
这个是一个初学ldap和jndi的心得,希望对大家有所帮助.
SpringLDAP和JNDI增删改查的入门级例子 能够通过编译和运行 已经包含了需要的包,用eclipse导入即可
亲测可以使用的,LDAP认证用户名密码,使用JNDI方式认证
JNDI修改OpenLDAP条目密码,内容包括一个MD5与Base64的转码操作,以及使用JNDI以MD5的方式修改LDAP条目的密码.
java连接和验证的ldap学习文档,密码的校验,通过jndi连接ldap服务