[英]LDAP via JAVA without providing password
in C#, I have written below code to connect to LDAP server and query the same. 在C#中,我编写了下面的代码来连接到LDAP服务器并查询相同的内容。
String ldapUrl = "LDAP://...";
DirectoryEntry entry = new DirectoryEntry(ldapUrl);
DirectorySearcher dSearch = new DirectorySearcher(entry);
String Name = "ravi";
dSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + Name + "))";
foreach (SearchResult sResultSet in dSearch.FindAll())
{
String data = "Login Name :" + (GetProperty(sResultSet, "cn")) + "\r\n" +
"First Name :" + (GetProperty(sResultSet, "givenName")) + "\r\n" +
"Middle Initials :" + (GetProperty(sResultSet, "initials")) + "\r\n" +
"Last Name : " + (GetProperty(sResultSet, "sn"));
}
If you notice, no where I have provided the username and or password. 如果您注意到,我没有提供用户名和/或密码。 I think it logs-in to the LDAP server using the OS logged in users credentials. 我认为它使用登录用户凭据的操作系统登录LDAP服务器。
but in JAVA 但在JAVA
String url = "ldap://localhost:10389";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(***Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"***);
env.put(***Context.SECURITY_CREDENTIALS, "secret"***);
try {
DirContext ctx = new InitialDirContext(env);
System.out.println("connected");
System.out.println(ctx.getEnvironment());
ctx.close();
} catch (Exception ex) {
System.out.println("error when trying to create the context");
}
Is there a way in java to bind to the LDAP server without providing the username and password? java中是否有一种方法可以在不提供用户名和密码的情况下绑定到LDAP服务器? I tried bind to by setting the Context.SECURITY_AUTHENTICATION as NONE, but it them throws the exception for anonymous login not allowed. 我通过将Context.SECURITY_AUTHENTICATION设置为NONE来尝试绑定,但是它们会抛出匿名登录的例外,不允许。 I don't what to use Anonymous user credentials but the OS logged in users credentials. 我不知道如何使用匿名用户凭据,但操作系统登录用户凭据。
is this possible and how? 这可能吗?怎么样?
Regards, 问候,
I used JNI to invoke a C# dll... the problem is JNI is very slow. 我使用JNI来调用C#dll ...问题是JNI非常慢。 it is taking almost 15-20 sec per call 每次通话大约需要15-20秒
Use command line (cmd), in JAVA, :: from this stack Overflow Answer 在JAVA中使用命令行(cmd)::来自此堆栈溢出应答
import com4j.Variant;
import com4j.typelibs.ado20.ClassFactory;
import com4j.typelibs.ado20._Command;
import com4j.typelibs.ado20._Connection;
import com4j.typelibs.ado20._Recordset;
public static void queryADForComputers() throws Exception{
String query = "cn,sn,givenName,department";
String filter = "(&(objectclass=user)(objectcategory=person))";
String namingContext = "OU=Desktops,OU=Workstations,OU=HO,DC=win";
_Connection conn = ClassFactory.createConnection();
conn.provider("ADsDSOObject");
conn.open("Active Directory Provider","","",-1);
_Command cmd = ClassFactory.createCommand();
cmd.activeConnection(conn);
cmd.commandText("<LDAP://" + namingContext + ">;" + filter + ";" + query + ";subTree");
_Recordset rs = cmd.execute(null, Variant.getMissing(), -1);
System.out.println("Found " + rs.recordCount() + " users/computers/whatever i was looking for");
//Then here you can use a while loop while(!rs.eof())
//in which you can get each value as rs.fields().item(i).value();
//in my case, i did rs.fields().item(i).value().toString()
//or you can check for its type and go from there.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.