(原创)使用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. SpringTemplate的Ldap测试代码编写
在配置文件中,需要配置数据源和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
- (原创)使用SPRING配置LDAP认证服务
- Spring Security-Ldap认证配置
- spring-security + ldap的认证配置
- Spring Security初体验--使用LDAP认证
- Spring Security-Ldap认证(LdapAuthenticationProvider)
- spring security LDAP 认证
- Spring Ldap 域认证
- 配置 Winows Domain LDAP 认证 和 Exchange 邮件服务
- jenkins ldap 认证配置
- zabbix配置ldap认证
- 使用LDAP(ApacheDS)构建统一认证服务(SSO单点登录)
- Apache使用LDAP认证
- Nginx使用LDAP认证
- LDAP 认证服务可用性监测
- LDAP服务原理详解(原创)
- Spring Ldap 与域认证
- SSL + LDAP 认证的配置
- twiki配置ldap账户认证
- 在GlassFish V2上使用Cluster并配置load balancer
- PROMISE SMARAID PDC20265R卡安装
- ActiveRecord中实体类问题解决方法
- SharePoint管理中心通过远程桌面不能访问,本机ie可以解决
- ALV 用对象的方法
- (原创)使用SPRING配置LDAP认证服务
- ALV Call function 的方法
- 如何添加
- 用C#调用Windows API和其它进程通信
- GridView双击某行弹出一新窗口
- GridView 72般绝技
- 流媒体技术原理及播放方式
- 关于Redo与Undo
- 防止多次提交表单的JS