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

spring ldap简单的例子(三)

2016-04-26LDAP攻城狮13086°c
A+ A-

下面接上一篇文章,我们将演示将一个人添加到部门下面,首先还是要看core.schema中对人员的定义:

objectclass ( 2.5.6.6 NAME 'person'
    DESC 'RFC2256: a person'
    SUP top STRUCTURAL
    MUST ( sn $ cn )
    MAY ( userPassword $ telephoneNumber $ seeAlso $ description $ useremail $ anquanma $ sslvpn $ incontroller ) )

可以看大sn属性和cn属性是人员必须的属性,那么我们在程序里面添加的时候就要注意了,这两个属性必须要添加上

在com.study.cn先新建一个XML,命名为ldap-cn.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.103:389"/>
        <property name="base" value="dc=itrus,dc=com,dc=cn"/>
        <property name="userDn" value="cn=Manager,dc=itrus,dc=com,dc=cn" />
        <property name="password" value="password"/>
    </bean>
    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
        <constructor-arg ref="contextSource"/>
    </bean>
    <bean id="ldapContext" class="com.study.dao.impl.CnDAOImpl">
        <property name="ldapTemplate" ref="ldapTemplate"/>
    
    </bean></beans>

可以看到,这个XML和单位的那个基本上都一样,只有最后的实现类不一样

然后再定义CnDAO接口,命名为CnDAO.java

package com.study.dao;
import java.util.Map;
public interface CnDAO {    
/**
     * 添加人员
     * @param map
     * @param supDn     */
    public void insertCn(Map<String,String> map,String supDn);
}

实现定义的接口,内容也和单位的实现类差不多CnDAOImpl.java:

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.CnDAO;
public class CnDAOImpl implements CnDAO {    
    private LdapTemplate ldapTemplate;   
    public void setLdapTemplate(LdapTemplate ldapTemplate){       
         this.ldapTemplate=ldapTemplate;
    }
    @Override    
    public void insertCn(Map<String, String> map, String supDn) {
        Attributes ouAttributes=new BasicAttributes();
        BasicAttribute ouBasicAttribute=new BasicAttribute("objectclass");
        ouBasicAttribute.add("person");
        ouAttributes.put(ouBasicAttribute);        
        for(String str:map.keySet()){
            ouAttributes.put(str,map.get(str));
        }
        DistinguishedName newCon\=new DistinguishedName(supDn);
        newContactDN.add("cn",map.get("cn"));
        ldapTemplate.bind(newContactDN,null,ouAttributes);
    }

}

下面的这个是测试类EngerCn.java:

package com.study.cn;
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.CnDAO;

@SuppressWarnings("deprecation")
public class EngerCn {    
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<String,String>();       
         /*
         * 通过core.schema可以看出objectclass为person的定义里面可以看到sn和cn是必须的
         * 需要什么属性直接put进去即可,符合schema定义的属性就可以        
         */
        map.put("sn", "张三");
        map.put("cn", "ca001");
        map.put("telephoneNumber", "168999878908");
        map.put("description", "测试人员");        
        new EngerCn().insertPersonMethod(map, "ou=软件部");//父节点是软件部
    }    
    public void insertPersonMethod(Map<String,String> map,String supDn){
        Resource resource=new ClassPathResource("com/study/cn/ldap-cn.xml");
        BeanFactory factory=new XmlBeanFactory(resource);
        CnDAO ldapCon\=(CnDAO) factory.getBean("ldapContext");
        ldapContact.insertCn(map, supDn);
    }
}

执行后结果如下:

21132744-186f943fcedf441a91ca7cb254650132.jpg

其实到这里就完成了部门和人员的添加,看懂的话其实我的方法可以再继续抽取的,添加部门和添加人员都可以使用同一个接口和同一个实现类,只不过是需要在调用方法的时候objectclass和ou、cn这些动态传入方法即可实现,我后面的导入功能同样没有做这样的的向上抽取,因为我要实现功能限定了导入部门和导入人员不能使用同一个方法。

在下一篇文章中我将介绍通过EXCEL导入部门以及部门下的部门。

 

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

PS:

  这篇文章写得感觉有点多余了,因为添加部门和添加人员基本没有区别,有区别地方在上一篇文章加以说明就可以了,但是我还是写了这篇,因为我比较笨,我怕自己以后忘记了,想记录的详细一点。



未定义标签

发表评论

必填

选填

选填

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


  登录