使用 Java 查找简单的 Active Directory 信息 [英] Using Java to find simple Active Directory Information

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

问题描述

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

This is my first post, so please be gentle.

我最近开始在工作中使用 Powershell 来更改 AD 组、查找 AD 信息等.但是我缺少我非常喜欢的 Java 的 GUI.

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.

是否有一种简单的方法(或代码示例)可以让我输入目标主机名,然后返回我要求的详细信息.AD 会员群组、帐户信息等?

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 丰富,所以我真的很感激尽可能多的帮助.

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

谢谢

推荐答案

如果你正在寻找一个完整的 Java GUI 来查询 Active-Directory,你可以看看 Apache Directory Studio.

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

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

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 信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆