我想使用 Java (Eclipse) 获取 Activity 目录的属性。
我找到了这段代码:
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");
// 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");
// Create the initial context
DirContext ctx = new InitialDirContext(env);
但我已经开始了解是否存在联系:
String ldapUrl = "ldap://"+serverAddress+":389";
//Prepare the environment with the username and password.
Hashtable env = new Hashtable();
DirContext ctx = null;
env.put(Context.SECURITY_PRINCIPAL, DOMAIN+username);
//env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
ctx = LdapCtxFactory.getLdapCtxInstance(ldapUrl, env); // To test the connection
那么如何创建一个 InitialDirContext 呢?
我应该在 env.put(Context.INITIAL_CONTEXT_FACTORY, "?????????")
中放入什么才能有效?
非常感谢。
最佳答案
在您找到的示例中,当您执行以下操作时,就会建立连接:
// Create the initial context
DirContext ctx = new InitialDirContext(env);
为此,您需要设置所有必需的环境属性,例如
SECURITY_CREDENTIALS
SECURITY_PRINCIPAL
SECURITY_AUTHENTICATION
PROVIDER_URL
INITIAL_CONTEXT_FACTORY
(possibly others depending on if you have encryption set in your Active Directory)
Java 将使用上下文工厂来创建该连接。如果您不指定它,java将使用默认值,即com.sun.jndi.ldap.LdapCtxFactory
(如果我没记错的话)。
关于Java eclipse - Activity 目录、属性修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14899339/