LDAP方式连接AD获取用户信息

来源:互联网 发布:浙江儿童dna数据库利弊 编辑:程序博客网 时间:2024/06/13 21:34

LDAP资料介绍可以参考:http://wenku.baidu.com/view/262742f9f705cc17552709f9.html

ldap访问AD域的的错误一般会如下格式:

Ldap load error: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 525, vece]

其中红字部分的意思如下(这些错误码跟语言无关):

525 - 用户没有找到

52e - 证书不正确

530 - not permitted to logon at this time

532 - 密码期满

533 - 帐户不可用

701 - 账户期满

773 - 用户必须重设密码

Java代码  收藏代码
  1. import java.util.Hashtable;  
  2. import javax.naming.Context;  
  3. import javax.naming.NamingEnumeration;  
  4. import javax.naming.NamingException;  
  5. import javax.naming.directory.Attribute;  
  6. import javax.naming.directory.Attributes;  
  7. import javax.naming.directory.SearchControls;  
  8. import javax.naming.directory.SearchResult;  
  9. import javax.naming.ldap.InitialLdapContext;  
  10. import javax.naming.ldap.LdapContext;  
  11.   
  12. public class LdapADHelper {  
  13.     public LdapADHelper() {  
  14.     }  
  15.     private String host,url,adminName,adminPassword;  
  16.     private LdapContext ctx = null;  
  17.     /** 
  18.      * 初始化ldap 
  19.      */  
  20.     public void initLdap(){  
  21.         //ad服务器  
  22.         this.host = "xxx.com"// AD服务器  
  23.         this.url = new String("ldap://" + host );//默认端口为80的可以不用填写,其他端口需要填写,如ldap://xxx.com:8080  
  24.         this.adminName = "admin@xxx.com";// 注意用户名的写法:domain\User 或  User@domain.com  
  25.         this.adminPassword = "admin";             
  26.         Hashtable HashEnv = new Hashtable();  
  27.         HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别  
  28.         HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); // AD User  
  29.         HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); // AD Password  
  30.         HashEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类  
  31.         HashEnv.put(Context.PROVIDER_URL, url);  
  32.         try {  
  33.              ctx = new InitialLdapContext(HashEnv, null);  
  34.              System.out.println("初始化ldap成功!");  
  35.         } catch (NamingException e) {  
  36.             e.printStackTrace();  
  37.             System.err.println("Throw Exception : " + e);  
  38.         }  
  39.     }  
  40.     /** 
  41.      * 关闭ldap 
  42.      */  
  43.     public void closeLdap(){  
  44.         try {  
  45.             this.ctx.close();  
  46.         } catch (NamingException e) {  
  47.             // TODO Auto-generated catch block  
  48.             e.printStackTrace();  
  49.         }  
  50.     }  
  51.     /** 
  52.      *  
  53.      * @param type organizationalUnit:组织架构 group:用户组 user|person:用户 
  54.      * @param name 
  55.      * @return 
  56.      */  
  57.     public String GetADInfo(String type ,String filter ,String name) {  
  58.   
  59.         String userName = name; // 用户名称  
  60.         if (userName == null) {  
  61.             userName = "";  
  62.         }  
  63.         String company = "";  
  64.         String result = "";  
  65.         try {  
  66.             // 域节点  
  67.             String searchBase = "DC=xx,DC=xxx,DC=com";  
  68.             // LDAP搜索过滤器类  
  69.             //cn=*name*模糊查询 cn=name 精确查询  
  70. //          String searchFilter = "(objectClass="+type+")";  
  71.             String searchFilter = "(&(objectClass="+type+")("+filter+"=*" + name + "*))";  
  72.             // 创建搜索控制器  
  73.             SearchControls searchCtls = new SearchControls();   
  74.             //  设置搜索范围  
  75.             searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);   
  76. //          String returnedAtts[] = {  "memberOf" }; // 定制返回属性  
  77. //      searchCtls.setReturningAttributes(returnedAtts); // 设置返回属性集 不设置则返回所有属性  
  78.             // 根据设置的域节点、过滤器类和搜索控制器搜索LDAP得到结果  
  79.             NamingEnumeration answer = ctx.search(searchBase, searchFilter,searchCtls);// Search for objects using the filter  
  80.             // 初始化搜索结果数为0  
  81.             int totalResults = 0;// Specify the attributes to return  
  82.             int rows = 0;  
  83.             while (answer.hasMoreElements()) {// 遍历结果集  
  84.                 SearchResult sr = (SearchResult) answer.next();// 得到符合搜索条件的DN  
  85.                 ++rows;  
  86.                 String dn = sr.getName();  
  87.                 System.out.println(dn);  
  88.                 Attributes Attrs = sr.getAttributes();// 得到符合条件的属性集  
  89.                 if (Attrs != null) {  
  90.                     try {  
  91.                         for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore();) {  
  92.                             Attribute Attr = (Attribute) ne.next();// 得到下一个属性  
  93.                             System.out.println(" AttributeID=属性名:"+ Attr.getID().toString());  
  94.                             // 读取属性值  
  95.                             for (NamingEnumeration e = Attr.getAll(); e.hasMore(); totalResults++) {  
  96.                                 company = e.next().toString();  
  97.                                 System.out.println("    AttributeValues=属性值:"+ company);  
  98.                             }  
  99.                             System.out.println("    ---------------");  
  100.   
  101.                         }  
  102.                     } catch (NamingException e) {  
  103.                         System.err.println("Throw Exception : " + e);  
  104.                     }  
  105.                 }// if  
  106.             }// while  
  107.             System.out.println("************************************************");  
  108.             System.out.println("Number: " + totalResults);  
  109.             System.out.println("总共用户数:"+rows);  
  110.         } catch (NamingException e) {  
  111.             e.printStackTrace();  
  112.             System.err.println("Throw Exception : " + e);  
  113.         }  
  114.         return result;  
  115.     }  
  116.   
  117.     public static void main(String args[]) {  
  118.         // 实例化  
  119.         LdapADHelper ad = new LdapADHelper();  
  120.         ad.initLdap();  
  121.     ad.GetADInfo("user","cn","李XX");//查找用户  
  122.         ad.GetADInfo("organizationalUnit","ou","工程");//查找组织架构  
  123.     ad.GetADInfo("group","cn","福建xxx");//查找用户组  
  124.           
  125.         ad.closeLdap();  
  126.     }  
  127. }  

原创粉丝点击