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. 从零开始手写 mybatis(一)MVP 版本

    什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. MyB ...

  2. Vue中虚拟DOM的理解

    Vue中虚拟DOM的理解 Virtual DOM是一棵以JavaScript对象作为基础的树,每一个节点称为VNode,用对象属性来描述节点,实际上它是一层对真实DOM的抽象,最终可以通过渲染操作使这 ...

  3. Oracle system identifier already exists specify another SID

    问题说明 Centos7重装Oracle,使用DBCA重新创建实例test的时候报错,中文意思是: Oracle系统标识符(SID)"test"已存在,请指定另一个SID. 问题原 ...

  4. 《深入理解Java虚拟机》(三)类加载机制

    @ 目录 1.什么是类的加载 2.类加载的过程 加载 连接 验证 文件格式验证 元数据验证 字节码验证 符号引用验证 准备 解析: 类或接口的解析 字段解析 类方法解析 接口方法解析 初始化 结束生命 ...

  5. k8s(Kubernetes) 常用命令配置

    一.基础命令 $ kubectl create -f ./my-manifest.yaml # 创建资源 $ kubectl create -f ./my1.yaml -f ./my2.yaml # ...

  6. 【Android 抓包对抗】代理检查绕过

    1. 安装apk,点进去发现一点就挂 2. apk 拖入到jadx中观察,发现多出检查,一旦满足条件就会退出 .... if (((ConnectivityManager) getSystemServ ...

  7. pexpect模块(替代subprocess)

    https://blog.csdn.net/pcn01/article/details/104993742/

  8. Jina AI x 矩池云Matpool |神经搜索引擎,一键构建

    图片.视频.语音等非结构化数据在快速增长,随着深度学习技术的不断升级,非结构化数据的搜索也逐渐形成可能.在这样的背景下,专注于神经搜索技术的商业开源软件公司--Jina AI,提出了神经搜索 (Neu ...

  9. NodeJS开启GZIP功能

    gzip是用于压缩,效果特别好,js.css等文件的压缩率一般高达70% 具体方法如下: 先安装一个依赖 npm install compression --save 然后在已有代码里(一般是app. ...

  10. Learning by teaching --- 费曼学习法

    世界上存在成千上万种学习法,如果上天只让我掌握一种,那一定就是"费曼学习法". 介绍 费曼学习法是由诺贝尔物理学奖获得者理查德·费曼提出的一种学习方法,其核心思想是将所学内容用自己 ...