连接到Ldap

时间:2011-04-06 12:28:19

标签: java ldap

我需要用Java编写一段代码,它将与LDAP建立连接并从那里检索一些值。

我需要知道建立与LDAP连接所需的详细信息。

5 个答案:

答案 0 :(得分:4)

Java使用JNDI作为与LDAP目录服务器交互的方法。 Oracle提供了很棒的JNDI tutorial。这将详细介绍JNDI API并解释它与LDAP操作的关系。它充满了关于如何连接,验证和查询目录的代码示例。

答案 1 :(得分:4)

在这里,您获得了一些代码片段,以显示实现更改密码操作的方式,您可以将其作为起点来了解有关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;
    }

}

答案 2 :(得分: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);
}
}

答案 3 :(得分: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 :(得分:0)

此Spring LDAP配置tutorial将帮助您实现LDAP的Java客户端。它包含连接到LDAP并执行操作的代码段。