从 Java 应用程序连接 LDAP 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2880968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting LDAP server from java application
提问by dhiraj
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?
我正在构建一个基于 GXT (J2EE) 的应用程序。现在的问题是我必须将应用程序连接到 LDAP 服务器。你能告诉我如何从我们的 Java 应用程序连接 LDAP 服务器以及我必须使用什么库或 API 吗?
回答by Pierre Henry
To connect to LDAP, check out the following packages/classes:
要连接到 LDAP,请查看以下包/类:
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 = ******
回答by Dungeon Hunter
You can even use Netscape LDAP SDKwhich is currently not active but gives more control in LDAP Programming
您甚至可以使用Netscape LDAP SDK,它当前未处于活动状态,但可以在 LDAP 编程中提供更多控制
回答by Pratik Patil
- 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.
- 使用 Java 中的 JNDI(Java 命名和目录接口)API 连接到 LDAP 服务器。
JNDI 的接口、类和异常在 JDK 附带的以下包中可用:
- javax.naming.*
- javax.naming.directory.*
这意味着在大多数情况下,我们不必使用任何外部库来处理 LDAP 服务器。
指定 LDAP 服务器的 URL 由主机名组成,LDAP 服务器在其上运行端口号。一个众所周知的轻量目录访问协议的端口号是 389,这是默认的。
还需要在 Hashtable 对象中为连接和身份验证指定一些环境属性。
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");
}
}
}