SpringBoot集成LDAP同步数据
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同步数据的更多相关文章
- 使用Logstash同步数据至Elasticsearch,Spring Boot中集成Elasticsearch实现搜索
安装logstash.同步数据至ElasticSearch 为什么使用logstash来同步,CSDN上有一篇文章简要的分析了以下几种同步工具的优缺点:https://blog.csdn.net/la ...
- 流式大数据计算实践(5)----HBase使用&SpringBoot集成
一.前言 1.上文中我们搭建好了一套HBase集群环境,这一文我们学习一下HBase的基本操作和客户端API的使用 二.shell操作 先通过命令进入HBase的命令行操作 /work/soft/hb ...
- springboot集成Spring Data JPA数据查询
1.JPA介绍 JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据.它的出现主要是为 ...
- SpringBoot集成mybatis,同时读取一个数据库中多个数据表
SpringBoot集成mybatis,同时读取一个数据库中多个数据表: application.properties: mybatis.config-location=classpath:mybat ...
- springboot集成redis(mybatis、分布式session)
安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...
- SpringBoot集成Zipkin实现分布式全链路监控
目录 Zipkin 简介 Springboot 集成 Zipkin 安装启动 zipkin 版本说明 项目结构 工程端口分配 引入 Maven 依赖 配置文件.收集器的设置 编写 Controller ...
- Xxl-job安装部署以及SpringBoot集成Xxl-job使用
1.安装Xxl-job: 可以使用docker拉取镜像部署和源码编译两种方式,这里选择源码编译安装. 代码拉取地址: https://github.com/xuxueli/xxl-job/tree/2 ...
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- SpringBoot集成Shiro并用MongoDB做Session存储
之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
随机推荐
- keras建模的3种方式——序列模型、函数模型、子类模型
1 前言 keras是Google公司于2016年发布的以tensorflow为后端的用于深度学习网络训练的高阶API,因接口设计非常人性化,深受程序员的喜爱. keras建模有3种实现方式--序列模 ...
- RAID 10磁盘阵列实践
RAID概述 RAID技术通过把多个硬盘设备组合成一个容量更大.安全性更好的磁盘阵列,利用分散读写技术来提升磁盘阵列整体的性能,同时把多个重要数据的副本同步到不同的物理硬盘设备上,从而起到了非常好的数 ...
- 微服务程序运行步骤及nameko入门案例
首先一个微服务应用程序需要有服务的生产者和服务的消费者,另外还需要一个注册中心来管理和调度服务 1.服务提供方,即生产者启动服务,并将服务提交到注册中心注册服务 2.服务需求方,即消费者连接到注册中心 ...
- Retrofit 的基本用法
一.添加依赖和网络权限 添加依赖 implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup ...
- JVM运行时参数
JVM运行时参数 JVM运行时参数是用于配置和调整Java虚拟机的行为和性能的参数.这些参数可以在启动Java应用程序时通过命令行或配置文件进行设置,合理配置参数可以使JVM虚拟机的达到更好的性能,降 ...
- 【Azure 存储服务】如何查看Storage Account的删除记录,有没有接口可以下载近1天删除的Blob文件信息呢?
问题描述 如何查看Storage Account的删除记录,有没有接口可以下载近1天删除的Blob文件信息呢?因为有时候出现误操作删除了某些Blob文件,想通过查看删除日志来定位被删除的文件信息. 问 ...
- 【Azure 应用服务】Azure Function HTTP Trigger 遇见奇妙的500 Internal Server Error: Failed to forward request to http://169.254.130.x
问题描述 使用 Azure Funciton App,在本地运行完全成功的Python代码,发布到Azure Function就出现了500 Internal Server Error. 而且错误消 ...
- Tomcat8.5简介
1. Tomcat简介[1] Apache Tomcat是Servlet/JSP的容器.Tomcat8.5 实现了由 JCP 组织 (Java Community Process) 制定的Servle ...
- Java 抽象类 练习
1 package com.bytezreo.template; 2 3 import java.util.Calendar; 4 import java.util.Scanner; 5 6 /** ...
- Java UML类图
在UML的静态机制中类图是一个重点,它不但是设计人员关心的核心,更是实现人员关注的核心.建模工具也主要根据类图来产生代码.类图在UML的9个图中占据了一个相当重要的地位.James Rumbaugh对 ...