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

来源:互联网 发布:会计专用软件v6 编辑:程序博客网 时间:2024/06/05 02:33
这些内容也可以访问我的百度空间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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 公司注册了商标怎么办 2018年属兔不适合结婚怎么办 交通事故对方没钱赔怎么办 滴滴快车出车祸怎么办 交警法院扣车怎么办 肇事逃逸没钱赔怎么办 撞死人无力赔偿怎么办 宝贝咳嗽很厉害怎么办 开车有人别车怎么办 判刑罚金交不起怎么办 罚金太多交不起怎么办 判决书上的罚金怎么办 刑事罚款不交怎么办 缎面的鞋脏了怎么办 夏天脸上长粉刺怎么办 鬃狮不吃东西怎么办 买二手车的车牌怎么办 榴莲打开了肉没熟怎么办 gmat的prep做完怎么办 电脑没ip地址怎么办 电脑ip地址缺失怎么办 cos还原不了人物怎么办 执行局抓人十五天不放人怎么办 笔记本电脑键盘按键错乱怎么办 靠墙倒立上不去怎么办 医院多收钱了怎么办 学生总是转笔怎么办 吃错东西呕吐怎么办 手腕筋扭伤了怎么办 右膝盖内侧疼痛怎么办 膝盖关节腔积液怎么办 小孩玩游戏花钱怎么办 初三玩手机上瘾怎么办 无线网有感叹号怎么办 台式电脑网络感叹号怎么办 忘记发红包密码怎么办 电信诈骗被骗后怎么办 C小孩讨厌上学怎么办 军训来大姨妈怎么办 夏天军训来月经怎么办 军训遇到大姨妈怎么办