Java訪問windows活動目錄+Active+Directory

來源:互聯網
上載者:User

 1.活動目錄(AD)

  Active Directory 是用於 Windows Server 的目錄服務。

  它儲存著網路上各種對象的有關資訊,並使該資訊易於管理員和使用者尋找及使用。

  Active Directory 目錄服務使用結構化的資料存放區作為目錄資訊的邏輯階層的基礎。

  通過登入驗證以及目錄中對象的存取控制,將安全性整合到 Active Directory 中。

  目錄服務,如 Active Directory,提供了用於儲存目錄資料並使該資料可由網路使用者和管理員使用的方法。

  例如,Active Directory 儲存了有關使用者帳戶的資訊,如名稱、密碼、電話號碼等,並允許相同網路上的其他已授權使用者訪問該資訊。

  2.LDAP

  LDAP是輕量目錄訪問協議,英文全稱是Lightweight Directory Access Protocol。

  LDAP是基於X.500標準的。

  LDAP 僅通過使用原始 X.500目錄存取協議 (DAP) 的功能子集而減少了所需的系統資源消耗。

  與X.500不同,LDAP支援TCP/IP,這對訪問Internet是必須的。

  LDAP和關聯式資料庫是兩種不同層次的概念,後者是存貯方式(同一層次如網格資料庫,對象資料庫),前者是存貯模式和訪問協議。

  LDAP是一個比關聯式資料庫抽象層次更高的存貯概念,與關聯式資料庫的查詢語言SQL屬同一層級。

  3.ADSI

  在Delphi中可以使用微軟的ADSI(活動目錄服務介面)來訪問活動目錄。

  ADSI是一組以COM介面的形式提供目錄服務的,是為基於目錄服務提供的通用介面。

  一些標準的ADSI提供者(Provider)有WinNT、IIS、LDAP和NDS。

  可以通過ADSI存取四種網路目錄結構:

  WinNT (Microsoft SAM 資料庫)、LDAP (輕量目錄存取協議)、NDS (NetWare目錄服務)和NWCOMPAT(Novell NetWare 3.x)。

  ADSI可以使Windows NT 管理員的工作變得輕鬆。

  ADSI支援管理員執行一些一般的管理工作,比如添加新使用者、管理印表機、安全設定和控制NT域。

  因為ADSI使用COM介面,任何支援COM的程式設計語言像Delphi、BCB、VB、VC等都可以調用ADSI。

  如在Delphi中調用ADSI,則需要引入活動目錄類型庫。

  操作如下:

  在IDE中,Project--->Import Type Library。

  選擇“Active Ds Type Library(Version 1.0)”,單擊“Create Unit”。

  Delphi會做相應的封裝,產生ActiveDs_TLB.pas檔案。

  Uses ActiveDs_TLB,就可以在Delphi程式中使用ADSI了

package ADOper;    import java.util.Hashtable; 
  import javax.naming.Context; 
  import javax.naming.ldap.LdapContext; 
  import javax.naming.ldap.InitialLdapContext; 
  import javax.naming.NamingEnumeration; 
  import javax.naming.directory.SearchControls; 
  import javax.naming.directory.SearchResult; 
  import javax.naming.NamingException; 
  import javax.naming.directory.Attribute; 
  import javax.naming.directory.Attributes; 
  import java.util.Enumeration; 
  public class ADOperTest { 
  public ADOperTest() { 
  } 
  public void GetADInfo() { 
  Hashtable HashEnv = new Hashtable(); 
  String LDAP_URL = "ldap://192.168.100.3:389"; //LDAP訪問地址 
  //String adminName = "CN=OAWebUser,CN=Users,DC=Hebmc,DC=com";//AD的使用者名稱 
  String adminName = "Hebmc\OAWebUser"; //注意使用者名稱的寫法:domainUser 或 User@domain.com 
  adminName = "OAWebUser@Hebmc.com"; //注意使用者名稱的寫法:domainUser 或 User@domain.com 
  String adminPassword = "chenzuooaup02"; //密碼 
  HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); //LDAP訪問安全層級 
  HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); //AD User 
  HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); //AD Password 
  HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); //LDAP工廠類 
  HashEnv.put(Context.PROVIDER_URL, LDAP_URL); 
  try { 
  LdapContext ctx = new InitialLdapContext(HashEnv, null); 
  SearchControls searchCtls = new SearchControls(); //Create the search controls 
  searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); //Specify the search scope 
  String searchFilter = "objectClass=User"; //specify the LDAP search filter 
  //String searchFilter = "objectClass=organizationalUnit";//specify the LDAP search filter 
  String searchBase = "DC=Hebmc,DC=com"; //Specify the Base for the search//搜尋域節點 
  int totalResults = 0; 
  //Specify the attributes to return 
  //String returnedAtts[] = {"memberOf"};//定製返回屬性 
  String returnedAtts[] = { 
  "url", "whenChanged", "employeeID", "name", "userPrincipalName", 
  "physicalDeliveryOfficeName", "departmentNumber", "telephoneNumber", 
  "homePhone", "mobile", "department", "sAMAccountName", "whenChanged", 
  "mail"}; //定製返回屬性 
  searchCtls.setReturningAttributes(returnedAtts); //設定返回屬性集 
  //Search for objects using the filter 
  NamingEnumeration answer = ctx.search(searchBase, searchFilter,searchCtls); 
  while (answer.hasMoreElements()) { 
  SearchResult sr = (SearchResult) answer.next(); 
  System.out.println("************************************************"); 
  System.out.println(sr.getName()); 
  Attributes Attrs = sr.getAttributes(); 
  if (Attrs != null) { 
  try { 
  for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore(); ) { 
  Attribute Attr = (Attribute) ne.next(); 
  System.out.println(" AttributeID=" + Attr.getID().toString()); 
  //讀取屬性值 
  for (NamingEnumeration e = Attr.getAll(); e.hasMore();totalResults++) { 
  System.out.println(" AttributeValues=" + e.next().toString()); 
  } 
  System.out.println(" ---------------"); 
  //讀取屬性值 
  Enumeration values = Attr.getAll(); 
  if (values != null) { // 迭代 
  while (values.hasMoreElements()) { 
  System.out.println(" AttributeValues=" + values.nextElement()); 
  } 
  } 
  System.out.println(" ---------------"); 
  } 

  }

catch (NamingException e) { 
  System.err.println("Throw Exception : " + e); 
  } 
  } 
  } 
  System.out.println("Number: " + totalResults); 
  ctx.close(); 
  } 
  catch (NamingException e) { 
  e.printStackTrace(); 
  System.err.println("Throw Exception : " + e); 
  } 
  } 
  public static void main(String args[]) { 
  ADOperTest ad = new ADOperTest(); 
  ad.GetADInfo(); 
  } 
  }

  備忘:

  使用LADP訪問AD,注意使用者名稱的寫法:domainUser 或 User@domain.com。

  如使用者名稱不正確,則可能會出現如下異常:

  javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 525, vece


串連遠端桌面 CMD

mstsc

dcprom

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.