2011-04-06 120 views
3

我需要用Java編寫一段代碼,它將建立到LDAP的連接並從那裏檢索幾個值。連接到Ldap

我需要知道我需要建立到LDAP的連接的詳細信息。

+0

下面是一些示例代碼,以幫助您開始:http://developer.novell.com/documentation/samplecode/jldap_sample/index.htm – Nishan 2011-04-06 12:36:43

回答

4

Java使用JNDI作爲連接LDAP目錄服務器的一種手段。 Oracle提供了一個很棒的JNDI tutorial。這將詳細介紹JNDI API並解釋它與LDAP操作的關係。它充滿了關於如何連接,驗證和查詢目錄的代碼示例。

0

使用jldap

下面是示例代碼:

int ldapVersion  = LDAPConnection.LDAP_V3; 

    try 
    { 
     if(conn == null) 
      conn = new LDAPConnection(); 

     // connect to the server 
     if(conn.isConnected() == false) 
      conn.connect(hostName, port); 

     // bind to the server 
     if(authType.equals("Anonymous")) 
     { 
      conn.bind("Anonymous" ,null); 
     } 
     else 
     { 
      conn.bind(ldapVersion, login, password.getBytes("UTF8")); 
     } 

     Logs.write("LDAP CONNECTION Established "); 
     return true; 

    } 
    catch (LDAPException ex) { 

     Logs.write("CONNECTION ERROR "+ex.toString()); 
     return false; 
    } 
    catch (IllegalArgumentException ex) 
    { 
     Logs.write("CONNECTION ERROR "+ex.toString()); 
     return false; 
    } 
4

在這裏,你得到了一些代碼片段,以顯示它的外觀來實現更改密碼的操作,您可以使用它作爲一個起點了解更多關於Java的LDAP連接。檢查方法getCtx()...

package so; 

import java.util.Hashtable; 
import javax.naming.Context; 
import javax.naming.NamingException; 
import javax.naming.directory.BasicAttribute; 
import javax.naming.directory.DirContext; 
import javax.naming.directory.InitialDirContext; 
import javax.naming.directory.ModificationItem; 

public class DemoLdap4SO { 

    private void changePassword(String principal, String oldPassword, String newPassword) 
     throws NamingException { 
     InitialDirContext ctx = getCtx(principal, oldPassword); 
     if (ctx == null || newPassword == null || newPassword.equals("")) { 
      throw new NamingException(); 
     } 
     BasicAttribute attr = new BasicAttribute("userpassword", newPassword); 
     ModificationItem mi = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr); 
     ModificationItem[] items = new ModificationItem[1]; 
     items[0] = mi; 
     ctx.modifyAttributes(getUserDN(principal), items); 
    } 

    private String getUserDN(String user) { 
     String m_usersDn = "cn=Users,your realm"; 
     String usrDn = "cn=" + user + "," + m_usersDn; 
     return usrDn; 
    } 

    private InitialDirContext getCtx(String user, String pswd) throws NamingException { 
     String ldapUrl = "put your ldap url here"; 
     String ldapRealm = "put your realm here"; 
     Hashtable ht = new Hashtable(); 
     ht.put(Context.INITIAL_CONTEXT_FACTORY, 
     "com.sun.jndi.ldap.LdapCtxFactory"); 
     ht.put(Context.PROVIDER_URL, ldapUrl); 
     ht.put(Context.SECURITY_AUTHENTICATION, "simple"); 
     ht.put(Context.SECURITY_PRINCIPAL, getUserDN(user)); 
     ht.put(Context.SECURITY_CREDENTIALS, pswd); 
     try { 
      return new InitialDirContext(ht); 
     } catch (NamingException exc) { 
      // log error 
     } 
     return null; 
    } 

} 
0

這個春天LDAP配置tutorial將幫助你實現一個Java客戶端的LDAP。它包含連接到LDAP並執行操作的一段代碼。

1

這裏是LDAP連接的代碼..

public Connection() 
{ 
try 
{ 

    System.setProperty("javax.net.ssl.trustStore", TRUST_STORE); 

    ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 

    ldapEnv.put(Context.PROVIDER_URL, "ldap://localhost:389"); 
    ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); 
    ldapEnv.put(Context.SECURITY_PRINCIPAL, SECURITY_PRINCIPAL + BASE_NAME); 
    ldapEnv.put(Context.SECURITY_CREDENTIALS, SECURITY_CREDENTIALS); 

    ldapContext = new InitialDirContext(ldapEnv); 

catch (Exception e) 
{ 
    System.out.println(" bind error: " + e); 
    e.printStackTrace(); 
    System.exit(-1); 
} 
}