1、pom引入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>

2、创建一个资源类LdapRepository

首先需要创建一个实体

@Data
public class LdapServer { /**
* ldap服务器
*/
@NotBlank
private String url; /**
* 端口
*/
@NotBlank
private Integer port; /**
* 基础域
*/
@NotBlank
private String baseDN; /**
* 用户名
*/
@NotBlank
private String userName; /**
* 密码
*/
@NotBlank
private String password; }

建立连接:


  private LdapTemplate ldapTemplate;

/**
* 使用前必须先连接
*
* @param server
*/
public LdapRepository connect(LdapServer server) {
if(server.getUrl().contains("ldaps")){
SSLLdapContextSource contextSource = new SSLLdapContextSource();
contextSource.setUrl(server.getUrl() + ":" + server.getPort());
contextSource.setUserDn(server.getUserName());
contextSource.setPassword(server.getPassword());
contextSource.setPooled(false);
contextSource.setBase(server.getBaseDN());
contextSource.afterPropertiesSet();
contextSource.setReferral("follow");
// 设置连接超时时间 3s
Map<String, Object> envProperties = new HashMap<>();
envProperties.put("com.sun.jndi.ldap.connect.timeout", "3000");
envProperties.put("com.sun.jndi.ldap.read.timeout", "3000");
contextSource.setBaseEnvironmentProperties(envProperties);
ldapTemplate = new LdapTemplate(contextSource);
}else {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(server.getUrl() + ":" + server.getPort());
contextSource.setUserDn(server.getUserName());
contextSource.setPassword(server.getPassword());
contextSource.setPooled(false);
contextSource.setBase(server.getBaseDN());
contextSource.afterPropertiesSet(); // important
contextSource.setReferral("follow");
// 设置连接超时时间 3s
Map<String,Object> envProperties = new HashMap<>();
envProperties.put("com.sun.jndi.ldap.connect.timeout","3000");
envProperties.put("com.sun.jndi.ldap.read.timeout","3000");
contextSource.setBaseEnvironmentProperties(envProperties);
ldapTemplate = new LdapTemplate(contextSource);
}
ldapTemplate.setIgnorePartialResultException(true);
return this;
}

测试认证连接:

    public void authenticate(String username, String password) {
ldapTemplate.getContextSource().getContext(username, password);
}

一次查询所有人员:

  /**
* 查询所有人员
*/
public List findAll(LdapQuery ldapQuery) {
List<BasicAttributes> basicAttributesList = (List) ldapTemplate.search(ldapQuery, new AttributesMapper<Object>() {
@Override
public Object mapFromAttributes(Attributes attributes) throws NamingException {
BasicAttributes basicAttributes = (BasicAttributes) attributes;
return basicAttributes;
}
});
return basicAttributesList;
}

如果数据量太大,需要使用分页查询:

public List<BasicAttributes> findAllPageNew(LdapQuery ldapQuery) {
String searchFilter = "(&(objectClass=person)(!(objectclass=computer)))";
List<BasicAttributes> attributesList = new ArrayList<>();
ldapTemplate.setIgnorePartialResultException(true);
SearchControls searchControls = new SearchControls();
/**
* 0:OBJECT_SCOPE,搜索指定的命名对象。
* 1:ONELEVEL_SCOPE,只搜索指定命名对象的一个级别,这是缺省值。
* 2:SUBTREE_SCOPE,搜索以指定命名对象为根结点的整棵树
*/
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// 每次查询条数:默认1000条
PagedResultsDirContextProcessor processor = new PagedResultsDirContextProcessor(1000);
//返回的参数
AttributesMapper CN_ATTRIBUTES_MAPPER = attributes -> {
BasicAttributes basicAttributes = (BasicAttributes) attributes;
return basicAttributes;
};
do {
List<BasicAttributes> searchList = (List) ldapTemplate.search("",
searchFilter,
searchControls,
CN_ATTRIBUTES_MAPPER,
processor);
attributesList.addAll(searchList);
} while(processor.hasMore()); return attributesList;
}

SpringBoot集成LDAP同步数据的更多相关文章

  1. 使用Logstash同步数据至Elasticsearch,Spring Boot中集成Elasticsearch实现搜索

    安装logstash.同步数据至ElasticSearch 为什么使用logstash来同步,CSDN上有一篇文章简要的分析了以下几种同步工具的优缺点:https://blog.csdn.net/la ...

  2. 流式大数据计算实践(5)----HBase使用&SpringBoot集成

    一.前言 1.上文中我们搭建好了一套HBase集群环境,这一文我们学习一下HBase的基本操作和客户端API的使用 二.shell操作 先通过命令进入HBase的命令行操作 /work/soft/hb ...

  3. springboot集成Spring Data JPA数据查询

    1.JPA介绍 JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据.它的出现主要是为 ...

  4. SpringBoot集成mybatis,同时读取一个数据库中多个数据表

    SpringBoot集成mybatis,同时读取一个数据库中多个数据表: application.properties: mybatis.config-location=classpath:mybat ...

  5. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  6. SpringBoot集成Zipkin实现分布式全链路监控

    目录 Zipkin 简介 Springboot 集成 Zipkin 安装启动 zipkin 版本说明 项目结构 工程端口分配 引入 Maven 依赖 配置文件.收集器的设置 编写 Controller ...

  7. Xxl-job安装部署以及SpringBoot集成Xxl-job使用

    1.安装Xxl-job: 可以使用docker拉取镜像部署和源码编译两种方式,这里选择源码编译安装. 代码拉取地址: https://github.com/xuxueli/xxl-job/tree/2 ...

  8. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  9. SpringBoot集成Shiro并用MongoDB做Session存储

    之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...

  10. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

随机推荐

  1. Ubuntu下图形界面串口工具CuteCom的安装和升级

    串口的图形界面化工具在Windows下很多, 但是在Linux下可选择的就很少, CuteCom 是相对比较好用的一款了. Ubuntu20.04默认安装的是0.30.3, 这是一个比较早的版本, 最 ...

  2. Vue中data为何以函数形式返回

    data为何以函数形式返回 在使用Vue构建组件化应用时,每个组件的data属性都是以函数形式返回的,这主要是在组件化实现的时候,每个实例可以维护一份被返回对象的独立的拷贝,而不是共享同一个对象的引用 ...

  3. Java设计模式-原型模式Prototype

    介绍 当我们有一个类的实例(Prototype)并且我们想通过复制原型来创建新对象时,通常使用Prototype模式. 原型模式是一种创建型设计模式.能够复制已有对象, 而又无需使代码依赖它们所属的类 ...

  4. Java Socket编程系列(四)开发支持多客户端访问的Server

    例子来自Java官方教程,稍作调整. 上一篇介绍了单客户端访问的Server实现,这一篇实现的是多个客户端请求服务端,根据服务端提示进行一系列操作. 协议类(和系列三一样没变): package co ...

  5. C++ 多线程的错误和如何避免(11)

    不要在对时间敏感的上下文中使用 .get() 先看下面的代码, #include "stdafx.h" #include <future> #include <i ...

  6. QT - Day 1

    Date: 2021/3/12开始学习 教程视频: QT基本介绍: 跨平台图形界面引擎 优点  跨平台 接口简单,容易上手 一定程度上简化了内存回收 创建第一个QT程序  点击创建项目后,选择项目路径 ...

  7. Windows11补丁更新后无法使用Wifi和蓝牙

    最近在我的ThinkPAD T14上更新了Windows 11补丁,重启后Wifi和蓝牙鼠标都不能使用了,无法连接Wifi网络,也无法添加蓝牙设备. 使用厂家自带的管理工具查看驱动都是最新的,一度不知 ...

  8. RK3568开发笔记(三):RK3568虚拟机基础环境搭建之更新源、安装网络工具、串口调试、网络连接、文件传输、安装vscode和samba共享服务

    前言   开始搭建RK3568的基础虚拟机,具备基本的通用功能,主要包含了串口工具minicom,远程登陆ssh,远程传输filezilla,代码编辑工具vscode.   虚拟机   文档对对虚拟机 ...

  9. 【Azure Redis】Azure Redis添加了内部虚拟网络后,其他区域的主机通过虚拟网络对等互连访问失败

    问题描述 跨区域无法访问Azure Redis服务, Redis 启用了Network并设置在一个VNET中,现在客户端部署在另一个区域数据中心中,两个数据中心区域使用VNET Peer(对等互连)访 ...

  10. C# 多线程(17):小总结

    前言 本篇内容是小总结和过渡,看完这篇后,就要开始继续学习 C# 多线程中的知识点啦~. 前面,经过 16 篇的学习,我们学习了多线程.锁.线程池.任务.同步.异步等知识,还没有使用到 async.a ...