java如何连接AD
/**
* 初始化LDAP 目录服务上下文(打开数据库连接)
*/
public void initialContext()
{
Hashtable<String, String> env = new Hashtable<String, String>();
//密码
String password = "#cqjw123";
String url = "ladp://10.224.0.73:389";
//用户名
String user = "cn=administrator,cn=users,dc=chongqing,dc=jiao,dc=com";
env.put(Context.SECURITY_AUTHENTICATION, "易做图");
env.put(Context.SECURITY_PRINCIPAL, user);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
try
{
ctx = new InitialDirContext(env);
//Attributes answer = ctx.getAttributes("ou=uc,dc=chongqing,dc=jiao,dc=com");
//System.out.println("值:"+answer);
//关闭
ctx.close();
System.out.println("验证成功");
}
catch (Exception e)
{
System.out.println("失败:");
e.printStackTrace();
}
}
我是这样连接AD 失败了:
javax.naming.NamingException: Cannot parse url: ladp://10.224.0.73:389 [Root exception is java.net.MalformedURLException: Not an LDAP URL: ladp://10.224.0.73:389]
at com.sun.jndi.ldap.LdapURL.<init>(LdapURL.java:77)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:146)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:197)
at javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:82)
at com.ctfo.testdemo.test.Test.initialContext(Test.java:39)
at com.ctfo.testdemo.test.Test.main(Test.java:18)
Caused by: java.net.MalformedURLException: Not an LDAP URL: ladp://10.224.0.73:389
at com.sun.jndi.ldap.LdapURL.<init>(LdapURL.java:71)
... 11 more
求一段可行的 连接AD的代码 谢谢 由于以前没做过这个 实在头疼 网上也找了很多 都没实现 然后还有像问的是 如何知道连接成功呢! 谢谢
ou=uc,dc=chongqing,dc=jiao,dc=com这是根节点 谢谢 --------------------编程问答-------------------- 顺便问下 env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); 这个是在干嘛呢 谢谢 --------------------编程问答-------------------- 很多了。
这个也是 --------------------编程问答-------------------- 能详细点么 谢谢 --------------------编程问答-------------------- 我也是网上搜的。你搜搜看吧。 --------------------编程问答-------------------- url写错了吧,ldap还是ladp? --------------------编程问答-------------------- http://download.csdn.net/detail/wenjie4892543/5992299
补充:Java , Java EE