简体   繁体   English

连接到Ldap

[英]Connection To Ldap

I need to write a piece of code in Java which will make a connection to LDAP and retrieve a few values from there. 我需要用Java编写一段代码,它将与LDAP建立连接并从那里检索一些值。

I need to know what details I need to establish the connection to LDAP. 我需要知道建立与LDAP连接所需的细节。

Java uses JNDI as a means to interface with a LDAP directory server. Java使用JNDI作为与LDAP目录服务器交互的手段。 There is a great JNDI tutorial provided by Oracle. Oracle提供了一个很棒的JNDI教程 That will detail the JNDI API and explain how it relates to LDAP operations. 这将详细介绍JNDI API并解释它与LDAP操作的关系。 It is replete with code examples on how to connect, authenticate, and query a directory. 它充满了关于如何连接,验证和查询目录的代码示例。

Here you got some snippets of code to show how it looks to implement a change password operation, you could use it as a starting point to learn more about LDAP connection from Java. 在这里,您可以获得一些代码片段来展示实现更改密码操作的方式,您可以将其作为起点来了解有关Java连接的更多信息。 Check the method getCtx()... 检查方法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;
    }

}

Here is the code for LDAP connection.. 这是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);
}
}

Use jldap 使用jldap

Here is the example code: 这是示例代码:

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

This Spring LDAP configuration tutorial will help you implement a Java client for LDAP. 此Spring LDAP配置教程将帮助您实现LDAP的Java客户端。 It contains piece of code to connect to LDAP and do operations. 它包含连接到LDAP并执行操作的代码段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM