---
title: "ladp study"
author: "lujin55"
date: "2012-06-02T18:37:07Z"
tags: []
categories: ["编程语言"]
slug: "ladp-study"
draft: true
lastmod: "2012-04-28T10:55:28Z"
id: 1492535
url: "https://lujin55.iteye.com/blog/1492535"
---

ldap:lightweight directory access protocol轻量级目录访问协议。

  一般公司的账户都存在ldap services中，系统可以直接访问该services中的账户作为本系统的账户，这样避免了大量账户的创建。

  下载openLDAP software,安装。
  在安装目录下，找到sladp.conf文件。可以看见一下配置

```java

suffix		"dc=my-domain,dc=com"
rootdn		"cn=Manager,dc=my-domain,dc=com"
rootpw		secret

```

suffix在我的理解中就相当于domain，以后你家的entry中都必须用该domain。
rootdn相当于用户名，你对该services操作总要用户验证的。
rootpw相当于密码
  
   默认情况下，你不用配置什么，就可以开始对这个services进行操作
添加entry

```java
ldapadd -D "cn=liu,dc=my-domain,dc=com" -W -f init.ldif
```

-D输用户名，-W提示输密码，-f你要添加的实体，实体存放在文件里，文件自己创建并放在安装目录下。注意的是用单引号会出错。
init.ldif里面内容

```java

dn:dc=my-domain,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: controversial

dn:cn=liu,dc=my-domain,dc=com
objectClass: person
cn: liu

```

添加了一个根节点，并在根节点下添加了一个entry。一个dn就代表一个entry。

若第二次添加的时候，文件里就只放entry了。

```java

dn:cn=quan,dc=my-domain,dc=com
objectClass: person
cn: quan

```


添加后，就可以查询你刚才添加的entry

```java
ldapsearch -D "cn=manager,dc=my-domain,dc=com" -W "cn=liu,dc=my-domain,dc=com"
```


不想要得话，就删除

```java
ldapsearch -D "cn=manager,dc=my-domain,dc=com" -W -b "cn=liu,dc=my-domain,dc=com"
```

-b指定要查询的根节点

看一下在程序中如何对ldap service进行操作。这里使用的JLDAP
  
```java
  
       LDAPConnection lc = new LDAPConnection();
       String host = "127.0.0.1";
        try {
            lc.connect(host, 389);  //连接
            lc.bind(LDAPConnection.LDAP_V3, "cn=manager,dc=my-domain,dc=com", "secret");  //认证
            LDAPSearchResults rs = lc.search("dc=my-domain,dc=com", LDAPConnection.SCOPE_SUB, "objectClass=*", null, false);  //查询
            while(rs.hasMore()){
                LDAPEntry entry = rs.next();
                System.out.println(entry.getDN());
            }
           LDAPAttributeSet las = new LDAPAttributeSet();
           las.add(new LDAPAttribute("cn","quan"));
           las.add(new LDAPAttribute("objectClass","person"));
           las.add(new LDAPAttribute("sn","015"));
           LDAPEntry addEntry = new LDAPEntry("cn=quan,dc=my-domain,dc=com",las);
           lc.add(addEntry);    //添加
           lc.delete("cn=quan,dc=my-domain,dc=com");  //删除
           lc.disconnect();;
        } catch (LDAPException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

```

可以在代码里面看到增，加，删操作。其中增操作不需要进行认证，则lc.bind就不需要。      


在看下另一个方式访问JNDI_LDAP

```java

          Hashtable env = new Hashtable(11);
		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, "ldap://localhost");

		try {

			// Create the initial directory context
			DirContext ctx = new InitialDirContext(env);

			// Ask for all attributes of the object
			Attributes attrs = ctx.getAttributes("cn=Ryan,dc=my-domain,dc=com");

			// Find the surname ("sn") and print it
			System.out.println("sn: " + attrs.get("sn").get());

			// Close the context when we're done
			ctx.close();
		} catch (NamingException e) {
			System.err.println("Problem getting attribute: " + e);
		}

```