好了,有了前一個例子,對spring ldap有了簡單的了解,下面我將一步一步加以說明,來完成上面那個例子。
首先你本地應該有LDAP環境,利用我上一篇文章里附帶的安裝包和配置文件安裝配置完畢即可,我剛剛又檢查了一遍,根據我上一篇文章附帶的安裝說明,搭建LDAP環境應該是沒有問題的 。
除了這些,你還要多LDAP的schema要有所了解,知道它是做什么的,如何更改他們來符合自己的需求等,這里我推薦一篇文章,因為我也對schema一知半解,僅僅停留在會使用的層面上,不敢誤導人,看看這篇文章吧:http://jimmyleeee.blog.163.com/blog/static/9309618200971123940563/
下面我通過程序向ldap添加一個部門,前提是已經按照上一篇文章創建好了目錄,不管你有沒有跑通我上一篇文章中的例子,至少目錄應該建立好了,下面所說的都基於這個目錄結構來說的
首先到你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 的時候看到過了,
我們項目中的這個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 newContactDN=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 ldapContact=(OuDAO) factory.getBean("ldapContext"); ldapContact.insertOu(map, supDn); } }
執行后會向ldap添加一條ou記錄,ou是對部門簡稱。
這樣就完成了添加部門的操作。
大致的操作也就這樣啦,下一篇將介紹人員的添加,其實和部門的添加也很相似。
================================================
PS:
1、最重要的是對LDAP要有點了解,要知道它的表現形式。
2、在用程序操作之前要明白為什么會選用LDAP,要看LDAP的一些百科
3、了解LDAP的可以無壓力了,我的文章只適合小白看了,我自己也是個小白。