LDAP:(轻量级目录访问协议,Lightweight Directory Access Protocol) 它是基于 X.500标准的,但是简单多了并且可以根据需要定制。与X.500不同,LDAP支持TCP/IP,这对访问Internet是必须的。
目录是一个为查询、浏览和搜索而优化的专业分布式数据库,它成树状结构组织数据,就好象Linux/Unix系统中的文件目录一样。目录数据库和关系数据 库不同,它有优异的读性能,但写性能差,并且没有事务处理、回滚等复杂功能,数据修改使用简单的锁定机制实现All-or-Nothing,不适于存储修 改频繁的数据。所以目录天生是用来查询的,就好象它的名字一样。现在国际上的目录服务标准有两个,一个是较早的X.500标准,一个是较新的LDAP标 准。
LDAP诞生的目标是快速响应和大容量查询并且提供多目录服务器的信息复制功能,它为读密集型的操作进行专门的 优化。因此,当从LDAP服务器中读取数据的时候会比从专门为OLTP优化的关系型数据库中读取数据快一个数量级。
LDAP常用术语解释:
DN:distinguished name。在LDAP目录中的所有记录项都有一个唯一的DN
CN,OU,DC都是LDAP连接服务器的端字 符串中的区别名称;
LDAP连接服务器的连接字串格式为:ldap://servername/DN
其中DN有三个属性,分别是CN,OU,DC
LDAP是一种通讯协议,如同HTTP是一种协议一样的!在LDAP目录中。
uid (User ID)
CN (Common Name)
DC (Domain Component)
OU (Organizational Unit)
SN (surname)
An LDAP 目录类似于文件系统目录. 下列目录:
DC=redmond,DC=wa,DC=microsoft,DC=com
如果我们类比文件系统的话,可被看作如下文件路径:
Com/Microsoft/Wa/Redmond
例如:CN=test,OU=developer,DC=domainname,DC=com
在上面的代码中cn=test代表一个用户名,ou=developer代表一个active directory中的组织单位。
这句话的含义是test这个对象处在domainname.com域的developer组织单元中。
OpenLDAP安 装及试用
现在市场上有关 LDAP的产品已有很多,各大软件公司都在他们的产品中集成了LDAP服务,如Microsoft的 ActiveDirectory、iPlanet的iPlanet Directory Server(现已更名为:Sun Java System Directory Server)、Lotus的Domino Directory、IBM的WebSphere中也集成了LDAP服务。OpenLDAP则是LDAP的开源实现,它比商业产品一点也不差,而且源码开放。
1. 下载安装 openldap for windows,版本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} -s mysecret (“mysecret“是自定义的密码,可以随便设置)算出加密的密码 {MD5}BsIZ5byDePOoo/g7S35GSQ== 替换配置中的 secret。
3. 启动 OpenLDAP
CMD 进入到 D:/openldap 下,
1)、启动OpenLDAP-slapd服务
slapd install OpenLDAP-slapd “OpenLDAP Directory Service” auto
net start OpenLDAP-slapd
NOTE: the “slapd install” is only needed if you didn’t choose the “create NT service” option during installation.
2)、启 动OpenLDAP服务器
slapd -d 1
启动成功可以看到控制台下打印一片信息,如下图
openldap 默认是用的 Berkeley DB 数据库存储目录数据的。
4. 建立条目,编辑导入 ldif 文件
1) 新建一个 ldif(LDAP Data Interchanged Format) 文件(纯文本格式),例如 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@nfschina.com
userPassword: intec
labeledURI: http://www.nfschina.com
sn: sa-sn
cn: sa-cn
2) 执行命令:ldapadd -x -D “cn=manager,o=tt,c=cn” -w mysecret -f test.ldif
参数含义可以运行 ldapadd /? 命令来查看,这样我们已经成功导入组织tt和一个用户-系统管理员(uid=sa)
导入成功,会如上图所显示。如果出现“ldap_bind: Invalid credentials (49)”这个错误,请检查配置文件是否配置正确,执行的命令(ldapadd -x -D “cn=manager,o=tt,c=cn” -w mysecret -f test.ldif)是否正确,是不是-w后面所跟 的密码跟我们步骤2-2)所 设置的不同等等,仔细检查一下即可。 另外如果想删除我们导入的数据,可以先ctrl+c终止OpenLDAP,然后运行net stop OpenLDAP-slapd停止服务,删除data子文件夹下的内容即可。
5. LdapBrowser 浏览
可点击链接 http://www.blogjava.net/Files/Unmi/LdapBrowser282.rar 下载。解压并双击lbe.bat批处理文件,弹出LDAP Browser窗口中默认会有一个OpenLdap_Localhost的链接,选edit,
点击 Fetch DNs 按钮会匹配出我们前面设置的DN,password那填写我们步骤2-2)所设置的密码mysecret,SAVE—>>>Connect—>>>我们会看到相关信息~~
6. Java 连接 openldap
package appDate;
import java.io.ObjectInputStream.GetField;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.xml.crypto.dsig.spec.XPathType.Filter;
import pojo.Tuser;
public class Test {
private DataBaseUtil db = new DataBaseUtil();
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// test2();
new Test().test1();
// new Test().getAllUser();
}
public void test1() {
List<Tuser> list = new ArrayList<Tuser>();
Hashtable env = new Hashtable();
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");
LdapContext ctx = null;
try {
ctx = new InitialLdapContext(env, null);
//System.out.println("认证成功");
} catch (javax.naming.AuthenticationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (ctx != null) {
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "(uid=*)";
String searchBase = "o=tt,c=cn";
int totalResults = 0;
String returnedAtts[] = { "sn", "cn" };
constraints.setReturningAttributes(returnedAtts);
NamingEnumeration answer = ctx.search(searchBase, searchFilter,
constraints);
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
List<String> listStr = new ArrayList<String>();
if (attrs != null) {
try {
for (NamingEnumeration ae = attrs.getAll(); ae
.hasMore();) {
Attribute attr = (Attribute) ae.next();
// System.out.println("AttributeID: " +
// attr.getID());
// System.out.println(ae.next());
for (NamingEnumeration e = attr.getAll(); e
.hasMore(); totalResults++) {
listStr.add(e.next().toString());
// System.out.println(e.next());
}
}
// 查询LDAP所有的信息
Tuser t = new Tuser();
t.setUserid(listStr.get(0));
t.setName(listStr.get(1));
list.add(t);
} catch (NamingException e) {
e.printStackTrace();
System.err.println("Problem listing membership: "
+ e);
}
}
}
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
List<Tuser> userList = new Test().getAllUser();
for (int j = 0; j < list.size(); j++) {
isExists(userList, list.get(j), list.get(j).getUserid());
}
}
// 如果存在该用户则不用同步数据,否则往本地数据库insert数据
public void isExists(List<Tuser> userList, Tuser t, String uid) {
boolean folg = false;
for (int i = 0; i < userList.size(); i++) {
// System.out.println(userList.get(i).getUserid());
// System.out.println(uid);
if (userList.get(i).getUserid().equals(uid)) {
folg = true;
continue;
}
}
if (!folg) {
// 插入本地数据库
String sql = "insert into tusertest values(?,?)";
db.update(sql, t.getUserid(), t.getName());
}
}
// 查询现有库的数据
public List<Tuser> getAllUser() {
String sql = "select userid,name from tusertest";
List<Tuser> list = new ArrayList<Tuser>();
ResultSet set = null;
try {
set = db.getResultSet(sql);
if (set != null) {
while (set.next()) {
Tuser t = new Tuser(set.getString("userid"),
set.getString("name"));
list.add(t);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
db.release(set);
}
return list;
}
}
分享到:
相关推荐
Windows下安装使用openldap Windows下安装使用openldap Windows下安装使用openldap
该文档详细描述了在windows环境下OPENLDAP的安装和使用方法。
windows下OpenLdap安装及使用
主要描述了在Windows环境下安装OpenLDAP的步骤,以及简单的使用方法。同时,给出了一个与Tomcate配置的例子,具体操作了不同用户下的访问权限问题。
NULL 博文链接:https://cjb-666888.iteye.com/blog/1967770
windows下openldap的安装与java操作测试 windows下openldap的安装与java操作测试
openldap在windows安装配置.pdf
openldap-for-windows2.4.23 windows版本
openLDAP for windows 服务器端和客户端安装文件,包含openLDAP windows 安装配置方法链接
附近为整理好的openldap工程,可以直接在VC9上编译openldap
OpenLDAP for Windows2.4.34
windows版的openldap,适合于本地开发测试用
windows x64位 OpenLDAP服务
openldap for windows 32位及64位安装包,要在windows环境进行安装
win32 openLdap 2.2.29 安装文件,亲测可用,不用注册码。
openldap for windows.msi file format
openldap安装包,亲测可用,傻瓜式下一步安装
openldap2.2.29 用于windows环境下搭建ldap服务
openldap-for-windows安装包 LdapBrowser282 辅助工具 RFC2256LDAPv3使用X500用户schema总结 文档
NULL 博文链接:https://ln-ydc.iteye.com/blog/1328829