• java读取AD域信息


    package com.eh.ad;

    /**
     *  JAVA 读取AD用户信息
     *  aa00a00
     */

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Hashtable;
    import java.util.List;


    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.Control;
    import javax.naming.ldap.InitialLdapContext;
    import javax.naming.ldap.LdapContext;
    import javax.naming.ldap.PagedResultsControl;
    import javax.naming.ldap.PagedResultsResponseControl;

    public class EhrADOperTest {


     public List<EhrPo> GetADInfo(String userName, String passwd, String host,
       String port, String domain) throws IOException {
      List<EhrPo> ehrList = new ArrayList();// 返回的list

      String url = new String("ldap://" + host + ":" + port);
      String user = userName.indexOf(domain) > 0 ? userName : userName
        + domain;
      Hashtable HashEnv = new Hashtable();

      HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
      HashEnv.put(Context.SECURITY_PRINCIPAL, userName); // AD User
      HashEnv.put(Context.SECURITY_CREDENTIALS, passwd); // AD// Password
      HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,
        "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
      HashEnv.put(Context.PROVIDER_URL, url);
      HashEnv.put(Context.BATCHSIZE, "4100");
      
      try {
       LdapContext ctx = new InitialLdapContext(HashEnv, null);

       ctx.setRequestControls(new Control[] { new PagedResultsControl(
         5000, Control.CRITICAL) });

       // 域节点
       String searchBase = "DC=genomics,DC=cn";
       // LDAP搜索过滤器类
       String searchFilter = "(&(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
       // String searchFilter = "objectClass=User";
       // 搜索控制器
       SearchControls searchCtls = new SearchControls(); // Create the
       searchCtls.setCountLimit(4110);
       // search
       // controls
       // 创建搜索控制器
       searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify

       System.out.println(searchCtls.getCountLimit());
      
       // 设置搜索范围
       // searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE); //
       
       String returnedAtts[] = { "Pwd-Last-Set", "User-Password", "mail",
         "description" };// 定制返回属性

       searchCtls.setReturningAttributes(returnedAtts); // 设置返回属性集

       ehrList = ldapDatalistPage(ctx, searchBase,searchFilter);  
       
       ctx.close();
      } catch (NamingException e) {
       e.printStackTrace();
       System.err.println("Throw Exception : " + e);
      }

      return ehrList;
     }

     public List ldapDatalistPage(LdapContext context, String base,
       String objectClass) {
      
      List resultlist = new ArrayList();
      int pageSize = 5000; // 1000 entries per page
      byte[] cookie = null;
      int total;
      
      try {
       context.setRequestControls(new Control[] { new PagedResultsControl(
         pageSize, Control.CRITICAL) });// 分页读取控制
       do {// 循环检索数据
        
        // Perform the search
        SearchControls searchCtls = new SearchControls(); // Create the

        // 创建搜索控制器
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify
        
        String returnedAtts[] = { "Pwd-Last-Set", "User-Password", "mail",
          "description" };// 定制返回属性

        searchCtls.setReturningAttributes(returnedAtts); // 设置返回属性集

        NamingEnumeration results = context.search(base,objectClass, searchCtls); // 查询所有信息
        
        while (results != null && results.hasMoreElements()) {// 遍历结果集
         SearchResult sr = (SearchResult) results.next();// 得到符合搜索条件的DN

         EhrPo ehr = new EhrPo(); // 接收属性值的对象;

         int count=0;
         
         Attributes Attrs = sr.getAttributes();// 得到符合条件的属性集
         if (Attrs != null) {
          try {
           for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore();) {
            
            Attribute Attr = (Attribute) ne.next();// 得到下一个属性

            // 读取属性值
            for (NamingEnumeration e = Attr.getAll(); e
              .hasMore(); count++) {

             if ("mail".equals(Attr.getID().toString())&&Attr.getID()!=null) {
              ehr.setUseremail(e.next().toString());
             }
             
             if ("description".equals(Attr.getID().toString())&&Attr.getID()!=null) {          
              ehr.setUsercode(e.next().toString().toUpperCase());
             }
            }
           }
          } catch (NamingException e) {
           e.printStackTrace();
          }
         }

         if (ehr.getUsercode() != null && ehr.getUseremail() != null) {
          resultlist.add(ehr);
         }
        }

        // Examine the paged results control response
        Control[] controls = context.getResponseControls();
        if (controls != null) {
         for (int i = 0; i < controls.length; i++) {
          if (controls[i] instanceof PagedResultsResponseControl) {
           PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
           total = prrc.getResultSize();
           cookie = prrc.getCookie();
          }
         }
        }
        // Re-activate paged results
        context.setRequestControls(new Control[] { new PagedResultsControl(
          pageSize, cookie, Control.CRITICAL) });
       } while (cookie != null);
       
      } catch (NamingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      System.out.println("总共:" + resultlist.size() + "条信息.");
      return resultlist;
     }

     public static void main(String[] args) throws IOException {
      EhrADOperTest test = new EhrADOperTest();

      // String userName = "P_ehr"; // 用户名称
      // String passwd = "@HNT78901";
      // String host = "192.168.16.1"; // AD服务器
      // String port = "389"; // 端口
      // String domain = "@genomics.cn"; // 邮箱的后缀名

      List<EhrPo> list = test.GetADInfo("P_ehr", "@HNT78901", "192.168.16.1",
        "389", "@genomics.cn");

      for (EhrPo ehr : list) {
       System.out.println(ehr.getUsercode() + "   " + ehr.getUseremail());
      }
      System.out.println(list.size());
     }
    }

  • 相关阅读:
    我的“.vimrc”配置
    js写的简单购物车2
    js写的简单购物车
    用css3绘制你需要的几何图形
    给父级DIV清除浮动
    HTML中canvas的大小调整
    Python
    Python文本编辑器推荐
    jQuery mobile基础
    Bootstrap网格系统
  • 原文地址:https://www.cnblogs.com/javawebstudy/p/AD.html
Copyright © 2020-2023  润新知