zoukankan      html  css  js  c++  java
  • java获取域账号下的用户完整版

    package encode;

    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 LDAPGetDomain {

     public static String getFormatOU(String ou) {
      String[] splt = ou.split(",");
      String realFormat = "";
      for (int i = splt.length - 1; i >= 0; i--) {
       realFormat = realFormat + "OU=" + splt[i] + ",";
      }
      if (',' == realFormat.charAt(realFormat.length() - 1)) {
       realFormat = realFormat.substring(0, realFormat.length() - 1);
      }
      return realFormat;
     }

     public static String getFormatDoamin(String domainName) {
      String[] splt = domainName.split("//.");
      String realFormat = "";
      for (int i = 0; i < splt.length; i++) {
       if (!"".equals(splt[i]))
        realFormat += "DC=" + splt[i] + ",";
      }
      if (',' == realFormat.charAt(realFormat.length() - 1)) {
       realFormat = realFormat.substring(0, realFormat.length() - 1);
      }
      return realFormat;
     }

     public static String GetRemoteDomainUser(LdapContext ctx, String ou,
       String domainName) throws NamingException {
      String xml = "";
      SearchControls searchCtls = new SearchControls();
      searchCtls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
      String searchFilter = "objectClass=User";
      String searchBase = "";
      searchBase = ou + "," + getFormatDoamin(domainName);
      String returnedAtts[] = { "name", "telephoneNumber", "mobile", "mail" };
      searchCtls.setReturningAttributes(returnedAtts);

      NamingEnumeration answer = ctx.search(searchBase, searchFilter,
        searchCtls);

      while (answer.hasMoreElements()) {
       SearchResult sr = (SearchResult) answer.next();
       int oulenth = 0;
       Attributes Attrs = sr.getAttributes();
       if (Attrs != null) {
        try {
         xml += "<User ";
         for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore();) {
          Attribute Attr = (Attribute) ne.next();

          if ("name".equals(Attr.getID())) {
           xml += "name=";
          }
          if ("telephoneNumber".equals(Attr.getID())) {
           xml += "tel=";
          }
          if ("mobile".equals(Attr.getID())) {
           xml += "mobile=";
          }
          if ("mail".equals(Attr.getID())) {
           xml += "email=";
          }
          Enumeration values = Attr.getAll();
          if (values != null) {
           while (values.hasMoreElements()) {
            xml += "/"" + values.nextElement() + "/" ";
            oulenth = oulenth + 1;
           }
          }
         }
         xml += "/>";
        } catch (NamingException e) {
         System.err.println("Throw Exception : " + e);
        }
       }
      }
      return xml;
     }

     public String GetRemoteDomainGroupDie(LdapContext ctx, String ou,
       String domainName) throws NamingException{
      String xml="";
      SearchControls searchCtls = new SearchControls();
      searchCtls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
      String searchFilter = "objectClass=organizationalUnit";
      String searchBase = "";
      searchBase =ou + "," + getFormatDoamin(domainName);
      String returnedAtts[] = { "name" };

      searchCtls.setReturningAttributes(returnedAtts);

      NamingEnumeration answer = ctx.search(searchBase, searchFilter,
        searchCtls);
      while (answer.hasMoreElements()) {
       SearchResult sr = (SearchResult) answer.next();
       String ouName = sr.getName();
       //System.out.println(ouName);
       if (ouName != null && !"".equals(ouName)) {
        
        Attributes Attrs = sr.getAttributes();
        if (Attrs != null) {
         try {
          for (NamingEnumeration ne = Attrs.getAll(); ne
            .hasMore();) {
           Attribute Attr = (Attribute) ne.next();
           if ("name".equals(Attr.getID())) {
            Enumeration values = Attr.getAll();
            if (values != null) { // 迭代
             while (values.hasMoreElements()) {
              String v=(String)values.nextElement();
              xml+="<Group name=/""+v+"/">";
              xml += GetRemoteDomainUser(ctx, "OU="+v+","+ou, domainName);
              xml+=GetRemoteDomainGroupDie(ctx,"OU="+v+","+ou,domainName);
              xml+="</Group>";
             }
            }

           }

          }
          

         } catch (NamingException e) {
          e.printStackTrace();
         }
        }
       }else{
        xml += GetRemoteDomainUser(ctx,
          getFormatOU(ou), domainName);
       }
       
      }
      return xml;
     }
     public String GetRemoteDomainGroup(String ip, String port,
       String adminName, String adminPassword, String domainName, String ou) {
      String xml = "<?xml version=/"1.0/" encoding=/"gbk/" ?>";
      Hashtable<String, String> HashEnv = new Hashtable<String, String>();
      String rport = port;
      if (port == null || "".equals(port))
       rport = "389";
      String LDAP_URL = "ldap://" + ip + ":" + rport;
      adminName = adminName + "@" + domainName;
      HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
      HashEnv.put(Context.SECURITY_PRINCIPAL, adminName);
      HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword);
      // Password
      HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,
        "com.sun.jndi.ldap.LdapCtxFactory");
      HashEnv.put(Context.PROVIDER_URL, LDAP_URL);

      try {
       LdapContext ctx = new InitialLdapContext(HashEnv, null);
       SearchControls searchCtls = new SearchControls();
       searchCtls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
       String searchFilter = "objectClass=organizationalUnit";
       String searchBase = "";
       searchBase = getFormatOU(ou) + "," + getFormatDoamin(domainName);
       String returnedAtts[] = { "name" };

       searchCtls.setReturningAttributes(returnedAtts);

       NamingEnumeration answer = ctx.search(searchBase, searchFilter,
         searchCtls);
       int oulenth = 0;
       String[] splt = ou.split(",");
       for (int j = 0; j < splt.length; j++) {
        if (!"".equals(splt[j])) {
         xml = xml + "<Group name=/"" + splt[j] + "/">";
         oulenth = oulenth + 1;
        }
       }
       xml += GetRemoteDomainUser(ctx, getFormatOU(ou), domainName);
       xml+=GetRemoteDomainGroupDie(ctx, getFormatOU(ou), domainName);
       for (int i = 0; i < oulenth; i++) {
        xml += "</Group>";
       }
       ctx.close();
      }

      catch (NamingException e) {
       e.printStackTrace();
      }
      return xml;
     }
     public static void main(String args[]) {
      LDAPGetDomain ad = new LDAPGetDomain();
      System.out.println(ad.GetRemoteDomainGroup("192.168.2.44", "389",
        "administrator", "qqqqqqqq1!", "Test.bomb", "hongan,rtrt"));
     }
    }

    输出结果:

    <?xml version="1.0" encoding="gbk" ?><Group name="hongan"><Group name="rtrt"><User name="small" /><Group name="ggd"><Group name="343"><Group name="6677"></Group></Group></Group><Group name="gggg"><Group name="111"><Group name="ggg"></Group><Group name="iiii"></Group></Group><Group name="222"><Group name="lllk"></Group></Group></Group></Group></Group>

  • 相关阅读:
    tomcat server.xml 配置示例
    Vue学习1:实例及生命周期
    flex布局
    从输入一个URL到页面完全显示发生了什么?
    webstorm配置eslint【标记错误,修复错误】
    JavaScript实现八大内部排序算法
    es6(六):module模块(export,import)
    es6(五):class关键字(extends,super,static)
    es6(四):Symbol,Set,Map
    es6(三):es6中函数的扩展(参数默认值、rest参数、箭头函数)
  • 原文地址:https://www.cnblogs.com/liaomin416100569/p/9331524.html
Copyright © 2011-2022 走看看