简体   繁体   中英

Run LDAP queries on AD through JAVA Code

I have configured ADDC on windows server 2012 R2 and I have added two users into DC - one is windows 8 and another one is ubuntu.

  • Windows server 2012 username - DC
  • Windows 8.1 username - Win
  • Ubuntu username - Linux

I am trying to achieve this - I want to write java program in ubuntu, that will connect to ADDC and sends back, detailed user information on windows 8.1

My program is like -

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class LdapSearch {
public static void main(String[] args) throws Exception {
Hashtable env = new Hashtable();

String sp = "com.sun.jndi.ldap.LdapCtxFactory";
env.put(Context.INITIAL_CONTEXT_FACTORY, sp);

String ldapUrl = "ldap://server.com, dc=com";
env.put(Context.PROVIDER_URL, ldapUrl);

DirContext dctx = new InitialDirContext(env);

String base = "ou=name";

SearchControls sc = new SearchControls();
String[] attributeFilter = { "cn", "mail" };
sc.setReturningAttributes(attributeFilter);
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

String filter = "(&(sn=W*)(l=Criteria*))";

NamingEnumeration results = dctx.search(base, filter, sc);
while (results.hasMore()) {
  SearchResult sr = (SearchResult) results.next();
  Attributes attrs = sr.getAttributes();

  Attribute attr = attrs.get("cn");
  System.out.print(attr.get() + ": ");
  attr = attrs.get("mail");
  System.out.println(attr.get());
}
dctx.close();
}

I am referring to above program and trying to achieve connection to AD through LDAP java. I dont know how to get ou , cn , etc.. I am very much new to the concepts of LDAP, ADDC.

Any idea on this? Please let me know.

Thanks, saurabh

I've done a similar scenario in C# so am not sure about the connection settings in Java but as for similarities you should create a directory entry for the LDAP and provide the path, user name and password of authorized user who can access the active directory, i didnt provide DC in the path just the LDAP path and then the query filter parameters that searched based upon user first name was

Filter = "(& (SAMAccountName=" + name + ") (| (&(objectCategory=person)(objectClass=user)(!(homeMDB=*))(!(msExchHomeServerName=*)))(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) ))";

then it would provide you with an result in an arraylist like object so you would query the rest of information you like by just providing the attribute name, you would find a list of LDAP attributes here

LDAP attributes

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