(原创)使用SPRING配置LDAP认证服务

这些内容也可以访问我的百度空间http://hi.baidu.com/five00来访问
1.     使用Spring配置文件配置Ldap认证源
在Spring中配置Ldap的过程与配置JDBC的过程类似,Ldap比JDBC多出一个base属性,这个是配置Ldap基结点(注意:这个基结点不仅仅指最高父节点),之后Ldap会查找基节点下所有子节点的信息。
< bean id = "ldapContextSource"
       class = "org.springframework.ldap.support.LdapContextSource" >
   < property name = "url" value = "ldap://localhost:10389" />
   < property name = "base" value = " ou=People,ou=rootOrg,o=sevenSeas " />
   < property name = "userName" value = "uid=admin,ou=system" />
   < property name = "password" value = "secret" />
</ bean >
注意红字标示部分 " ou=People,ou=rootOrg,o=sevenSeas " 的顺序 ,这句话的意思是:基节点的位置是sevenSeas父节点下的rootOrg节点下的People节点;再次强调,这个解释是由于顺序的原因,父节点要在子节点的右面这样,当前Ldap源的数据就是在这个基节点下的各子节点中操作。
如图:
people基节点下的用户
1.     SpringTemplateLdap测试代码编写
在配置文件中,需要配置数据源和LdapTemplate,LdapTemplate就是Spring提供的操作Ldap服务器数据的类。
配置文件applicationContext-ldap.xml:
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop = "http://www.springframework.org/schema/aop"
    xsi:schemaLocation = "http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" >
   
    < bean id = "ldapContextSource"
        class = "org.springframework.ldap.support.LdapContextSource" >
    < property name = "url" value = "ldap://localhost:10389" />
    < property name = "base" value = "ou=People,ou=rootOrg,o=sevenSeas" />
    < property name = "userName" value = "uid=admin,ou=system" />
    < property name = "password" value = "secret" />
    </ bean >
   
    < bean id = "ldapTemplate" class = "org.springframework.ldap.LdapTemplate" >
      < property name = "contextSource" ref = "ldapContextSource" />
    </ bean >    
</ beans >

 

根据配置文件,可以通过ldapTemplate来获得Ldap中的数据信息了。
测试类 SpringLdapDemo
package sample;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ldap.AttributesMapper;
import org.springframework.ldap.LdapTemplate;
public class SpringLdapDemo {
      
       protected static final Log log = LogFactory.getLog(SpringLdapDemo.class);
       public static void main(String[] args) {
              AbstractApplicationContext lbf =
                            new ClassPathXmlApplicationContext("/applicationContext-ldap.xml");
              lbf.registerShutdownHook();
              GenericBeanFactoryAccessor gbfa = new GenericBeanFactoryAccessor(lbf);
              LdapTemplate lt = gbfa.getBean("ldapTemplate");
             
              //inetOrgPerson也可以被person替代
              List usersList = lt.search(
                       "", "(objectclass=inetOrgPerson)",
                       new AttributesMapper() {
                          public Object mapFromAttributes(Attributes attrs)
                                         throws NamingException {
                             return attrs.get("cn").get();
                          }
                       });
              //打印出用户集合
              log.info(usersList);
             
//inetOrgPerson也可以被person替代
              List passwordsList = lt.search(
                       "", "(objectclass=inetOrgPerson)",
                       new AttributesMapper() {
                          public Object mapFromAttributes(Attributes attrs)
                                         throws NamingException {
                             return attrs.get("userpassword").get();
                          }
                       });
              //打印出用户密码集合
              log.info(passwordsList);
              List rolesList = lt.search(
                       "", "(objectclass=groupOfNames)",
                       new AttributesMapper() {
                          public Object mapFromAttributes(Attributes attrs)
                                         throws NamingException {
                             return attrs.get("cn").get();
                          }
                       });
              //打印出角色集合
              log.info(rolesList);           
       }
}

v

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值