本文介绍了使用Java找到简单的Active Directory信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,所以请温柔。

This is my first post, so please be gentle.

我recentley开始使用PowerShell的工作来改变AD组,发现信息内容等,但我缺乏的,我喜欢这么多关于Java的图形用户界面。

I've recentley started using Powershell at work to change AD groups, find AD information etc. but I'm lacking the GUI that I like so much about Java.

有没有一种简单的方法(或例子code),由此我输入目标主机名,我与我要求的细节返回。公元memberhsip组,帐户信息等?

Is there a simple way (or example of code) whereby I enter a target hostname and I'm returned with the details I ask for. AD memberhsip groups, account info etc?

我的Java知识不是一样大,我的Powershell使尽可能多的帮助可能会很AP prechiated。

My Java knowledge isn't as great as my Powershell so as much help as possible would be really apprechiated.

感谢

推荐答案

如果您正在寻找一个完整的Java GUI来查询活动目录,你可以看下到的。

If you are looking for a full java GUI to query Active-Directory, you may have a look to Apache Directory Studio.

如果你只想用java查询AD,这里是一个示例code:

If you want to query AD just using java, here is a sample code :

class TestAD 
{ 
  static DirContext ldapContext; 
  public static void main (String[] args) throws NamingException 
  { 
    try 
    { 
      System.out.println("Début du test Active Directory"); 

      Hashtable<String, String> ldapEnv = new Hashtable<String, String>(11); 
      ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
      //ldapEnv.put(Context.PROVIDER_URL,  "ldap://societe.fr:389"); 
      ldapEnv.put(Context.PROVIDER_URL,  "ldap://dom.fr:389"); 
      ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); 
      //ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=administrateur,cn=users,dc=societe,dc=fr"); 
      ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=jean paul blanc,ou=MonOu,dc=dom,dc=fr"); 
      ldapEnv.put(Context.SECURITY_CREDENTIALS, "pwd"); 
      //ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl"); 
      //ldapEnv.put(Context.SECURITY_PROTOCOL, "simple"); 
      ldapContext = new InitialDirContext(ldapEnv); 

      // Create the search controls          
      SearchControls searchCtls = new SearchControls(); 

      //Specify the attributes to return 
      String returnedAtts[]={"sn","givenName", "samAccountName"}; 
      searchCtls.setReturningAttributes(returnedAtts); 

      //Specify the search scope 
      searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); 

      //specify the LDAP search filter 
      String searchFilter = "(&(objectClass=user))"; 

      //Specify the Base for the search 
      String searchBase = "dc=dom,dc=fr"; 
      //initialize counter to total the results 
      int totalResults = 0; 

      // Search for objects using the filter 
      NamingEnumeration<SearchResult> answer = ldapContext.search(searchBase, searchFilter, searchCtls); 

      //Loop through the search results 
      while (answer.hasMoreElements()) 
      { 
        SearchResult sr = (SearchResult)answer.next(); 

        totalResults++; 

        System.out.println(">>>" + sr.getName()); 
        Attributes attrs = sr.getAttributes(); 
        System.out.println(">>>>>>" + attrs.get("samAccountName")); 
      } 

      System.out.println("Total results: " + totalResults); 
      ldapContext.close(); 
    } 
    catch (Exception e) 
    { 
      System.out.println(" Search error: " + e); 
      e.printStackTrace(); 
      System.exit(-1); 
    } 
  } 
}

这篇关于使用Java找到简单的Active Directory信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:36