从 java 应用程序连接 LDAP 服务器

发布于 2024-09-03 03:29:26 字数 109 浏览 4 评论 0原文

我正在构建一个基于 GXT (J2EE) 的应用程序。现在的问题是我必须将应用程序连接到 LDAP 服务器。你能告诉我如何从我们的 java 应用程序连接 LDAP 服务器以及我必须使用什么库或 API?

I am building an application based on GXT (J2EE). Now the problem is that I have to connect the application to a LDAP server. Can you tell me how to connect a LDAP server from our java application and what Library or API I will have to use for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事还在继续 2024-09-10 03:29:26

要连接到 LDAP,请查看以下包/类:

javax.naming.directory.*
javax.naming.ladp.*
com.sun.jndi.ldap.LdapCtxFactory
com.sun.jndi.ldap.ControlFactory

示例代码:

//build a hashtable containing all the necessary configuration parameters
Hashtable<String, String> environment = new Hashtable<String, String>();

environment.put(LdapContext.CONTROL_FACTORIES, conf.getProperty("ldap.factories.control"));
environment.put(Context.INITIAL_CONTEXT_FACTORY, conf.getProperty("ldap.factories.initctx"));
environment.put(Context.PROVIDER_URL, conf.getProperty("ldap.host"));
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, conf.getProperty("ldap.user"));
environment.put(Context.SECURITY_CREDENTIALS, conf.getProperty("ldap.password"));
environment.put(Context.STATE_FACTORIES, "PersonStateFactory");
environment.put(Context.OBJECT_FACTORIES, "PersonObjectFactory");

// connect to LDAP
DirContext ctx = new InitialDirContext(environment);

// Specify the search filter
String FILTER = "(&(objectClass=Person) ((sAMAccountName=" + user.getUsername() + ")))";

// limit returned attributes to those we care about
String[] attrIDs = { "sn", "givenName" };

SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

// Search for objects using filter and controls
NamingEnumeration answer = ctx.search(searchBase, FILTER, ctls);

...

SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
surName = attrs.get("sn").toString();
givenName = attrs.get("givenName").toString();
...

在此示例中,我有一个从配置文件读取这些值的配置对象。

这些值将是:

# LDAP parameters
ldap.host = ldap://ldap.mydomain.com:389
ldap.factories.initctx = com.sun.jndi.ldap.LdapCtxFactory
ldap.factories.control = com.sun.jndi.ldap.ControlFactory
ldap.searchbase = dc=mydomain,dc=us
ldap.user = MYDOMAIN.COM\\ldap-user
ldap.userBase= MYDOMAIN.COM\\
ldap.password = ******

To connect to LDAP, check out the following packages/classes:

javax.naming.directory.*
javax.naming.ladp.*
com.sun.jndi.ldap.LdapCtxFactory
com.sun.jndi.ldap.ControlFactory

Example code:

//build a hashtable containing all the necessary configuration parameters
Hashtable<String, String> environment = new Hashtable<String, String>();

environment.put(LdapContext.CONTROL_FACTORIES, conf.getProperty("ldap.factories.control"));
environment.put(Context.INITIAL_CONTEXT_FACTORY, conf.getProperty("ldap.factories.initctx"));
environment.put(Context.PROVIDER_URL, conf.getProperty("ldap.host"));
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, conf.getProperty("ldap.user"));
environment.put(Context.SECURITY_CREDENTIALS, conf.getProperty("ldap.password"));
environment.put(Context.STATE_FACTORIES, "PersonStateFactory");
environment.put(Context.OBJECT_FACTORIES, "PersonObjectFactory");

// connect to LDAP
DirContext ctx = new InitialDirContext(environment);

// Specify the search filter
String FILTER = "(&(objectClass=Person) ((sAMAccountName=" + user.getUsername() + ")))";

// limit returned attributes to those we care about
String[] attrIDs = { "sn", "givenName" };

SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

// Search for objects using filter and controls
NamingEnumeration answer = ctx.search(searchBase, FILTER, ctls);

...

SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
surName = attrs.get("sn").toString();
givenName = attrs.get("givenName").toString();
...

In this example I have a Configuration object that reads these values from a config file.

The values would be :

# LDAP parameters
ldap.host = ldap://ldap.mydomain.com:389
ldap.factories.initctx = com.sun.jndi.ldap.LdapCtxFactory
ldap.factories.control = com.sun.jndi.ldap.ControlFactory
ldap.searchbase = dc=mydomain,dc=us
ldap.user = MYDOMAIN.COM\\ldap-user
ldap.userBase= MYDOMAIN.COM\\
ldap.password = ******
一梦浮鱼 2024-09-10 03:29:26
  • 使用 JNDI(Java 命名和
    目录接口)Java 中的 API。
  • JNDI 的接口、类和异常可在
    JDK 附带以下软件包:

    • javax.naming.*
    • javax.naming.directory.*
  • 这意味着我们不必使用任何外部库来工作
    在大多数情况下,使用 LDAP 服务器。

  • 指定 LDAP 服务器的 URL,其中包含主机名
    LDAP 服务器正在运行的端口号。众所周知的端口号
    轻量级目录访问协议是默认值 389。

  • 还需要为连接指定一些环境属性
    以及 Hashtable 对象中的身份验证。

这是示例代码:

import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;

public class Ldap
{
    public static void main(String[]args)
    {
        Hashtable<String, String> environment = new Hashtable<String, String>();

        environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        environment.put(Context.PROVIDER_URL, "ldap://<hostname>:389");
        environment.put(Context.SECURITY_AUTHENTICATION, "simple");
        environment.put(Context.SECURITY_PRINCIPAL, "<Login DN>");
        environment.put(Context.SECURITY_CREDENTIALS, "<password>");

        try 
        {
            DirContext context = new InitialDirContext(environment);
            System.out.println("Connected..");
            System.out.println(context.getEnvironment());
            context.close();
        } 
        catch (AuthenticationNotSupportedException exception) 
        {
            System.out.println("The authentication is not supported by the server");
        }

        catch (AuthenticationException exception)
        {
            System.out.println("Incorrect password or username");
        }

        catch (NamingException exception)
        {
            System.out.println("Error when trying to create the context");
        }
    }
}
  • Connection to a LDAP server is made using JNDI (Java Naming and
    Directory Interface) APIs in Java.
  • The JNDI’s interfaces, classes and exceptions are available in the
    following packages come with JDK:

    • javax.naming.*
    • javax.naming.directory.*
  • That means we don’t have to use any external libraries for working
    with LDAP servers, in most cases.

  • That specifies URL of a LDAP server consists of hostname on which
    LDAP Server is running port number. A well known port number of the
    Lightweight Directory Access Protocol is 389 which is default.

  • Also need to specify some environment properties for the connection
    and authentication in a Hashtable object.

Here is the sample code:

import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;

public class Ldap
{
    public static void main(String[]args)
    {
        Hashtable<String, String> environment = new Hashtable<String, String>();

        environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        environment.put(Context.PROVIDER_URL, "ldap://<hostname>:389");
        environment.put(Context.SECURITY_AUTHENTICATION, "simple");
        environment.put(Context.SECURITY_PRINCIPAL, "<Login DN>");
        environment.put(Context.SECURITY_CREDENTIALS, "<password>");

        try 
        {
            DirContext context = new InitialDirContext(environment);
            System.out.println("Connected..");
            System.out.println(context.getEnvironment());
            context.close();
        } 
        catch (AuthenticationNotSupportedException exception) 
        {
            System.out.println("The authentication is not supported by the server");
        }

        catch (AuthenticationException exception)
        {
            System.out.println("Incorrect password or username");
        }

        catch (NamingException exception)
        {
            System.out.println("Error when trying to create the context");
        }
    }
}
靖瑶 2024-09-10 03:29:26

您甚至可以使用 Netscape LDAP SDK,它目前尚未激活,但可以提供更多控制LDAP编程

You can even use Netscape LDAP SDK which is currently not active but gives more control in LDAP Programming

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文