LDAP入门http://www.jianshu.com/p/7e4d99f6baaf

  • Spring LDAP,是Spring的一个组件,实现对LDAP的操作。

  • 在编程操作MySQL时,我们除了用JDBC,可能都会选用一些框架,比如JbdcTemplate。

  • JdbcTemplate的实现是通过传入sql语句和RowMapper,query返回目标列表,或是传入sql和参数,执行update方法。JdbcTemplate的优点是简化了与数据库连接的代码,以及避免了一些常见的错误。

  • 同样的,Spring LDAP框架也提供了类似的特性——LdapTemplate。

  • 优点都是相通的,Spring LdapTemplate的优点是简化了与LDAP交互的代码。

  • 按之前Spring配置JavaBean的方式,在xml文件配置LdapTemplate及其属性值即可,本文将演示使用Springboot 用Java代码的方式定义LdapTemplate,完成Spring ldap连接数据库服务及进行相关操作。

下面是使用Spring-ldap的依赖
<!-- spring ldapTemplate操作 -->
<dependency>
<groupId>com.sun</groupId>
<artifactId>ldapbp</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
  • 先介绍一些Spring-ldap,因为网上有很多教程,在给出的工程依赖中有用spring-ldap的,也有spring-ldap-core的,而且还有版本问题。笔者使用目前最新的spring-ldap-2.3.2.RELEASE。推荐直接使用,这个最新版本。

  • spring-ldap框架,是Spring集成ldap操作的总和,包含spring-ldap-core,spring-ldap-core-tiger,spring-ldap-ldif-core,spring-ldap-odm等jar,而通常我们在工程中只需要引入spring-ldap-core即可,它提供了绝大部分功能。而且截至目前,spring-ldap的<version>2.3.2.RELEASE</version>不在maven的中央仓库,不好获取。但spring-ldap-core在。

  • 另外,Spring LDAP 2.0对jdk版本要求是1.6,并且开始支持ODM,并后来引入Spring ldap pool连接池。

  • 据本人尝试,这些版本之间,变化差异很大。在新版本中可能有些关键的核心类,都会被移动到不同的package下;一些老版本完成的愚钝功能,可能在新版本中有了更好的实现或支持,所以在新版本中,一些“愚钝”实现可能会被移除。

  • 比如LdapTemplate,原先在org.springframework.ldap包,在最新版本被移至core包。在spring-ldap-core的<version>2.0.2.RELEASE</version>版本中支持类似于JPA方式的LdapRepository,但在2.3.2最新版本中,完全被移除,但是新版本增强的LdapTemplate,使得LdapTemplate功能更强大,可以完全替代LdapRepository。

下面是用Java代码的方式定义LdapTemplate,完成用Spring ldap连接LDAP服务器
import com.cvte.csb.sim.ldap.constants.LdapConstans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.pool.factory.PoolingContextSource;
import org.springframework.ldap.pool.validation.DefaultDirContextValidator;
import org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy;
import java.util.HashMap;
import java.util.Map; /**
* LDAP 的自动配置类
*
* 完成连接 及LdapTemplate生成
*/
@Configuration
public class LdapConfiguration { private LdapTemplate ldapTemplate; @Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
Map<String, Object> config = new HashMap(); contextSource.setUrl(LdapConstans.LDAP_URL);
contextSource.setBase(LdapConstans.BASE_DC);
contextSource.setUserDn(LdapConstans.USER_NAME);
contextSource.setPassword(LdapConstans.PASS_WORD); // 解决 乱码 的关键一句
config.put("java.naming.ldap.attributes.binary", "objectGUID"); contextSource.setPooled(true);
contextSource.setBaseEnvironmentProperties(config);
return contextSource;
} @Bean
public LdapTemplate ldapTemplate() {
if (null == ldapTemplate)
ldapTemplate = new LdapTemplate(contextSource());
return ldapTemplate;
} }
  • 完成LdapTemplate的bean定义,是最关键的一步。因为后续的操作,对于LDAP目录树的CRUD操作,全都靠它完成。
  • 通过上面的代码,在IOC容器完成bean的定义,我们在外部就可以注入使用LdapTemplate了。
下面给出用LdapTemplate完成CRUD功能:
import com.cvte.csb.sim.ldap.attribute.LdapDeptAttributeMapper;
import com.cvte.csb.sim.ldap.attribute.LdapUserAttributeMapper;
import com.cvte.csb.sim.ldap.module.dto.LdapUser;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import java.util.List; public class ConfigTest extends BaseTest {
@Autowired
private LdapTemplate ldapTemplate; /**
* 获取所有 内部人员
* ou=Internal,ou=People
*/
@Test
public void listUsers(){
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectClass", "person")); //查询所有内部人员
List<LdapUser> users = ldapTemplate.search("ou=Internal,ou=People", filter.encode(), new LdapUserAttributeMapper());
for (LdapUser user: users ) {
System.out.println(user);
} // Assert.assertEquals(3056, users.size());
} /**
* 根据uid 查找单个人员
*/
@Test
public void findUser(){ //uid=00012047,ou=Internal,ou=People,o=cvte.com,o=isp
DirContextAdapter obj = (DirContextAdapter) ldapTemplate.lookup("uid=00012047,ou=Internal,ou=People");//BASE_DC 不用填
System.out.println(obj);
} /**
* 根据部门编号o,查找部门
*/
@Test
public void findDept(){
//o=598b09cb12ab4364864d8ac73ecee00d,ou=Organizations,ou=People,o=cvte.com,o=isp
DirContextAdapter obj = (DirContextAdapter) ldapTemplate.lookup("o=598b09cb12ab4364864d8ac73ecee00d,ou=Organizations");//BASE_DC 不用填
System.out.println(obj);
} @Test
public void listDepts(){
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectClass", "organization"));
//search是根据过滤条件进行查询,第一个参数是父节点的dn,可以为空,不为空时查询效率更高
List depts = ldapTemplate.search("", filter.encode(), new LdapDeptAttributeMapper());
System.out.println(depts.size());
// Assert.assertEquals(3056, depts.size());
}
}
  • 在ldap中,有两个"查询"概念,search和lookup。search是ldaptemplate对每一个entry进行查询,lookup是通过DN直接找到某个条目。
  • 在Ldap中,新增与删除叫做绑定bind和解绑unBind。这些方法LdapTemplate全部提供,并且还提供各种条件过滤等方法,不如findAll(),list()等。

我们注意到,findAll(),list()肯定是返回一个java.util.List<T>,包括,

//查询所有内部人员
List<LdapUser> users = ldapTemplate.search("ou=Internal,ou=People", filter.encode(), new LdapUserAttributeMapper());

也是返回列表,列表里装的是查询出来的结果。但是上一篇文章用JNDI方式查询出来的是
Attributes attrs = ctx.getAttributes("uid=00012047,ou=Internal,ou=People");//获取到一个人员

Spring-ldap是基于JNDI实现的封装,那是哪里实现的把Attributes转成我们需要的Java Bean对象呢?

答案在new LdapUserAttributeMapper(),这个接口实现了查询结果到对象的转化。

import com.cvte.csb.sim.ldap.module.dto.LdapUser;
import org.springframework.ldap.core.AttributesMapper;
import javax.naming.NamingException;
import javax.naming.directory.Attributes; /**
* 将ldap返回的结果,转成指定对象
*/
public class LdapUserAttributeMapper implements AttributesMapper { /**
* 将单个Attributes转成单个对象
* @param attrs
* @return
* @throws NamingException
*/
public Object mapFromAttributes(Attributes attrs) throws NamingException {
LdapUser user = new LdapUser(); if(attrs.get("uid") != null){
user.setUsername( attrs.get("uid").get().toString());
}
if(attrs.get("cn") != null){
user.setUserCn( attrs.get("cn").get().toString());
}
if(attrs.get("mobile") != null){
user.setMobile( attrs.get("mobile").get().toString());
}
if(attrs.get("mail") != null){
user.setMail( attrs.get("mail").get().toString());
}
if(attrs.get("employeeNumber") != null){
user.setUserNumber( attrs.get("employeeNumber").get().toString());
} if(attrs.get("type") != null){
user.setUserType( attrs.get("type").get().toString());
}
if(attrs.get("py") != null){
user.setPinyin(attrs.get("py").get().toString());
}
if(attrs.get("alias") != null){
user.setAlias(attrs.get("alias").get().toString());
}
if(attrs.get("departmentNumber") != null){
user.setDeptId(attrs.get("departmentNumber").get().toString());
}
if(attrs.get("departmentName") != null){
user.setDeptName(attrs.get("departmentName").get().toString());
}
if(attrs.get("jobname") != null){
user.setPositionName(attrs.get("jobname").get().toString());
}
if(attrs.get("modifyTimestamp") != null){
user.setModifyTimestamp(attrs.get("modifyTimestamp").get().toString());
}
return user;
}
}

可以看到转化的过程非常繁琐,无非就是拿JNDI查询到的Attributes,不停的获取属性值,再设置到Java对象中;attrs.get("uid").get().toString()然后set。

那好了,在每次查询的时候,要查询到多少列,在这个AttributesMapper转化方法中就要写多少个,判断及赋值。而且,如果因为业务不同,要查询不同的列,那AttributesMapper接口的实现必须重新写。那有没有支持复用的方式呢?答案是肯定的。下节分享spring ldap ODM , Object-Directory Mapping。

spring-ldap-2.3.2.RELEASE所有jar包下载
http://download.csdn.net/download/ljheee/10150501

Spring-ldap最新版官方文档:
https://docs.spring.io/spring-ldap/docs/2.3.2.RELEASE/reference/

Spring LDAP的使用的更多相关文章

  1. Spring LDAP

    LDAP Spring LDAP 使用 - Sayi像秋天一样优雅 - 开源中国社区 http://docs.spring.io/spring-ldap/docs/current/reference/ ...

  2. Spring Ldap 的增删改查

    package ldap.entity; /** * 本测试类person对象来自schema文件的core.schema文件 * objectClass为person,必填属性和可选属性也是根据该对 ...

  3. 【spring实战第五版遇到的坑】4.2.3中LDAP内嵌服务器不启动的问题

    按照4.2.3中的指导一步一步的去做,在登录界面进行登录时,报错了,报错信息是LDAP服务器连接不上. 后来查了一些资源发现还需要加入一些其他的依赖,如下: <dependency> &l ...

  4. Spring Boot教程(四十二)LDAP来管理用户信息(2)

    使用spring-data-ldap的基础用法,定义LDAP中属性与我们Java中定义实体的关系映射以及对应的Repository @Data @Entry(base = "ou=peopl ...

  5. Spring Boot教程(四十一)LDAP来管理用户信息(1)

    LDAP简介 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门针对读 ...

  6. Spring学习笔记—Spring之旅

    1.Spring简介     Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...

  7. 一句话概括下spring框架及spring cloud框架主要组件

    作为java的屌丝,基本上跟上spring屌丝的步伐,也就跟上了主流技术.spring 顶级项目:Spring IO platform:用于系统部署,是可集成的,构建现代化应用的版本平台,具体来说当你 ...

  8. Spring顶级项目以及Spring cloud组件

    作为java的屌丝,基本上跟上spring屌丝的步伐,也就跟上了主流技术. spring 顶级项目: Spring IO platform:用于系统部署,是可集成的,构建现代化应用的版本平台,具体来说 ...

  9. 【译文】用Spring Cloud和Docker搭建微服务平台

    by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...

随机推荐

  1. SpringCloud(二)之我学 Ribbon

    1.负载均衡 Ribbon 虽然不是显示的配置为一个子项目,但是无论是在 API 网关的转发请求,还是服务之间的调用 Feign ,都是通过 Ribbon 来做负载均衡的. 负载均衡,主要是为了对系统 ...

  2. Vulnhub DC-6靶机渗透

    信息搜集 nmap -sP 192.168.146.0/24 #找靶机ip nmap -sS -Pn -A 192.168.146.143 #扫描靶机信息 22和80端口,老朋友了. 先直接访问htt ...

  3. 2017蓝桥杯日期问题(C++B组)

    标题:日期问题小明正在整理一批历史文献.这些历史文献中出现了很多日期.小明知道这些日期都在1960年1月1日至2059年12月31日.令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的, ...

  4. javascript入门 之 ztree (八 一系列鼠标事件)

    <!DOCTYPE html> <HTML> <HEAD> <meta http-equiv="content-type" content ...

  5. 数据结构-Python 列表(List)

    列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现 一.列表常用方法 1.创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可. eg:list1 = ['1', ' ...

  6. 多数据源系统接入mybatis-plus, 实现动态数据源、动态事务。

    目录: 实现思想 导入依赖.配置说明 代码实现 问题总结 一.实现思想 接手一个旧系统,SpringBoot 使用的是纯粹的 mybatis ,既没有使用规范的代码生成器,也没有使用 JPA 或者 m ...

  7. String 对象-->toLowerCase() 方法

    1.定义和用法 将字符串中所有的大写字符转换成小写字符,小写字符不变 返回转换后的结果字符串 语法: string.toLowerCase() 注意:不会改变字符串本身,仅以返回值的形式返回结果 举例 ...

  8. Python爬虫系列(七):提高解析效率

    如果仅仅因为想要查找文档中的<a>标签而将整片文档进行解析,实在是浪费内存和时间.最快的方法是从一开始就把<a>标签以外的东西都忽略掉. SoupStrainer 类可以定义文 ...

  9. 【Selenium04篇】python+selenium实现Web自动化:文件上传,Cookie操作,调用 JavaScript,窗口截图

    一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第四篇博 ...

  10. python 自动生成model 文件 案例分析

    生成方式 Python中想要自动生成 model文件可以通过 sqlacodegen这个命令来生成对应的model文件 sqlacodegen 你可以通过pip去安装: pip install sql ...