如果要总结Spring的LDAP(Spring开发的操作LDAP的开源Jar),必须要从LDAP说起。
LDAP:Lightweight Directory Access Protocol,翻译过来是轻量级目录访问协议。
它是基于X.500标准的(X.500:构成全球分布式名录系统的协议),说的这么抽象基本上理解不了,只需要知道是一种协议,以目录的形式(结构树)来管理资原(用户、用户组、地址簿、邮件用户等)。一些大公司会选择以LDAP来存储用户及其信息。
所以就像是数据库一般,LDAP也是有client端和server端。server端是用来存放资源,client端用来操作增删改查等操作。
1. LDAP:Schema
在LDAP中目录是按照树型结构组织——目录信息树(DIT)
directory information tree (DIT).
DIT由(Entry)组成,条目相当于关系数据库中表的记录;
条目是具有分辨名DN(Distinguished Name)的属性-值对(Attribute-value)的集合。(DN相当于关系型数据库表中的主键Primary key)
关于LDAP的基础要学习的还有很多,比如客户端的安装、数据模型的学习等等。
2. Spring LDAP
Spring LDAP就是基于JAVA开发的LDAP客户端开源工具,主要用来操用LDAP,其实现方法有点类似Spring JdbcTemplate(这个大家都非常熟悉了~)
支持Transaction (事务)
支持Pooling (连接池)
官网:
http://www.springframework.org/ldap
官方文档及例子(重要):
http://docs.spring.io/spring-ldap/docs/2.1.0.RELEASE/reference/
JAVA文档(重要):
http://docs.spring.io/spring-ldap/docs/2.1.0.RELEASE/apidocs/
GitHub(大量例子):
https://github.com/spring-projects/spring-ldap
3. 核心类:LdapTemplate
这个类非常类似Spring JdbcTemplate,JdbcTemplate的实现是通过传入sql语句和RowMapper,query返回目标列表,或是传入sql和参数,执行update方法。JdbcTemplate的优点是简化了与数据库连接的代码(实现了ldap属性到对象的映射,使得代码更为简单和优雅),以及避免了一些常见的错误。(这个开源已经更新到4+版本了,可见其应用之广)。
优点都是相通的,Spring LdapTemplate的优点是简化了与LDAP交互的代码(传统的类详见:
http://docs.oracle.com/javase/7/docs/api/javax/naming/directory/package-summary.html)以及避免了一些常见的错误。
4. 怎样理解Autherntication
要验证一个LDAP的Entry的身份(有点类似于用户名、密码登陆),LDAP的思路是通过DN搜索到目标Entry(例如一个公司员工),那么再通过这个Entry和password来验证合法性。
具体的业务比如是:一个员工要登陆公司网站,输入他的员工号和密码。我们是不能通过查询在LDAP里拿到用户的密码(安全性的限制),那么只能传入实际的密码,让LDAP server端验证合法性。
ldapTemplate.authenticate(LdapQuery query, String password);
在使用这个方法的时候曾经遇到过一个问题,如下:
调用ldapTemplate.authenticate时验证老是通不过(always return false),经查文档发现:如果ldap连接是有连接池的话,那么总是调用已创建好的连接去验证,这样是错误的。Authenticate的验证过程需要ContextSource通过传入的待验证的用户名和密码来重新绑定生成一个连接,也就是说这个方法要使用到的connection连接并不能是连接池里的那个connection。
所以需要new一个LdapContextSource类和LdapTemplate类,再通过LdapTemplate类的setContextSource(ContextSource contextSource)将持有用户名密码的ContextSource传入。
注意:在contextSource创建后,需要调用afterPropertiesSet()方法来验证所有必要的参数都已经set了(特指url、用户名、密码等),这个方法执行后,真正的contextSource才会被实例化。(特别是在Spring context上下文之外的配置中,必须要执行该方法。
这么说好像很抽象,具体代码如下:
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(url);
contextSource.setUserDn(userDn);
contextSource.setPassword(userPwd);
contextSource.setPooled(false);
contextSource.afterPropertiesSet(); // important
LdapTemplate template = new LdapTemplate();
template.setContextSource(contextSource);
Boolean result = template.authenticate(LDAP_BASE_DN, filter, pwd);
5. Pooling
Spring LDAP的pool用的是apache commons pool(
http://commons.apache.org/proper/commons-pool/index.html)
6. 通过SSL的认证方式连接
这块公司是用IBM的portal来安装的SSL,所以对于tomcat的配置并不是很了解。可以在stack-overflow上看看相关资料。
【总结】
在使用Spring ldap的一年多时间里,没有碰到太过复杂的问题,产品上线后表现也很稳定。总得来说因为跟大家都熟悉的JdbcTemplate思想上有些相似,所以学习起来成本也不高。
分享到:
相关推荐
Spring LDAP。 官网 Spring LDAP API。 Spring LDAP 开发文档。
Practical Spring LDAP is your guide to developing Java-based enterprise applications using the Spring LDAP Framework. This book explains the purpose and fundamental concepts of LDAP before giving a ...
spring ldap的使用
使用spring-ldap-core测试链接ldap com.ldap.test.service包下为Junit测试类
基于springldap的增删改查,以及通过ldap的用户名、密码进行验证
spring ldap 1.3.0的全部jar包下载
学习SpringSecurity时,看到LDAP认证,不了解LDAP根本无从下手。所以转头学习了一下LDAP,搭建了一个DEMO,仅作记录。 LDAP(Lightweight Directory Access Protocol) 轻量级目录访问协议,LDAP目录以树状的层次结构...
rar包:一个Web工程, 主要有,利用Spring-ldap对LDAP的基本操作(查询,增删改);Extjs实现的对Ldap的树状结构的显示,结构有点类似Softerra LDAP;一个测试类。 pdf:spring-ldap-reference.pdf Extjs.pdf ...
SpringLDAP和JNDI增删改查的入门级例子 能够通过编译和运行 已经包含了需要的包,用eclipse导入即可
Spring Data LDAP API。 Spring Data LDAP 开发文档。 官网 Spring Data LDAP API。
spring-ldap1.3.1,一种框架技术用来实现对ldap服务器增删改查数据的工具包。
Spring 实现LDAP 域认证,内有个人项目的实例分析,,希望大家参考,也可以给意见
spring_ldap spring_ldap spring_ldap
spring_ldap1.3的chm格式api
Practical Spring LDAP英文原版
springldap增删改查的源码 适合初学者下载学习 写的很简答 注释中加入了自己写代码遇到的问题解决方案和心得
Spring LDAP is a Java library for simplifying LDAP operations, based on the pattern of Spring's JdbcTemplate. The framework relieves the user of common chores, such as looking up and closing contexts,...
去掉JAR包了。Spring+LDAP实现单点登录SSO 单点登录 LDAP SPRING
最新spring Ldap jar包 The following distinct jar files are included in the distribution. This list specifies the respective contents and third-party dependencies.
NULL 博文链接:https://zhangzhenting.iteye.com/blog/622311