内容概览:
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
--------------------------------------------------------------------------------------------------------------------------------