LDAP操作代码样例  初始化LDAP 目录服务上下文 
该例子中,我们使用uid=linly,ou=People,dc=jsoso,dc=net这个账号,链接位于本机8389端口的LDAP服务器(ldap://localhost:8389),认证方式采用simple类型,即用户名/密码方式。

private static void initialContext() throws NamingException{ 
   if(singleton == null){ 
    singleton = new LDAPConnection(); 
    /* 
    * 在实际编码中,这些环境变量应尽可能通过配置文件读取 
    */ 
    //LDAP服务地址 
    singleton.sLDAP_URL = "ldap://localhost:8389"; 
    //管理员账号 
    singleton.sMANAGER_DN = "uid=linly,ou=People,dc=jsoso,dc=net"; 
    //管理员密码 
    singleton.sMANAGER_PASSWORD = "coffee"; 
    //认证类型 
    singleton.sAUTH_TYPE = "simple"; 
    //JNDI Context工厂类 
    singleton.sCONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory"; 
   
    singleton.envProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, singleton.sCONTEXT_FACTORY); 
    singleton.envProps.setProperty(Context.PROVIDER_URL, singleton.sLDAP_URL); 
    singleton.envProps.setProperty(Context.SECURITY_AUTHENTICATION, singleton.sAUTH_TYPE); 
    singleton.envProps.setProperty(Context.SECURITY_PRINCIPAL, singleton.sMANAGER_DN); 
    singleton.envProps.setProperty(Context.SECURITY_CREDENTIALS, singleton.sMANAGER_PASSWORD); 
    /* 
    * 绑定ldap服务器 
    */ 
    singleton.dirCtx = new InitialDirContext(singleton.envProps); 
   } 
}

通过一个Hashtable或者Properties对象为LDAP的Context设置参数,而后初始化InitialDirContext,即可绑定LDAP服务。这相当于JDBC中获取数据库的Connection对象。

绑定/创建LDAP条目对象
用户可以使用bind方法创建新的LDAP条目,下面的代码创建一个DN:"ou=Employee , dc=jsoso ,dc=net"的OrganizationUnit类LDAP条目如下:

public boolean createOrganizationUnit(){ 
   String ldapGroupDN = "ou=Employee , dc=jsoso ,dc=net"; 
   try { 
    /* 
    * 查找是否已经存在指定的OU条目 
    * 如果存在,则打印OU条目的属性信息 
    * 如果不存在,则程序会抛出NamingException异常,进入异常处理 
    */ 
    Attributes attrs = dirContext.getAttributes(ldapGroupDN); 
    System.out.println("Find the group , attributes list :"); 
    NamingEnumeration<String> nEnum = attrs.getIDs();   
    for( ; nEnum.hasMore() ; ){ 
     String attrID = nEnum.next(); 
     Attribute attr = (Attribute)attrs.get(attrID); 
     System.out.println(attr.toString()); 
    }   
    return false; 
   } catch (NamingException e) { 
    /* 
    * 没有找到对应的Group条目,新增Group条目 
    */ 
    //创建objectclass属性 
    Attribute objclass = new BasicAttribute("objectclass"); 
    objclass.add("top"); 
    objclass.add("organizationalunit"); 
    //创建cn属性 
    Attribute cn = new BasicAttribute("ou", "Employee"); 
    //创建Attributes,并添加objectclass和cn属性 
    Attributes attrs = new BasicAttributes(); 
    attrs.put(objclass); 
    attrs.put(cn); 
    //将属性绑定到新的条目上,创建该条目 
    try { 
     dirContext.bind(ldapGroupDN, null, attrs); 
     System.out.println("Group created successful"); 
     return true; 
    } catch (NamingException e1) { 
     e1.printStackTrace(); 
    }    
   } 
   return false; 
}

获取条目属性 
下面一段代码获取entryDN参数指定条目中的属性集合,并打印到控制台

/** 
* 获取一个指定的LDAP Entry 
* @param entryDN 
*/ 
public void find(String entryDN){ 
   try { 
    Attributes attrs = dirContext.getAttributes(entryDN); 
    if (attrs != null) { 
     NamingEnumeration<String> nEnum = attrs.getIDs(); 
     for( ; nEnum.hasMore() ; ){ 
      String attrID = nEnum.next(); 
      Attribute attr = (Attribute)attrs.get(attrID); 
      System.out.println(attr.toString()); 
     } 
     System.out.println(); 
    }else{ 
     System.out.println("No found binding."); 
    } 
   }catch(NamingException ne) { 
    ne.printStackTrace(); 
   } 
}

修改条目属性 
修改DN=user.getDistinguishedName()的条目中的cn、givenname、sn和userpassword四个属性值。 
(注:参数DirContext.REPLACE_ATTRIBUTE有另外两个常量:DirContext.ADD_ATTRIBUTE;DirContext.REMOVE_ATTRIBUTE,分别表示新增属性和删除属性。)

/** 
* 修改用户信息 
* @param user 
* @return 
* @throws Exception 
*/ 
public boolean modifyUser(LDAPUser user) throws Exception { 
   //用户对象为空 
   if (user == null) { 
    throw new Exception("No user information!n"); 
   }

//检查uid 
   String userDN = user.getDistinguishedName(); 
   if (userDN == null && userDN.length() == 0) { 
    throw new NamingException("No userDN you specify!n"); 
   }

//判断用户条目是否已经存在 
   if(!isUserexist(userDN)){ 
    return false; 
   } 
  
   //设置属性 
   Attributes attrs = new BasicAttributes(); 
   setBasicAttribute(attrs, "cn", user.getCommomName()); 
   setBasicAttribute(attrs, "givenname", user.getFirstName()); 
   setBasicAttribute(attrs, "sn", user.getLastName()); 
   setBasicAttribute(attrs, "userpassword", user.getPassword()); 
   //修改属性 
   try{ 
    dirContext.modifyAttributes(user.getDistinguishedName(),DirContext.REPLACE_ATTRIBUTE, attrs); 
    System.out.println("User(" + user.getDistinguishedName() + ") information modified.n"); 
    return true; 
   }catch(NamingException ne){ 
    ne.printStackTrace(); 
   } 
   return false; 
}

根据属性集搜索条目 
根据属性集matchingAttributes中的匹配值,在上下文DN= "ou=People,dc=jsoso ,dc=net"中搜索它的所有子树中的匹配条目。 
(注:SearchControls的SCOPE参数详见SearchControls SCOPE补充说明)

/** 
* 通过属性搜索LDAP范例 
* @return 
*/ 
public void searchByAttribute(Attributes matchingAttributes){ 
   String baseDN = "ou=People,dc=jsoso ,dc=net"; 
   SearchControls cons = new SearchControls(); 
   cons.setSearchScope(SearchControls.SUBTREE_SCOPE); 
   try { 
    Name baseName = new LdapName(baseDN); 
    NamingEnumeration<SearchResult> ne = dirContext.search(baseName, matchingAttributes); 
    SearchResult entry = null; 
    for(;ne.hasMore();){ 
     entry = ne.next(); 
     showEntry(entry); 
    }     
   } catch (NamingException e) { 
    e.printStackTrace(); 
   } 
}

根据过滤器搜索条目 
根据过滤器条件,在上下文DN = "ou=People,dc=jsoso ,dc=net"中,搜索它的所有子树中的匹配条目。 
(注:过滤器filter的相关语法详见LDAP filter语法补充说明)

/** 
* 通过过滤器搜索LDAP范例 
* @return 
*/ 
public void searchByFilter(String filter){ 
   String baseDN = "ou=People,dc=jsoso ,dc=net";   
   SearchControls cons = new SearchControls(); 
   cons.setSearchScope(SearchControls.SUBTREE_SCOPE); 
   try { 
    NamingEnumeration<SearchResult> ne = dirContext.search(baseDN, filter , cons); 
    SearchResult entry = null; 
    for(;ne.hasMore();){ 
     entry = ne.next(); 
     showEntry(entry); 
    }     
   } catch (NamingException e) { 
    e.printStackTrace(); 
   }

这里的内容是抄录别人的,自己写的没有别人写的这份全。这里的增加用户,增加组织单元,查找用户都经过了我的验证,没有问题。但是修改我没有验证通过。

删除没有做,但是从API上看,是没有问题的。 详细内容可以去百度文库搜:LDAP实用资料收录3.doc 。

该例子中,我们使用uid=linly,ou=People,dc=jsoso,dc=net这个账号,链接位于本机8389端口的LDAP服务器(ldap://localhost:8389),认证方式采用simple类型,即用户名/密码方式。

Java代码

1.   private static void initialContext() throws NamingException{

2.       if(singleton == null){

3.           singleton = new LDAPConnection();

4.           /*

5.            * 在实际编码中,这些环境变量应尽可能通过配置文件读取

6.            */

7.           //LDAP服务地址

8.           singleton.sLDAP_URL = "ldap://localhost:8389";

9.           //管理员账号

10.        singleton.sMANAGER_DN = "uid=linly,ou=People,dc=jsoso,dc=net";

11.        //管理员密码

12.        singleton.sMANAGER_PASSWORD = "coffee";

13.        //认证类型

14.        singleton.sAUTH_TYPE = "simple";

15.        //JNDI Context工厂类

16.        singleton.sCONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";

17.

18.        singleton.envProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, singleton.sCONTEXT_FACTORY);

19.        singleton.envProps.setProperty(Context.PROVIDER_URL, singleton.sLDAP_URL);

20.        singleton.envProps.setProperty(Context.SECURITY_AUTHENTICATION, singleton.sAUTH_TYPE);

21.        singleton.envProps.setProperty(Context.SECURITY_PRINCIPAL, singleton.sMANAGER_DN);

22.        singleton.envProps.setProperty(Context.SECURITY_CREDENTIALS, singleton.sMANAGER_PASSWORD);

23.        /*

24.         * 绑定ldap服务器

25.         */

26.        singleton.dirCtx = new InitialDirContext(singleton.envProps);

27.    }

28.}

利用LDAP操作AD域的更多相关文章

  1. JAVA使用Ldap操作AD域

    项目上遇到的需要在集成 操作域用户的信息的功能,第一次接触ad域,因为不了解而且网上其他介绍不明确,比较费时,这里记录下. 说明: (1). 特别注意:Java操作查询域用户信息获取到的数据和域管理员 ...

  2. Java使用LdAP获取AD域用户

    随着我们的习大大上台后,国家在网络信息安全方面就有了非常明显的改变!所以如今好多做网络信息安全产品的公司和须要网络信息安全的公司都会提到用AD域server来验证,这里就简单的研究了一下! 先简单的讲 ...

  3. SonarQube 配置 LDAP(AD域)

    安装插件 1.下载 LDAP Plugin 插件,地址:https://docs.sonarqube.org/display/SONARQUBE67/LDAP+Plugin2.将下载的插件,放到 SO ...

  4. AD 域服务简介(一)- 基于 LDAP 的 AD 域服务器搭建及其使用(转)

    一.前言 1.1 AD 域服务 什么是目录(directory)呢? 日常生活中使用的电话薄内记录着亲朋好友的姓名.电话与地址等数据,它就是 telephone directory(电话目录):计算机 ...

  5. JAVA 通过LDAP获取AD域用户及组织信息

    因为工作需求近期做过一个从客户AD域获取数据实现单点登录的功能,在此整理分享. 前提:用户可能有很多系统的情况下,为了方便账号的统一管理使用AD域验证登录,所以不需要我们的系统登录,就需要获取用户的A ...

  6. 实验记录贴 —— 账号同步实验 RTX 和 LDAP(AD域)

    目前,公司有多个系统,RTX,邮箱(MD),OA,NC. 这些系统之间,如果要实现单点登录的话,账户肯定需要同步,或者某一种映射机制. 如果所有数据都和中央账号数据库(LDAP,这里是AD域)看齐,那 ...

  7. Ldap实现AD域认证

    1.java Ldap基础类 package com.common; import java.io.FileInputStream; import java.io.IOException; impor ...

  8. Java利用jcifs集成AD域用户认证

    近期一段时间发现AD这东西老火了,尤其是涉及到安全这一方面的,所以AD域用户认证成了如今网络安全方面的产品必备!这里就简单的分享一下,Java通过jcifs集成AD域用户实现认证,以实现网络安全! 我 ...

  9. AD域相关的属性和C#操作AD域

     “常规”标签  姓 Sn 名 Givename 英文缩写 Initials 显示名称 displayName 描述 Description 办公室 physicalDeliveryOfficeNam ...

随机推荐

  1. Template简介

    分类   ControlTemplate ItemsPanelTemplate DataTemplate 样式Style和模板Template对比 Style:样式,风格Template:模版,某种控 ...

  2. WPF依赖属性对内存的使用方式

    WPF允许对象在创建时候并不包含存储数据的空间,只保留在用到时获取数据默认值,借用其他对象数据或者实时分配空间的能力

  3. facebook javascript api 使用

    官方api文档:http://developers.facebook.com/docs 先简单的介绍下创建一个app(https://developers.facebook.com/apps),

  4. 数据绑定(六)使用XML数据作为Binding的Source

    原文:数据绑定(六)使用XML数据作为Binding的Source .NET Framework提供了两套处理XML数据的类库 1. 符合DOM标准的类库:包括XmlDocument.XmlEleme ...

  5. 关于阿里云centos7安装svn,客服端无法链接的问题

    阿里云的centos7的版本中,通过yum安装了subversion之后,svn客服端无法链接svn服务器. 首先确定服务器的安全组策略中的3690端口是否打开 然后确定svnserve配置是否正确, ...

  6. Python标准库(3.x): itertools库扫盲

    itertools functions accumulate() compress() groupby() starmap() chain() count() islice() takewhile() ...

  7. iText类库再pdf中显示中文字体

    using iTextSharp.text; using System; using System.Collections.Generic; using System.IO; using System ...

  8. Solr Principal - 工作原理/机制

    From http://lucene.apache.org/solr/guide/7_1/overview-of-documents-fields-and-schema-design.html The ...

  9. 《HTML开发Mac OS App 视频教程》 第001讲、入门教程

    土豆网同步更新:http://www.tudou.com/plcover/VHNh6ZopQ4E/   使用HTML 创建Mac OS App 视频教程. 官方QQ群: (1)App实践出真知 434 ...

  10. QT多个UI文件加入一个项目

    这样可在多个UI界面上进行分部开发.避免都在一个UI下太凌乱…… 在网上找了一些资料,很少有介绍这方面的,以及类似这样项目的源码. 看 一些基本控件的使用时,想到了一种方法:使用gridLayout控 ...