编程语言

img hoho19840130

spring-ldap

发表于2008/9/30 13:01:00  3192人阅读

分类: 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 AOPSpring 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 的基准dnldap的基准dn3种定义方式,你也可以用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 0

相关文章推荐

  • Spring + LDAP
    huanxue517 2016-07-11 15:40 953
  • LDAP用户验证(Spring-LDAP)
    whuqin 2012-04-11 10:25 18765
  • spring-ldap学习(一)
    Victor_Cindy1 2017-05-18 15:39 1034
  • 利用Spring进行LDAP验证登录遇到的问题及其解决方式
    t894690230 2016-10-25 22:47 3433
  • SpringLDAPDemo
    u011687117 2015-01-17 14:07 1629
  • Spring-ldap 操作LDAP
    garyichgo 2016-07-19 19:03 1494
  • SpringLdap访问Ldap服务
    Chen_yu_ting 2011-03-04 15:33 4437
img
取 消
img