2013-02-15 56 views
1

我想用Java(Eclipse)在我的活动目录上获取属性。Java eclipse - 活动目录,属性修改

我发现这个代码:

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, "?????????")放置那个作品?

非常感谢。

+0

为什么不把你发现的代码放在你的代码中:'com.sun.jndi.ldap.LdapCtxFactory'?不要直接调用'getLdapCtxInstance',就像你的例子和'new InitialDirContext(env)' – 2013-02-15 16:59:38

回答

0

在你找到的例子中,建立连接,当你做:

// 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,如果我没有弄错的话。