[英]Caused by: java.net.UnknownHostException: www.btechldap.com
I am trying to access LDAP Server through Java code as I have founded a free hosted server on the internet and want to test the reading mechanism via LDAP.我正在尝试通过 Java 代码访问 LDAP 服务器,因为我在互联网上建立了一个免费的托管服务器,并想通过 LDAP 测试读取机制。 But when I access it I get the captioned exception.
但是当我访问它时,我得到了标题异常。
I have surfed on the internet and found that the LDAP URL Format shall be as per the correct practice and standard.我在网上冲浪,发现 LDAP URL 格式应符合正确的做法和标准。 I fixed that too, but nothing worked.
我也解决了这个问题,但没有任何效果。
I dont know where I have done a mistake.我不知道我在哪里做错了。
The complete stack trace is provided below:下面提供了完整的堆栈跟踪:
Exception例外
Rdentry example failed.
javax.naming.CommunicationException: www.btechldap.com:1389 [Root exception is java.net.UnknownHostException: www.btechldap.com]
at com.sun.jndi.ldap.Connection.<init>(Unknown Source)
at com.sun.jndi.ldap.LdapClient.<init>(Unknown Source)
at com.sun.jndi.ldap.LdapClient.getInstance(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at javax.naming.directory.InitialDirContext.<init>(Unknown Source)
at LDAPAccess.main(LDAPAccess.java:33)
Caused by: java.net.UnknownHostException: www.btechldap.com
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.sun.jndi.ldap.Connection.createSocket(Unknown Source)
... 15 more
CODE代码
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class LDAPAccess {
public static void main(String[] args) {
Hashtable env = new Hashtable(5, 0.75f);
/*
* Specify the initial context implementation to use.
* This could also be set by using the -D option to the java program.
* For example,
* java -Djava.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory \
* Rdentry
*/
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, "ldap://www.btechldap.com:1389/dc=btechsample,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_CREDENTIALS, "btechpass");
try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);
/* Read Babs' entry */
Attributes attrs = ctx.getAttributes("uid");
if (attrs == null) {
System.out.println("uid" + "has no attributes");
} else {
/* print each attribute */
for (NamingEnumeration ae = attrs.getAll();
ae.hasMoreElements();) {
Attribute attr = (Attribute)ae.next();
String attrId = attr.getID();
/* print each value */
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println(attrId + ": " + vals.nextElement()))
;
}
}
} catch (NamingException e) {
System.err.println("Rdentry example failed.");
e.printStackTrace();
}
}
}
I am new to LDAP, Please help!我是 LDAP 的新手,请帮忙!
The code is specifying the credentials (password) but missing the principal (ie the user to authenticate as).该代码指定了凭据(密码),但缺少主体(即要进行身份验证的用户)。 Just add the following line:
只需添加以下行:
env.put(Context.SECURITY_PRINCIPAL, "cn=readonlybind,ou=admins,dc=btechsample,dc=com");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.