简体   繁体   中英

LDAP via JAVA without providing password

in C#, I have written below code to connect to LDAP server and query the same.

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.

but in 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? I tried bind to by setting the Context.SECURITY_AUTHENTICATION as NONE, but it them throws the exception for anonymous login not allowed. 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. it is taking almost 15-20 sec per call

Use command line (cmd), in JAVA, :: from this stack Overflow Answer

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. 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM