ITeye博客

用JAVA实现LDAP的访问(二)

superallen · 2007-02-04 10:33 · ldap · Markdown

下面来具体的说一下怎么用JLDAP。首先要去下载一下JLDAP,具体下载的地址可以上网去搜。下载下来以后,lib里面的是开发所要用到的包,doc里面是帮助文档API和示例程序。

    先说说怎么查询,其实查询非常的简单,如果用过JDBC连数据库的话,那么连LDAP相比起来更加的简单。

    首先建立一个LDAPConnection对象。这个对象也可以通过连接池PoolManager来获得。LDAPConnection con = new LDAPConnection();然后运行connect方法和bind方法。连接上LDAP以后,就可以通过search方法来查找数据了。示例程序如下:

java 代码

  1. LDAPConnection lc = new LDAPConnection();
  2. try {
  3. lc.connect("6.1.19.154",389);
  4. lc.bind(LDAPConnection.LDAP_V3,"cn=xxx","xxxxxx");
  5. LDAPSearchResults rs = lc.search("dc=excel,dc=com,dc=cn",LDAPConnection.SCOPE_SUB,"objectClass=*",null,false);
  6. int count = 0;
  7. while(rs.hasMore()){
  8. LDAPEntry entry = rs.next();
  9. System.out.println(entry.getDN());
  10. count++;
  11. }
  12. System.out.println("共有"+count+"条记录。");
  13. } catch (LDAPException e) {
  14. System.err.print("连接异常!   ");
  15. e.printStackTrace();
  16. }

评论(5)

  1. #5 楼 · Hejrcc · 2007-06-14 21:15

    我觉得自己发表的帖子, 要是可以修改就好了。

  2. #4 楼 · Hejrcc · 2007-06-14 21:13

    看见还有一种写法用来获取 DirContext, 下面的写法指定了 INITIAL_CONTEXT_FACTORY属性,我想知道我前面一种写法里面, env.put(Context.INITIAL_CONTEXT_FACTORY, ?); 这个INITIAL_CONTEXT_FACTORY 我没有设置, 不知道默认是什么?

     Hashtable<String, String> env = new Hashtable<String, String>();
     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
     env.put(Context.PROVIDER_URL, "ldap://localhost:389");
     env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=mycompany,dc=com");
     env.put(Context.SECURITY_CREDENTIALS, "secret");
     //env.put(Context.SECURITY_AUTHENTICATION, "simple"); //"none", "simple", "strong"         
     DirContext context = new InitialDirContext(env);
    
  3. #3 楼 · Hejrcc · 2007-06-14 21:10

    看不出用JLDAP有什么优势, 我也刚刚开始学。 我写了个测试例子,请指点:

    public void testLdap() {
        try {
            DirContext context = getContext();
            addEntry(context, "uid=oracle,ou=people,dc=mycompany,dc=com");
            printEntry(context, "uid=oracle,ou=people,dc=mycompany,dc=com");
            context.close();
        } catch (AuthenticationException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
    
    public DirContext getContext() throws NamingException {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=mycompany,dc=com");
        env.put(Context.SECURITY_CREDENTIALS, "secret");
        env.put(Context.SECURITY_AUTHENTICATION, "simple"); //"none", "simple", "strong"
    
        DirContext initial = new InitialDirContext(env);
        DirContext context = (DirContext) initial.lookup("ldap://localhost:389");
        return context;
    }
    
    public void addEntry(DirContext context, String dn) throws NamingException {
        Attributes attrs = new BasicAttributes();
        attrs.put("uid", "oracle");
        attrs.put("sn", "Lee");
        attrs.put("cn", "Amy Lee");
        attrs.put("telephoneNumber", "+1 408 555 0033");
        attrs.put("userPassword", "redqueen".getBytes());
        //the following attribute has two values
        Attribute objclass = new BasicAttribute("objectClass");
        objclass.add("uidObject");
        objclass.add("person");
        attrs.put(objclass);
    
        context.createSubcontext(dn, attrs);
    }
    
  4. #2 楼 · Hejrcc · 2007-06-14 21:05

    呵呵, 就是。。。

  5. #1 楼 · Dustbin · 2007-03-30 16:39

    这样的程序会导致ldap服务器死机地,需要关闭ldap连接

评论功能维护中,当前仅展示历史评论。