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 ...
随机推荐
- lombok-ex 编译时注解框架,性能完爆 AOP
lombok-ex lombok-ex 是一款类似于 lombok 的编译时注解框架. 主要补充一些 lombok 没有实现,且自己会用到的常见工具. 编译时注解性能无任何损失,一个注解搞定一切,无三 ...
- useEffect与useLayoutEffect
useEffect与useLayoutEffect useEffect与useLayoutEffect可以统称为Effect Hook,Effect Hook可以在函数组件中执行副作用操作,副作用是指 ...
- java类初始化及代码块加载顺序连根拔起
说明 相信很多人对于java中父子继承关系中,子类实例化调用过程中,代码块的执行顺序都容易忘记或搞混,尤其是java初级笔试题或面试题最容易出这类题目,让人恨得牙痒痒!!! 本文就一次性将其连根铲除, ...
- vmware之NAT模式配置
题外话之前的题外话,本文迁移自别的社区,三年前大学实习时写下本文,过了几年再回过头来看,虽然讲得浅显,作为入门笔记也勉强合格. ---------------------------------- ...
- win32 - 使用Safer API创建受限的令牌
#include <Windows.h> #include <WinSafer.h> #include <stdio.h> #include <sddl.h& ...
- 三分钟数据持久化:Spring Boot, JPA 与 SQLite 的完美融合
三分钟,迎接一个更加高效和简便的开发体验. 在快节奏的软件开发领域,每一个简化工作流程的机会都不容错过.想要一个无需繁琐配置.能够迅速启动的数据持久化方案吗?这篇文章将是你的首选攻略.在这里,我们将向 ...
- 【Azure App Service】App Service设置访问限制后,使用git clone代码库出现403报错
问题描述 在App Service中,为App Service配置了访问限制,结果导致在克隆App Service的代码时候,遇见403错误. 问题解答 因为在使用 git clone App Ser ...
- 【Azure Developer】Github Action使用Azure/login@v1插件登录遇见错误的替代方案
问题描述 在使用 Github Action - Azure/login@v1 的插件时候,登录中国区Azure遇见了问题. Login YAML 内容: - name: 'Login via A ...
- OPPO 自研大规模知识图谱及其在数智工程中的应用
导读:OPPO 知识图谱是 OPPO 数智工程系统小布助手团队主导.多团队协作建设的自研大规模通用知识图谱,目前已达到数亿实体和数十亿三元组的规模,主要落地在小布助手知识问答.电商搜索等场景. 本文主 ...
- C++ //类模板成员函数类外实现
1 #include <iostream> 2 #include <string> 3 #include<fstream> 4 using namespace st ...