我正在构建一个基于 GXT (J2EE) 的应用程序。现在的问题是我必须将应用程序连接到 LDAP 服务器。你能告诉我如何从我们的 java 应用程序连接 LDAP 服务器,以及我必须使用什么库或 API 吗?
问问题
55797 次
3 回答
12
要连接到 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 = ******
于 2010-05-21T09:59:17.850 回答
8
- 使用 Java 中的 JNDI(Java 命名和目录接口)API 连接到 LDAP 服务器。
JNDI 的接口、类和异常在 JDK 附带的以下包中可用:
- javax.命名。*
- 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");
}
}
}
于 2017-06-05T09:32:48.217 回答
-1
您甚至可以使用当前未激活但在 LDAP 编程中提供更多控制的Netscape LDAP SDK
于 2012-05-18T04:55:06.160 回答