zoukankan      html  css  js  c++  java
  • LDAP读取域控的信息

    直接看代码:

    package ldap.a;


    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 LdapReadInfo {
    public static void main(String args[]) {
    Hashtable HashEnv = new Hashtable();

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

    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  ,这里也可以设置为:objectClass=computer";

    String searchBase = "DC=jasgroup,DC=com"; // Specify the Base for
    // the search//搜索域节点
    int totalResults = 0;
    int totalAnswer = 0;

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

    searchCtls.setReturningAttributes(returnedAtts); // 设置返回属性集,这里如果设为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");
    }

    while (answer.hasMoreElements()) {
    SearchResult sr = (SearchResult) answer.next();
    System.out.println("************************************************");
    totalAnswer++;
    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(" AttrName="+ Attr.getID().toString());
    int i =0;
    // 读取属性值
    for (NamingEnumeration e = Attr.getAll(); e.hasMore(); totalResults++,i++) {
    System.out.println(" AttrValue["+i+"]="+ 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);
    System.out.println("totalAnswer:"+totalAnswer);
    ctx.close();
    }

    catch (NamingException e) {
    e.printStackTrace();
    System.err.println("Throw Exception : " + e);
    }
    }
    }

  • 相关阅读:
    MySQL 清理slowlog方法
    MySQL定位锁争用比较严重的表
    Jvm介绍
    MyEclipse6.5的SVN插件的安装
    BASE64图片转字符串
    JDK常用工具
    Ftp服务端安装-Linux环境
    数据结构之队列
    自定义Exception异常
    基于Lua语言的触动精灵脚本开发
  • 原文地址:https://www.cnblogs.com/qlong8807/p/2741563.html
Copyright © 2011-2022 走看看