Active Directory query through Java

What is the best way to query Active Directory with Java?

Now I know that .NET has special methods built-in for this kind of thing. But in Java, could a Powershell script be called running a process / command line in a good way?

Also, why was it decided to call C # from Java anytime Java needs access to Windows WMI data? Is there a reason why starting something like pstools (which can run processes / commands on remote computers) from a command line called Java will not work?

thanks

+4
source share
3 answers

What is the best way to query Active Directory with Java?

I do not know if this is BEST , but a typical approach is to use LDAP. You can do this with JNDI. Examples of this can be found here .

Now I know that .NET has special methods created for this kind of thing. But in Java, a Powershell script by running a process / command line is a good way to do this?

It depends. I think some java people will shrink because this is a pretty obvious violation of the โ€œwrite once, run anywhereโ€ mentality. However, if you can guarantee that your application will be used only in windows and you have no other alternatives, I see no harm.

Also, why is it decided to call C # from Java anytime Java needs to access Windows WMI data? Is there a reason why something like pstools (which can run processes / commands on remote computers) from a command line in Java will not work?

I'm not sure I know the answer to this question. I think I will need to see some clear examples to crack it.

Hope this helps.

+6
source

Please pass the following code.

package active.security.util.ldap; import java.util.Date; import java.util.Hashtable; import javax.naming.*; import javax.naming.directory.*; //import javax.naming.ldap.PagedResultsControl; import active.security.util.DateTool; public class JNDISearch { public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory"; // driver public static String MY_HOST = "ldap://dcserver:389"; // server and port public static String MY_SEARCHBASE = "DC=active,DC=local"; // base DC public static String MY_FILTER = "(&(objectclass=User)(!(objectclass=computer)))"; // filter public static String MGR_DN = "domain\\username"; // username public static String MGR_PW = "password"; // password public static String MY_ATTRS[] = {/* "cn","userpassword","mail", */"cn" }; public static String temp = new String(); public static void main(String[] ags) throws Exception{ new JNDISearch().search(); } @SuppressWarnings({ "rawtypes", "unchecked" }) public String search() throws Exception { int userCount = 0; Date begin = new Date(); try { Hashtable env = new Hashtable(); //PagedResultsControl control = new PagedResultsControl(5000, true); env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX); env.put(Context.PROVIDER_URL, MY_HOST); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, MGR_DN); env.put(Context.SECURITY_CREDENTIALS, MGR_PW); DirContext ctx = new InitialDirContext(env); SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER,constraints); while (results != null && results.hasMore()) { SearchResult sr = (SearchResult) results.next(); // String dn = sr.getName(); String dn = sr.getName() + "," + MY_SEARCHBASE; Attributes ar = ctx.getAttributes(dn, MY_ATTRS); if (ar == null) { System.out.println("Entry " + dn + " has none of the specified attributes\n"); } else { Attribute attr = ar.get("cn"); String cn = (String)attr.get(0); System.out.println(cn); } userCount++; } } catch (Exception e) { e.printStackTrace(); } finally{ Date end = new Date(); long seconds = DateTool.getSenconds(begin, end); System.out.println("total user: "+userCount); System.out.println("time cost: "+seconds+" seconds"); } return null; } } 
+4
source

You can use this library. It is easy to use and powerful.

http://code.google.com/p/jedi-obi/

+1
source

Source: https://habr.com/ru/post/1332284/


All Articles