1 OpenLDAP
windows下openldap的安装与测试
1.openldap的下载:
可以通过 openldap for windows 在google上搜索到。
目前最新版本是:2.2.19
下载地址:
http://download.bergmans.us/openldap/openldap-2.2.19/openldap-2.2.19-db-4.3.21-openssl-0.9.7e-win32.exe
相关联接:
http://lucas.bergmans.us/hacks/openldap/
2.运行openldap-2.2.19-db-4.3.21-openssl-0.9.7e-win32.exe安装,一路next就可以了安装ldap for windows到D:/openldap/。
3.假设我们使用的域名是 lizongbo.com 对应的主机IP是192.168.9.126
4.修改C:/WINNT/system32/drivers/etc/下的host文件
添加下面一行
192.168.9.226 lizongbo.com
或者127.0.0.1 zjg.com //预先把自己的机器的名字设置成zjg.com,在我的电脑右击,计算机名称更改里的其他,输入zjg.com
5.配置openldap,修改D:/openldap/slapd.conf里的内容
把下面两行(57,58行)
suffix "dc=my-domain,dc=com"
rootdn "cn=Manager,dc=my-domain,dc=com"
改成
suffix "dc=zjg,dc=com"
rootdn "cn=Manager,dc=zjg,dc=com"
运行
D:/openldap>slappasswd -h {MD5}
New password: Re-enter new password: {MD5}S6CYCoq9tq5LPyFg79WaMQ==
(我输入的密码是zjg)
然后继续修改slapd.conf
把下面这行(62行)
rootpw secret
改成
rootpw {MD5}S6CYCoq9tq5LPyFg79WaMQ==
接下来运行D:/openldap/slapd.exe来启动LDAP服务。
或者运行slapd -d 1
可以看到日志信息。
6.java测试:
package com.lizongbo.ldapdemo;
import java.util.*;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class LDAPTest {
public LDAPTest() {
}
public static void main(String[] args) {
LDAPTest LDAPTest1 = new LDAPTest();
String root = "dc=zjg,dc=com"; //root
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.9.226/" + root);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=lizongbo,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "zjg");
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
}
}
System.exit(0);
}
}
2 Sun ONE Directory Server 5.2 LDAP
Sun ONE Directory Distribution Version 5.2
先说一下环境WinXP + Sun ONE Directory Server 5.2 + JDK1.5
Directory Server, 和JDK可以到Sun的网站上去下载。
Directory Server的安装:
首先用鼠标右键单击我的电脑->属性->网络标识->属性
这里可以看到计算机名,我的计算机名为xiaobai。
点击其他,将此计算机的主DNS后缀栏为输入com然后点击确定。
这是可以看到完整的计算机名称:xiaobai.com。接下来重新启动计算机。
启动完成后就可以安装Directory Server了。
Fully Qualified Computer Name: xiaobai.com /zhoujianguo.com
Server Identifier: xiaobai/zhoujianguo
Server Port: 26773/30478
Suffix: dc=com
admin id: admin
admin pwd: admin
Administration Domain: com
Directory Manager Settings:
Directory Manager DN: cn=Directory Manager
pwd: zhoujianguo
Administration Port: 26774/36401
WildFire 配置SUN ONE LDAP 5.2
Server类型选其它或未知
主机选xiaobai.com
端口选26773
基础DN:dc=example,dc=com
管理员DN:cn=Directory Manager
密码:zhoujianguo
Java:测试
package com.ldap.sunoneldap;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class sunOneLdap {
public static void main(String[] args)
{
DirContext ctx = null;
String account="Directory Manager";//操作LDAP的帐户。默认就是admin。
String password="zhoujianguo";//帐户admin的密码。
String root="dc=example,dc=com"; //所操作的域。也就是LDAP的根节点的DC
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");//必须这样写,无论用什么LDAP服务器。
env.put(Context.PROVIDER_URL, "ldap://zjg.com:30478/" + root);//LDAP服务器的地址:端口。
env.put(Context.SECURITY_AUTHENTICATION, "simple");//授权界别,可以有三种授权级别,但是如果设为另外两种都无法登录,我也不知道为啥,但是只能设成这个值"none"。
env.put(Context.SECURITY_PRINCIPAL, "cn=" + account);//载入登陆帐户和登录密码
env.put(Context.SECURITY_CREDENTIALS, password);
try{
ctx = new InitialDirContext(env);//初始化上下文
System.out.println("认证成功");//这里可以改成异常抛出。
}catch(javax.naming.AuthenticationException e){
System.out.println("认证失败");
}catch(Exception e){
System.out.println("认证出错:"+e);
}
}
}