簡體   English   中英

連接到Ldap

[英]Connection To Ldap

我需要用Java編寫一段代碼,它將與LDAP建立連接並從那里檢索一些值。

我需要知道建立與LDAP連接所需的細節。

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

在這里,您可以獲得一些代碼片段來展示實現更改密碼操作的方式,您可以將其作為起點來了解有關Java連接的更多信息。 檢查方法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;
    }

}

這是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);
}
}

使用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;
    }

此Spring LDAP配置教程將幫助您實現LDAP的Java客戶端。 它包含連接到LDAP並執行操作的代碼段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM