【发布时间】:2014-01-07 22:44:17
【问题描述】:
我发现了这个简单的 java 连接 LDAP 服务器示例:
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
/**
* Demonstrates how to create an initial context to an LDAP server
* using SSL. For this example to work, JSSE must be installed and
* configured, and the issuer of the LDAP server's certificate must
* be in the JSSE trust store.
*
* usage: java Ssl
*/
class Ssl {
public static void main(String[] args) {
// Set up environment for creating initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:636/o=JNDITutorial");
// Specify SSL
env.put(Context.SECURITY_PROTOCOL, "ssl");
// Authenticate as S. User and password "mysecret"
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
env.put(Context.SECURITY_CREDENTIALS, "mysecret");
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
System.out.println(ctx.lookup("ou=NewHires"));
// ... do something useful with ctx
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
有什么方法可以在我连接时获取 LDAP 服务器和版本?
【问题讨论】: