4
這是我的第一篇文章,所以請溫和。使用Java查找簡單的Active Directory信息
我剛開始使用Powershell在工作中更改AD組,查找AD信息等,但我缺乏我非常喜歡Java的GUI。
有沒有一個簡單的方法(或代碼示例),我輸入目標主機名,並返回與我要求的細節。 AD memberhsip團體,帳戶信息等?
我的Java知識不如我的Powershell那麼好,所以儘可能多地幫助我們。
感謝
這是我的第一篇文章,所以請溫和。使用Java查找簡單的Active Directory信息
我剛開始使用Powershell在工作中更改AD組,查找AD信息等,但我缺乏我非常喜歡Java的GUI。
有沒有一個簡單的方法(或代碼示例),我輸入目標主機名,並返回與我要求的細節。 AD memberhsip團體,帳戶信息等?
我的Java知識不如我的Powershell那麼好,所以儘可能多地幫助我們。
感謝
如果你正在尋找一個完整的Java GUI來查詢活動目錄,你可以看下到Apache Directory Studio。
如果你只想用java查詢AD,這裏是一個示例代碼:
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代碼必須添加標籤吧! – 2012-04-18 15:41:14
目前還不清楚你在問什麼。你想使用java執行AD查詢嗎? – zdan 2012-04-18 17:09:04
我認爲OP意味着他想爲他的PowerShell腳本使用JAVA GUI應用程序。 – Rekin 2012-04-18 17:11:50