`
chilongxph
  • 浏览: 135772 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

LDAP代码操作Demo

阅读更多
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();
   }



文章来源:http://hi.baidu.com/millionware/blog/
分享到:
评论

相关推荐

    LDAP代码操作 Demo

    LDAP代码操作Demo,LDAP操作代码样例 初始化LDAP 目录服务上下文、绑定/创建LDAP条目对象、获取条目属性、修改条目属性等实例

    LDAP完整读取demo

    LDAP完整读取demo

    cas整合ldap demo

    这是cas+ldap的完整demo,只需要将这个下载下来的demo解压缩,就可直接导入eclipse或者myeclipse中使用,只需要修改几个参数而已,详情请看博客: http://blog.csdn.net/danruoshui315/article/details/48001547

    spring-ldap demo

    包含spring-ldap的简单操作的例子和EXT结合展示 以及相应的文档

    ldap相关代码例子.rar

    ldap相关代码例子.rar

    SpringLdapDemo

    学习SpringSecurity时,看到LDAP认证,不了解LDAP...所以转头学习了一下LDAP,搭建了一个DEMO,仅作记录。 LDAP(Lightweight Directory Access Protocol) 轻量级目录访问协议,LDAP目录以树状的层次结构来存储数据。

    c操作ldap例子c操作ldap例

    c操作ldap例c操作ldap例c操作ldap例c操作ldap例c操作ldap例c操作ldap例c操作ldap例c操作ldap例c操作ldap例

    基于JavaJNDI的LDAP Demo

    基于Java JNDI 操纵LDAP的基础实现,包括LDAP连接相关生命周期,认证的两种策略demo,条目的操作,schema的基础操作包括(attributeDefinition\objectDefinition\)但未包含相关syntaxDefinition的操纵. 没有资源分的朋友...

    ldap源代码

    ldap协议源代码

    java操作ldap类

    java操作ldap类java操作ldap类java操作ldap类

    修改ldap密码源代码

    修改ldap密码源代码 change_ldap_pwd.zip

    如何将对ldap的操作与对oracle的操作封装在一个事务中

    如何将对ldap的操作与对oracle的操作封装在一个事务中? 如何处理分布的database,LDAP事务? 详见项目代码 其中测试文件位置:src\test\com\aua\service\impl\StudentServiceTest.java spring配置文件位置: src...

    对LDAP的基本操作(Spring-ldap)+Ext实现显示LDAP的树状结构

    rar包:一个Web工程, 主要有,利用Spring-ldap对LDAP的基本操作(查询,增删改);Extjs实现的对Ldap的树状结构的显示,结构有点类似Softerra LDAP;一个测试类。 pdf:spring-ldap-reference.pdf Extjs.pdf ...

    Python+ldap3 实现操作AD域控

    通过Python,基于ldap3来实现操作AD域控,账户信息获取、解锁账户、禁用账户、启用账户、重置密码等功能。

    JAVA使用Ldap操作AD域的方法示例

    主要介绍了JAVA使用Ldap操作AD域的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    ldap启动,操作ldap

    操作ldap,便以后的操作,为广大Java开发者提供更好的资料。

    LDAP java 操作

    java 操作 ids ldap!使用java 操作 IBM dirctory server ldap v3

    cas4.1.x 集成 mysql,ldap,redis(session和票据),写了简单的两个客户端 demo

    cas4.1.x 集成 mysql,ldap,redis(session和票据),写了简单的两个客户端 democas4.1.x 集成 mysql,ldap,redis(session和票据),写了简单的两个客户端 democas4.1.x 集成 mysql,ldap,redis(session和票据),写了简单...

    LDAP 资料代码实例

    关于LDAP的资料,经常用的,包含新建,查询,删除,修改

    LDAP类库,方便操作LDAP的类库

    LDAP类库,方便操作LDAP的类库。包含Sample

Global site tag (gtag.js) - Google Analytics