java获取AD域用户信息

http://aa00aa00.iteye.com/blog/1276936

http://blog.sina.com.cn/s/blog_6ef2c4540100nuvq.html

package com.webservice.message;

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

public ADOperTest() {

}

public String GetADInfo(String name ) {

  String userName = name; // 用户名称

  if(userName==null){

userName = "";

  }

  String company = "";

  String host = "10.10.10.21"; // AD服务器

  String port = "389"; // 端口

  String url = new String("ldap://" + host + ":" + port);

  Hashtable HashEnv = new Hashtable();

  // String adminName ="CN=oyxiaoyuanxy,CN=Users,DC=Hebmc,DC=com";//AD的用户名

  String adminName = "cq\\administrator"; // 注意用户名的写法:domain\User

  String adminPassword = "prd@"; // 密码

  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, url);

  try {

   LdapContext ctx = new InitialLdapContext(HashEnv, null);

   // 域节点

   String searchBase = "OU=重庆烟草,DC=cq,DC=tobacco,DC=com,DC=cn";

   // LDAP搜索过滤器类

   String searchFilter = "objectClass=User";

   // 搜索控制器

   SearchControls searchCtls = new SearchControls(); // Create the

   // search

   // controls

   // 创建搜索控制器

   searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify

   // the

   // search

   // scope

   // 设置搜索范围

   // searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE); //

   // Specify the search scope 设置搜索范围

//   String returnedAtts[] = { "memberOf", "distinguishedName",

//     "Pwd-Last-Set", "User-Password", "cn" };// 定制返回属性

   String returnedAtts[] = { "company" };// 定制返回属性

  

   // String returnedAtts[] = { "url", "whenChanged", "employeeID",

   // "name", "userPrincipalName", "physicalDeliveryOfficeName",

   // "departmentNumber", "telephoneNumber", "homePhone",

   // "mobile", "department", "sAMAccountName", "whenChanged",

   // "mail" }; // 定制返回属性

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

   // 根据设置的域节点、过滤器类和搜索控制器搜索LDAP得到结果

   NamingEnumeration answer = ctx.search(searchBase, searchFilter,

     searchCtls);// Search for objects using the filter

   // 初始化搜索结果数为0

   int totalResults = 0;// Specify the attributes to return

   int rows = 0;

   while (answer.hasMoreElements()) {// 遍历结果集

    SearchResult sr = (SearchResult) answer.next();// 得到符合搜索条件的DN

    System.out.println(++rows

      + "************************************************");

   

    String dn = sr.getName();

    System.out.println(dn);

    String match = dn.split("CN=")[1].split(",")[0];//返回格式一般是CN=ptyh,OU=专卖

    System.out.println(match);

    if(userName.equals(match)){

    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++) {

      company =  e.next().toString();

        System.out.println("    AttributeValues=属性值:" + company);

       }

       System.out.println("    ---------------");

      

      }

     } catch (NamingException e) {

      System.err.println("Throw Exception : " + e);

     }

    }//if

    }

   }//while

   System.out.println("************************************************");

   System.out.println("Number: " + totalResults);

   ctx.close();

  } catch (NamingException e) {

   e.printStackTrace();

   System.err.println("Throw Exception : " + e);

  }

  return company;

}

public static void main(String args[]) {

  // 实例化

  ADOperTest ad = new ADOperTest();

  ad.GetADInfo("shz");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要通过Java使用LDAP获取AD用户和组织信息,需要使用Java的JNDI API。 以下是一个简单的Java程序,演示如何使用JNDI API连接到AD获取用户和组织信息: ``` import java.util.*; import javax.naming.*; import javax.naming.directory.*; public class ADInfo { public static void main(String[] args) { String ldapURL = "ldap://AD服务器地址:389"; String ldapUser = "CN=LDAP查询用户,OU=xxx,DC=xxx,DC=xxx"; String ldapPassword = "LDAP查询用户密码"; String searchBase = "OU=xxx,DC=xxx,DC=xxx"; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, ldapUser); env.put(Context.SECURITY_CREDENTIALS, ldapPassword); try { DirContext ctx = new InitialDirContext(env); SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(objectCategory=user)"; NamingEnumeration<SearchResult> results = ctx.search(searchBase, filter, searchControls); while (results.hasMore()) { SearchResult searchResult = results.next(); Attributes attributes = searchResult.getAttributes(); Attribute attribute = attributes.get("cn"); String cn = (String) attribute.get(); System.out.println(cn); } filter = "(objectCategory=organizationalUnit)"; results = ctx.search(searchBase, filter, searchControls); while (results.hasMore()) { SearchResult searchResult = results.next(); Attributes attributes = searchResult.getAttributes(); Attribute attribute = attributes.get("ou"); String ou = (String) attribute.get(); System.out.println(ou); } ctx.close(); } catch (NamingException e) { e.printStackTrace(); } } } ``` 在上面的代码中,替换以下变量: - ldapURL:AD服务器地址和端口号 - ldapUser:用于查询AD的LDAP用户的DN - ldapPassword:用于查询AD的LDAP用户的密码 - searchBase:要搜索的AD的基本DN 该程序连接到AD并搜索用户和组织。它使用过滤器来限制搜索结果,只搜索用户和组织单位对象。它还使用SearchControls对象来设置搜索范围。 对于每个搜索结果,程序从属性中提取cn或ou,并将其打印到控制台上。 请注意,此代码需要在Java应用程序中包含JNDI API类路径。如果您使用Maven或Gradle之类的构建工具,则可以将以下依赖项添加到项目中: ``` <dependency> <groupId>com.sun.jndi</groupId> <artifactId>ldap</artifactId> <version>1.2.1</version> </dependency> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值