AD域和LDAP协议

时间:2023-03-09 17:08:26
AD域和LDAP协议

随着我们的习大大上台后,国家在网络信息安全方面就有了很明显的改变!所以现在好多做网络信息安全产品的公司和需要网络信息安全的公司都会提到用AD域服务器来验证,这里就简单的研究了一下!

先简单的讲讲AD域和LdAP目录访问协议:AD(active directory)活动目录,动态的建立整个域模式网络中的对象的数据库或索引,协议为LDAP,安装了AD的服务器称为DC域控制器,存储整个域的对象的信息并周期性更新!其中的对象分为三大类——资源(如印表机)、服务(如电子邮件)、和人物(即帐户或用户,以及组)。

LDAP是一个用来发布目录信息到许多不同资源的协议。通常它都作为一个集中的地址被使用,不过根据组织者的需要,它可以做得更加强大。
LDAP其实是一个电话簿,类似于我们所使用诸如NIS(Network Information Service)、DNS (Domain Name Service)等网络目录,也类似于你在花园中所看到的树木。

不少LDAP开发人员喜欢把LDAP与关系数据库相比,认为是另一种的存贮方式,然后在读性能上进行比较。实际上,这种对比的基础是错误的。LDAP和关系数据库是两种不同层次的概念,后者是存贮方式(同一层次如网络数据库,对象数据库),前者是存贮模式和访问协议。LDAP是一个比关系数据库抽象层次更高的存贮概念,与关系数据库的查询语言SQL属同一级别。LDAP最基本的形式是一个连接数据库的标准方式。该数据库为读查询作了优化。因此它可以很快地得到查询结果,不过在其它方面,例如更新,就慢得多。AD域和LdAP目录访问协议就介绍到这里!下面来看看简单的demo吧!

  1. /**
  2. * @Description:
  3. *
  4. * @Title: LdAPTest.java
  5. * @Package com.joyce.itext.main
  6. * @Copyright: Copyright (c) 2014
  7. *
  8. * @author Comsys-LZP
  9. * @date 2014-8-7 上午10:20:22
  10. * @version V2.0
  11. */
  12. package com.joyce.itext.main;
  13. import java.util.Properties;
  14. import javax.naming.Context;
  15. import javax.naming.NamingEnumeration;
  16. import javax.naming.NamingException;
  17. import javax.naming.directory.SearchControls;
  18. import javax.naming.directory.SearchResult;
  19. import javax.naming.ldap.InitialLdapContext;
  20. import javax.naming.ldap.LdapContext;
  21. /**
  22. * @Description:拉取AD域账户
  23. *
  24. * @ClassName: LdAPTest
  25. * @Copyright: Copyright (c) 2014
  26. *
  27. * @author Comsys-LZP
  28. * @date 2014-8-7 上午10:20:22
  29. * @version V2.0
  30. */
  31. public class LdAPTest {
  32. public static void main(String[] args) {
  33. Properties env = new Properties();
  34. String adminName = "administrator@2003.com";//username@domain
  35. String adminPassword = "admin";//password
  36. String ldapURL = "LDAP://10.10.2.153:389";//ip:port
  37. env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
  38. env.put(Context.SECURITY_AUTHENTICATION, "simple");//"none","simple","strong"
  39. env.put(Context.SECURITY_PRINCIPAL, adminName);
  40. env.put(Context.SECURITY_CREDENTIALS, adminPassword);
  41. env.put(Context.PROVIDER_URL, ldapURL);
  42. try {
  43. LdapContext ctx = new InitialLdapContext(env, null);
  44. SearchControls searchCtls = new SearchControls();
  45. searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  46. String searchFilter = "(&(objectCategory=person)(objectClass=user)(name=*))";
  47. String searchBase = "DC=2003,DC=com";
  48. String returnedAtts[] = {"memberOf"};
  49. searchCtls.setReturningAttributes(returnedAtts);
  50. NamingEnumeration<SearchResult> answer = ctx.search(searchBase, searchFilter,searchCtls);
  51. while (answer.hasMoreElements()) {
  52. SearchResult sr = (SearchResult) answer.next();
  53. System.out.println("<<<::[" + sr.getName()+"]::>>>>");
  54. }
  55. ctx.close();
  56. }catch (NamingException e) {
  57. e.printStackTrace();
  58. System.err.println("Problem searching directory: " + e);
  59. }
  60. }
  61. }

以上就是从AD域上拉取域账号!当然现在Java还有许多其他的jar也能操作AD域,这里不就不多介绍了,有兴趣的同伴可以去百度、谷歌!最后给大伙儿看看效果图:

AD域和LDAP协议

还想多说一句,其实Java原生态的API老好了,所以能用原生态的最好,因为别人提供的jar包如果没有处理好的话,还不如原生态的呢!感谢大家的关注