LDAP搭建及其Java代碼連接

OpenLDAP implementation of the Lightweight Directory Access Protocol.  http://www.openldap.org/

 

Windows 下安裝 LDAP:

 

1、下載下面附件中的 openldap-2.2.29-db-4.3.29-openssl-0.9.8a-win32_Setup.rar 解壓後安裝,安裝的時候一直點擊“下一步”即可。

 

2、配置LDAP,假設我們安裝到了“C:\Program Files\OpenLDAP”,在該目錄中找到 slapd.conf 文件,修改文件內容。在 include  ./schema/core.schema 後面添加如下內容:

    include  ./schema/cosine.schema
    include  ./schema/inetorgperson.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

找到

suffix  "dc=my-domain,dc=com"
rootdn  "cn=Manager,dc=my-domain,dc=com"

 

將如上兩行修改爲

suffix "o=tcl,c=cn" 
rootdn "cn=Manager,o=tcl,c=cn"

下面的

rootpw        secret

是Manager的明文密碼。

 

3、啓動 OpenLDAP ,CMD切換到“C:\Program Files\OpenLDAP” 然後執行如下命令

slapd -d 1

 

4、建立新的條目

創建一個文本文件 item.ldif 其內容如下:

dn: o=tcl,c=cn
objectClass: dcObject
objectClass: organization
o: tcl
dc: com

dn: uid=Unmi, o=tcl,c=cn
uid: Unmi
objectClass: inetOrgPerson
mail: [email protected]
userPassword:: MTIzNDU2
labeledURI: http://unmi.blogcn.com
sn: Qiu
cn:: 6ZqU5Y+26buE6I66

執行命令:ldapadd -x -D "cn=manager,o=tcl,c=cn" -w secret -f item.ldif ,將內容導入到ldap中。

 

5、查看LDAP中的條目

下載下面附件 "LdapAdmin.rar" ,將其解壓無需安裝。配置連接信息:



 

 

連接成功之後,查看剛剛創建的條目:



 

 

6、Java讀取條目

package com.neusoft.util;

import java.util.Hashtable;

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;
        
public   class  LDAPTest {   
       public   static   void  main(String[] args) {   
        LDAPTest LDAPTest1 =  new  LDAPTest();   
        String root =  "o=tcl,c=cn" ;  //root   
        Hashtable env =  new  Hashtable();   
        env.put(Context.INITIAL_CONTEXT_FACTORY,  "com.sun.jndi.ldap.LdapCtxFactory" );   
        env.put(Context.PROVIDER_URL,  "ldap://localhost/"  + root);       
        env.put(Context.SECURITY_AUTHENTICATION,  "simple" );   
        env.put(Context.SECURITY_PRINCIPAL,  "cn=Manager,o=tcl,c=cn" );   
        env.put(Context.SECURITY_CREDENTIALS,  "secret" );   
        DirContext ctx =  null ;   
         try  {   
          ctx =  new  InitialDirContext(env);   
          System.out.println( "Ldap連接成功" );   
          SearchControls constraints = new SearchControls();
          constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
          NamingEnumeration en = ctx.search("", "(uid=Unmi)", constraints);
          while(en!=null && en.hasMoreElements()){
        	  Object obj = en.nextElement() ;
        	  if (obj instanceof SearchResult) {
				SearchResult result = (SearchResult) obj;
				result.getName();
				Attributes attrs = result.getAttributes();
				System.out.println(attrs.toString());
				Attribute attr = attrs.get("userPassword");
				String str = new String((byte[])attr.get());
				System.out.println(str);
			}
          }
        }   
         catch  (javax.naming.AuthenticationException e) {   
          e.printStackTrace();   
          System.out.println( "Ldap連接失敗" );   
        }   
         catch  (Exception e) {   
          System.out.println( "認證出錯:" );   
          e.printStackTrace();   
        }   
        
         if  (ctx !=  null ) {   
           try  {   
            ctx.close();   
          }   
           catch  (NamingException e) {   
             //ignore   
          }   
        }   
      }   
}  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章