码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA用LDAP读取微软AD域里面的用户名组织架构等信息

时间:2021-03-08 13:05:28      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ad域   search   eva   one   number   return   cto   detail   class   

package com.app.frame.ldap;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class LdapTest {
 public static void main(String args[]) {
  Hashtable HashEnv = new Hashtable();

  String LDAP_URL = "ldap://192.168.107.2: 389"; // LDAP访问地址
  String adminName = "Administrator@SNGCC.com"; // 注意用户名的写法:domain\User 或
              // User@domain.com
  String adminPassword = "maindc"; // 密码

  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);
   
   // Create the search controls
   SearchControls searchCtls = new SearchControls();
   
   // 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=SNGCC,DC=com";
             
   int totalResults = 0;

   String returnedAtts[] = { "url", "whenChanged", "employeeID",
     "name", "userPrincipalName", "physicalDeliveryOfficeName",
     "departmentNumber", "telephoneNumber", "homePhone",
     "mobile", "department", "sAMAccountName", "whenChanged",
     "mail" }; // 定制返回属性   

   searchCtls.setReturningAttributes(returnedAtts); // 设置返回属性集
   
//   searchCtls.setReturningAttributes(null); // 不定制属性,将返回所有的属性集

   // Search for objects using the filter
   NamingEnumeration answer = ctx.search(searchBase, searchFilter,
     searchCtls);

   if (answer == null || answer.equals(null)) {
    System.out.println("answer is null");
   } else {
    System.out.println("answer not null");
   }

   System.out.println(answer.hasMoreElements());

   while (answer.hasMoreElements()) {
    SearchResult sr = (SearchResult) answer.next();
    System.out
      .println("************************************************");
    System.out.println("getname="+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);
  }
 }
}
————————————————
版权声明:本文为CSDN博主「guchuanlong」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/guchuanlong/article/details/7634103

JAVA用LDAP读取微软AD域里面的用户名组织架构等信息

标签:ad域   search   eva   one   number   return   cto   detail   class   

原文地址:https://www.cnblogs.com/LilLazy/p/14490441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!