你好,欢迎访问我的博客!登录
当前位置:首页 - LDAP - 正文 求知成瘾,却无作品!

spring ldap的简单例子(二)

2016-04-25LDAP攻城狮3168°c
A+ A-

好了,有了前一个例子,对spring ldap有了简单的了解,下面我将一步一步加以说明,来完成上面那个例子。

首先你本地应该有LDAP环境,利用我上一篇文章里附带的安装包和配置文件安装配置完毕即可,我刚刚又检查了一遍,根据我上一篇文章附带的安装说明,搭建LDAP环境应该是没有问题的 。

除了这些,你还要多LDAP的schema要有所了解,知道它是做什么的,如何更改他们来符合自己的需求等,这里我推荐一篇文章,因为我也对schema一知半解,仅仅停留在会使用的层面上,不敢误导人,看看这篇文章吧:http://jimmyleeee.blog.163.com/blog/static/9309618200971123940563/

下面我通过程序向ldap添加一个部门,前提是已经按照上一篇文章创建好了目录,不管你有没有跑通我上一篇文章中的例子,至少目录应该建立好了,下面所说的都基于这个目录结构来说的

21103952-c1cbfd45d5e44ed6897b0c2fe26e9a18.jpg

首先到你LDAP安装目录下找到这个文件,我的路径是:C:\Program Files (x86)\OpenLDAP\schema\core.schema

我们是要添加部门,那么首先要看看部门定义允许存入哪些属性,我的如下

objectclass ( 2.5.6.5 NAME 'organizationalUnit'
    DESC 'RFC2256: an organizational unit'
    SUP top STRUCTURAL
    MUST ou
    MAY ( userPassword $ searchGuide $ seeAlso $ businessCategory $
        x121Address $ registeredAddress $ destinationIndicator $
        preferredDeliveryMethod $ telexNumber $ teletexTerminalIdentifier $
        telephoneNumber $ internationaliSDNNumber $
        facsimileTelephoneNumber $ street $ postOfficeBox $ postalCode $
        postalAddress $ physicalDeliveryOfficeName $ st $ l $ description $ sslvpn $ incontroller $ deptcode ) )

这个你的不一定和我的会一样,我的修改过了,我这里就用默认的属性操作,你可以不用动自己的,默认就行。

下面编写程序来添加部门

首先是要连接到LDAP,所以,跟使用JDBC一样,要有连接地址、用户名、密码、以及LDAP要求的DN值,其实这些你已经在配置LDAP 的时候看到过了,

21105150-815e6ebd542d44c69eba50369b3b99c6.jpg

我们项目中的这个XML文件com.study.ou.ldap-ou.xml的配置信息如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>
    <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
        <property name="url"  value="ldap://192.168.0.108:389"/><!--这个就是连接地址了,LDAP默认端口是389-->
        <property name="base" value="dc=itrus,dc=com,dc=cn"/><!--这个就是baseDN了-->
        <property name="userDn" value="cn=Manager,dc=itrus,dc=com,dc=cn" /><!--这个是用户DN-->
        <property name="password" value="secret"/><!--密码-->
    </bean>
<!--下面这些你应该认识了,不用我多说了吧-->    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
        <constructor-arg ref="contextSource"/>
    </bean>
    <bean id="ldapContext" class="com.study.dao.impl.OuDAOImpl">
        <property name="ldapTemplate" ref="ldapTemplate"/>
    
    </bean></beans>

接下来我们在程序里面需要定义一个DAO,看到上面的配置文件,我想这个你应该可以想到了吧

package com.study.dao;
import java.util.Map;
public interface OuDAO {    
/**
     * 根据DN值插入单位
     * @param map
     * @param supDn     */
    public void insertOu(Map<String,String> map,String supDn);
}

然后实现这个接口

package com.study.dao.impl;
import java.util.Map;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import com.study.dao.OuDAO;
public class OuDAOImpl implements OuDAO {
    LdapTemplate ldapTemplate;    
    public void setLdapTemplate(LdapTemplate ldapTemplate){        
        this.ldapTemplate=ldapTemplate;
    }
    @Override    
    public void insertOu(Map<String, String> map, String supDn) {
        Attributes ouAttributes=new BasicAttributes();//创建一个dn的属性集,
        BasicAttribute ouBasicAttribute=new BasicAttribute("objectclass");
        ouBasicAttribute.add("organizationalUnit");//声明自己要添加的类型是objectclass=organizationalUnit的,这样就对应了我上面贴出来的core.schema
        ouAttributes.put(ouBasicAttribute);        
        for(String str:map.keySet()){//从map中循环遍历拿出属性和属性值放入属性集中
            ouAttributes.put(str,map.get(str));
        }
        DistinguishedName newCon\=new DistinguishedName(supDn);//这里的参数supDn是说自己的上一级节点是谁,这个方法中我们传入的是"",没有上一级
        newContactDN.add("ou",map.get("ou"));//显示在树中的值
        ldapTemplate.bind(newContactDN,null,ouAttributes);//执行添加,这个方法在被添加的节点已存在的情况下会抛异常

    }

}

写一个简单的测试方法,就不用junit了,直接写main方法方便观package com.study.ou;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.study.dao.OuDAO;


@SuppressWarnings("deprecation")
public class EngerOu {    
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<String,String>();
      //构建一个map,键是core.schema中organizationalUnit下必选属性+任意的可选属性,ou是organizationalUnit的必选属性        
      map.put("deptCode", "a01");
        map.put("ou", "软件部");
        map.put("description", "这是一个部门节点");
        map.put("postalAddress", "正常"); 
        new EngerOu().testMethod(map, "");//传入map和父节点
    }    
  public void testMethod(Map<String,String> map,String supDn){
        Resource resource=new ClassPathResource("com/study/ou/ldap-ou.xml");
        BeanFactory factory=new XmlBeanFactory(resource);
        OuDAO ldapCon\=(OuDAO) factory.getBean("ldapContext");
        ldapContact.insertOu(map, supDn);
    }
}

执行后会向ldap添加一条ou记录,ou是对部门简称。

21113746-70942b607ecf484499f35cc88c421786.jpg

这样就完成了添加部门的操作。

大致的操作也就这样啦,下一篇将介绍人员的添加,其实和部门的添加也很相似。

 

================================================

PS:

  1、最重要的是对LDAP要有点了解,要知道它的表现形式。

  2、在用程序操作之前要明白为什么会选用LDAP,要看LDAP的一些百科

  3、了解LDAP的可以无压力了,我的文章只适合小白看了,我自己也是个小白。



未定义标签

发表评论

必填

选填

选填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。


  登录