通过JAVA访问LDAP而不提供密码
在C#中,我编写了以下代码来连接LDAP服务器并查询相同的内容通过JAVA访问LDAP而不提供密码,java,c#,ldap,Java,C#,Ldap,在C#中,我编写了以下代码来连接LDAP服务器并查询相同的内容 String ldapUrl = "LDAP://..."; DirectoryEntry entry = new DirectoryEntry(ldapUrl); DirectorySearcher dSearch = new DirectorySearcher(entry); String Name = "ravi"; dSearc
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"));
}
如果您注意到,我没有提供用户名和/或密码。我认为它使用OS登录用户凭据登录到LDAP服务器
但是在爪哇
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");
}
java中有没有一种方法可以绑定到LDAP服务器而不提供用户名和密码?我试图通过将Context.SECURITY\u身份验证设置为NONE来绑定到,但它会引发不允许匿名登录的异常。我不知道如何使用匿名用户凭据,但操作系统登录用户凭据
这可能吗?如何实现
关于,我使用JNI调用C#dll。。。问题是JNI非常慢。每次调用大约需要15-20秒使用命令行(cmd),在JAVA中,::从此开始
导入com4j.变量;
进口com4j.typelibs.ado20.ClassFactory;
导入com4j.typelibs.ado20.\u命令;
导入com4j.typelibs.ado20.\u连接;
导入com4j.typelibs.ado20.\u记录集;
public static void queryADForComputers()引发异常{
String query=“cn、sn、givenName、department”;
字符串筛选器=“(&(objectclass=user)(objectcategory=person))”;
String namingContext=“OU=台式机,OU=工作站,OU=HO,DC=win”;
_Connection conn=ClassFactory.createConnection();
连接提供商(“ADSSOObject”);
conn.open(“Active Directory提供程序”、“”、“”、-1);
_Command cmd=ClassFactory.createCommand();
cmd.activeConnection(conn);
cmd.commandText(“;”+filter+“;”+query+“子树”);
_记录集rs=cmd.execute(null,Variant.getMissing(),-1);
System.out.println(“find”+rs.recordCount()+“users/computers/我要找的任何东西”);
//然后在这里可以使用while循环while(!rs.eof())
//在其中,您可以将每个值作为rs.fields().item(i).value()获取;
//在我的例子中,我做了rs.fields().item(i).value().toString()
//或者你可以检查它的类型并从那里开始。
}
我假设您希望使用Windows集成身份验证,允许Windows操作系统提供凭据。挑战在于从操作系统获取凭据。您的应用程序是基于http的吗?是的,Windows集成身份验证,我们为应用程序客户端和基于http的客户端提供服务。这是一个在WAS中运行的Web服务。WAS配置为windows服务,并配置为在具有查询LDAP权限的服务帐户下运行。应用程序将为我提供用户名。我只需要通过查询LDAP来确保这些用户能够访问我的服务。我知道WAS可以配置为执行AD auth。但是目前我们面临一些cookie问题,所以我不能使用它。所以,这在JAVA中是不可能的?六羟甲基三聚氰胺六甲醚。。。我将不得不为C#编写JNI包装器:(AFIK,是的,不确定您的详细信息。可能能够使用JAAS和Kerberos,或者可能需要从JNI访问LSA或SPPI。我使用JNI调用C#dll…问题是,JNI非常慢。每次调用几乎需要15-20秒
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.
}