spring-ldap

 
最近一直在弄spring 与lapd的配置,上网查了不少资料,大部分中文的资料都是相似的,而且讲的也不是很好,有的只给个配置文件,连个测试类都不给下。好不容易找了个英文的,觉得讲的不错,( http://www.javaworld.com/javaworld/jw-06-2007/jw-06-springldap.html?page=2 ),完完整整的把文章看完了,然后又照着做了几个方法,在自己的
Myeclipse 下测试并通过。下面是我做的例子:
首先建个 web project ,导入 spring capabilities, 要选择 jar 包, spring AOP Spring JDBC
Spring Core ,Spring Persistence Spring Web ,可以看下我的 jar 包(下图):



在上面还有个 struts1.2 包,在这里先不有管它(整合 Struts 用的),还有个 Referenced 库,在这里面有个 Spring-ldap-1.2.1.jar 包(如图)


这个包 myeclipse 是没有的,可以在 http://www.springframework.org/ldap 去下这个 jar 包。
到此 jar 包是导完了,接下来是接口和接口类。(为了简单,我把接口和类的代码只定义了一个方法,可以跳过粉红色 的代码,只看后面的简单代码。)
接口的代码 :
package whut.com.ldap;

import java.util.List;

public interface ContactDAO {
    public List getAllContactNames();
    public List getContactDetails(String commonName,String lastName);
    public void insertContact(ContactDTO contactDTO);
    public void updateContact(ContactDTO contactDTO);
    public void deleteContact(ContactDTO contactDTO);
    public List getByCn(String cn);
    public List getBySn(String sn);

}
接口类的代码:
package whut.com.ldap;

import java.util.List;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.SearchControls;

import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;



public class LDAPContactDAO implements ContactDAO {
       private LdapTemplate ldapTemplate;

       public void setLdapTemplate(LdapTemplate ldapTemplate) {
              this.ldapTemplate = ldapTemplate;
       }

       public List getAllContactNames() {
              return ldapTemplate.search("", "(objectclass=person)",
                            new ContactAttributeMapper(){
                     public Object mapFromAttributes(Attributes attrs)
                     throws NamingException{
                            return attrs.get("cn").get();
                     }
              });
       }
       public List getByCn(String cn){
              AndFilter andFilter=new AndFilter();
              andFilter.and(new EqualsFilter("cn",cn));
              return ldapTemplate.search("uid=employee", andFilter.encode(),
                         new ContactAttributeMapper());
       }

       public List getBySn(String sn){
              AndFilter andFilter=new AndFilter();
              andFilter.and(new EqualsFilter("sn",sn));
              return ldapTemplate.search("uid=employee", andFilter.encode(),
                         new ContactAttributeMapper());
       }

       public List getContactDetails(String firstName,String lastName){
              AndFilter andFilter=new AndFilter();
              andFilter.and(new EqualsFilter("objectclass","person"));
              andFilter.and(new EqualsFilter("cn",firstName));
              andFilter.and(new EqualsFilter("sn",lastName));
           System.out.println("LDAP query :"+andFilter.encode());
           return ldapTemplate.search("", andFilter.encode(),
                         new ContactAttributeMapper());
       }
       public void insertContact(ContactDTO contactDTO){
              Attributes personAttributes=new BasicAttributes();
              BasicAttribute personBasicAttribute=new BasicAttribute("objectclass");
           personBasicAttribute.add("person");
           personAttributes.put(personBasicAttribute);
           personAttributes.put("cn",contactDTO.getCommonName());
           personAttributes.put("sn",contactDTO.getLastName());
           personAttributes.put("description",contactDTO.getDescription());
           DistinguishedName newContactDN=new DistinguishedName("uid=employee");
           newContactDN.add("cn",contactDTO.getCommonName());
           ldapTemplate.bind(newContactDN, null, personAttributes);

       }
       public void updateContact(ContactDTO contactDTO){
              Attributes personAttributes=new BasicAttributes();
              BasicAttribute personBasicAttribute=new BasicAttribute("objectclass");
              personBasicAttribute.add("person");
              personAttributes.put(personBasicAttribute);
              personAttributes.put("cn",contactDTO.getCommonName());
              personAttributes.put("sn",contactDTO.getLastName());
              personAttributes.put("description",contactDTO.getDescription());
              DistinguishedName newContactDN=new DistinguishedName("uid=employee");
              newContactDN.add("cn",contactDTO.getCommonName());
              ldapTemplate.rebind(newContactDN, null, personAttributes);
       }
       public void deleteContact(ContactDTO contactDTO){
              DistinguishedName newContactDN=new DistinguishedName("uid=employee");
              newContactDN.add("cn",contactDTO.getCommonName());
              ldapTemplate.unbind(newContactDN);
       }
}
可能写的方法有点多,对于初学者(本人也是,哈哈)我们可以只定义一个方法,把上面
接口和类简单点:
package whut.com.ldap;

import java.util.List;

public interface ContactDAO {
     public List getAllContactNames();
    }
接口类:
package whut.com.ldap;

import java.util.List;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.SearchControls;

import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;



public class LDAPContactDAO implements ContactDAO {
       private LdapTemplate ldapTemplate;

       public void setLdapTemplate(LdapTemplate ldapTemplate) {
              this.ldapTemplate = ldapTemplate;
       }

       public List getAllContactNames() {
              return ldapTemplate.search("", "(objectclass=person)",
                            new AttributesMapper(){
                     public Object mapFromAttributes(Attributes attrs)
                     throws NamingException{
                            return attrs.get("cn").get();
                     }
              });
       }
这里只做一个方法的测试。
( 注意简单的 getAllContactNames 方法中的 new AttributesMApper() 与上面的不同 )
接着当然是 application.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://localhost:389" />  
        <property name="base" value="o=tcl,c=cn" />  
        <property name="userDn" value="cn=Manager,o=tcl,c=cn" />  
        <property name="password" value="secret" />  
    </bean>  
    <bean id="ldapTemplate"
    class="org.springframework.ldap.core.LdapTemplate" >
    <constructor-arg>
        <ref bean="contextSource" />
    </constructor-arg>
    </bean>
    <bean id="ldapContact" class="whut.com.ldap.LDAPContactDAO">
    <property name="ldapTemplate">
        <ref bean="ldapTemplate" />
    </property>
    </bean>
  </beans>
contextSource bean 是用来和ldap连接的(注意有的class中可能这样定义:
class="org.springframework.ldap.support.LdapContextSource")少了个core.
应该是版本不同,可以在referenced 库下的spring-ldap.jar包下去查看。

Base 是你的 ldap 的基准 dn ldap 的基准 dn 3 种定义方式,你也可以用 dc=*** dc=** 来代替你的基准 dn ,但必须是你的基准 dn ,在 openldap 里的 slapd.conf 可以查看), 接着是用户和密码 <property name="userDn" value="cn=Manager,o=tcl,c=cn" />  
        <property name="password" value="secret" />  
有的 userDn 也用 userName 代替,(不知道有什么区别)。
下面的我就不多说了<bean id="ldapTemplate"
    class="org.springframework.ldap.core.LdapTemplate" >
    <constructor-arg>
(注意下有没有 core ,和上面的contextSource bean一样)。
最后一个 bean
<bean id="ldapContact" class="whut.com.ldap.LDAPContactDAO">
    <property name="ldapTemplate">
        <ref bean="ldapTemplate" />
    </property>
    </bean>
使用的是 spring 的注入。(了解 spring 都应该知道的。)
到此我们的 application.xml 配置文件就搞定了。下面就写个测试类:(代码如下)
package whut.com.ldap;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessException;

public class SpringFrameworkLDAP {
     public static void main(String[] args) {
       try {
        //Resource resource = new ClassPathResource("applicationContext.xml");
           //BeanFactory factory=new XmlBeanFactory(resource);
        // ContactDAO ldapContact = (LDAPContactDAO)factory.getBean("ldapContact");
        ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
           ContactDAO ldapContact = (LDAPContactDAO)ctx.getBean("ldapContact");
            List contactList = ldapContact.getAllContactNames();
             for ( int i = 0 ; i < contactList.size(); i++){
                System. out .println("Contact Name " + contactList.get(i));
            }  
        } catch (DataAccessException e) {
            System. out .println("Error occured " + e.getCause());
      }
    }
}
在英文资料中给的是我注释的连接方式来得到一个实例的。(本人不太了解,所以用最简单的了。)到此一个简单的spring和ldap的应用就完了。
在英文资料中还有ContactDTO类和ContactAttributeMapper类。感兴趣的可以
看下英文资料
http://www.javaworld.com/javaworld/jw-06-2007/jw-06-springldap.html?page=2
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值