Python爬虫,及大数据机器学习收藏文集程序猿

LDAP入门

2017-12-07  本文已影响31599人  消失er

LDAP入门

那LDAP这种数据库有什么特殊的呢?

假设你要树上的一个苹果(一条记录),你怎么告诉园丁它的位置呢?当然首先要说明是哪一棵树(dc,相当于MYSQL的DB),然后是从树根到那个苹果所经过的所有“分叉”(ou),最后就是这个苹果的名字(uid,相当于MySQL表主键id)。好了!这时我们可以清晰的指明这个苹果的位置了,就是那棵“歪脖树”的东边那个分叉上的靠西边那个分叉的再靠北边的分叉上的半红半绿的……,晕了!你直接爬上去吧!

就这样就可以描述清楚“树结构”上的一条记录了。
说一下LDAP里如何定义一个记录的位置吧。

树(dc=ljheee)
分叉(ou=bei,ou=xi,ou= dong)
苹果(cn=redApple)

好了,redApple的位置出来了:
dn:cn=honglv,ou=bei,ou=xi,ou=dong,dc=ljheee
其中dn标识一条记录,描述了一条数据的详细路径。
咦!有人疑问,为什么ou会有多个值?你想想,从树根到达苹果的位置,可能要经过好几个树杈,所有ou可能有多个值。关于dn后面一长串,分别是cn,ou,dc;中间用逗号隔开。

总结一下LDAP树形数据库如下:
dn :一条记录的详细位置
dc :一条记录所属区域    (哪一颗树)
ou :一条记录所属组织    (哪一个分支)
cn/uid:一条记录的名字/ID   (哪一个苹果名字)
LDAP目录树的最顶部就是根,也就是所谓的“基准DN"。

LDAP编程操作

可以把ApacheDirectoryStudio看做是连接数据库服务器的界面化的client,相当于Navicat、WorkBench。新建连接,连接数据库服务器的操作类似。

ApacheDirectoryStudio下载地址
http://download.csdn.net/download/ljheee/10145654

JNDI连接LDAP服务器

import org.springframework.beans.factory.annotation.Autowired;
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class LdapJNDI {

    @Autowired
    LdapConfiguration ldapConfiguration;

    public void JNDILookup() {
        String rootFilter = "o=cvte.com,o=isp";
//        String filter = "(&(smart-type=E1)(smart-status=1))";
        String filter = "(&(smart-type=E1)(uid=00012047))";
        String username = "uid=USER_NAME,ou=Authorization,ou=People,o=cc.com,o=isp";//xxx为申请的对接账户
        String password = "PASSW";

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");//设置连接LDAP的实现工厂
        env.put(Context.PROVIDER_URL, "ldap://172.26.39.77:389/" + rootFilter);// 指定LDAP服务器的主机名和端口号
        env.put(Context.SECURITY_AUTHENTICATION, "simple");//给环境提供认证方法,有SIMPLE、SSL/TLS和SASL
        env.put(Context.SECURITY_PRINCIPAL, username);//指定进入的目录识别名DN
        env.put(Context.SECURITY_CREDENTIALS, password); //进入的目录密码
        env.put("filter",filter);
        DirContext ctx = null;

        try {
            // 得到初始目录环境的一个引用
            ctx = new InitialDirContext(env);

            //The search base entry 'uid=00012047,ou=Internal,ou=People,o=cvte.com,o=isp' does not exist]; remaining name 'uid=00012047,ou=Internal'
//            Attributes attrs = ctx.getAttributes("uid=00012047,ou=Internal,ou=People");//获取到一个人员,


            NamingEnumeration bindings = ctx.listBindings("ou=Internal,ou=People");//列举 内部人员

            while (bindings.hasMore()) {
                Binding bd = (Binding)bindings.next();
                System.out.println(bd.getName() + ": " + bd.getObject());
            }


             /*根据结点的DN来查找它的所有属性, 然后再从属性中得到所有的值,注意一个属性可以有多个值*/
//            for (NamingEnumeration ae = attrs.getAll(); ae.hasMore(); ) {
//                //获取一个属性
//                Attribute attr = (Attribute) ae.next();
//                for (NamingEnumeration ve = attr.getAll(); ve.hasMore(); ) {
//                    System.out.println(String.format("Attribute=%s,Value=%s",attr.getID(),ve.next()) );
//                }
//            }

        } catch (javax.naming.AuthenticationException e) {
            System.out.println("认证失败");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("认证出错:");
            e.printStackTrace();
        }finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (NamingException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) {
        LdapJNDI ldapJNDI = new LdapJNDI();
        ldapJNDI.JNDILookup();

    }

}

最后总结一下LDAP:
1、LDAP的结构用树来表示,而不是用表格。正因为这样,就不能用SQL语句了。
2、LDAP可以很快地得到查询结果,不过在写方面,就慢得多。
3、LDAP提供了静态数据的快速查询方式。
4、Client/server模型,Server 用于存储数据,Client提供操作目录信息树的工具。
5、LDAP是一种开放Internet标准,LDAP协议是跨平台的Interent协议。


Spring LDAP的使用
Spring ldap ODMJava反射实战 操作ladp ODM

我的CSDN博客
github地址

上一篇下一篇

猜你喜欢

热点阅读