1、pom引入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-ldap</artifactId>
  4. </dependency>

2、创建一个资源类LdapRepository

首先需要创建一个实体

  1. @Data
  2. public class LdapServer {
  3.  
  4. /**
  5. * ldap服务器
  6. */
  7. @NotBlank
  8. private String url;
  9.  
  10. /**
  11. * 端口
  12. */
  13. @NotBlank
  14. private Integer port;
  15.  
  16. /**
  17. * 基础域
  18. */
  19. @NotBlank
  20. private String baseDN;
  21.  
  22. /**
  23. * 用户名
  24. */
  25. @NotBlank
  26. private String userName;
  27.  
  28. /**
  29. * 密码
  30. */
  31. @NotBlank
  32. private String password;
  33.  
  34. }

建立连接:

  1.  
  1.   private LdapTemplate ldapTemplate;

  1. /**
  2. * 使用前必须先连接
  3. *
  4. * @param server
  5. */
  6. public LdapRepository connect(LdapServer server) {
  7. if(server.getUrl().contains("ldaps")){
  8. SSLLdapContextSource contextSource = new SSLLdapContextSource();
  9. contextSource.setUrl(server.getUrl() + ":" + server.getPort());
  10. contextSource.setUserDn(server.getUserName());
  11. contextSource.setPassword(server.getPassword());
  12. contextSource.setPooled(false);
  13. contextSource.setBase(server.getBaseDN());
  14. contextSource.afterPropertiesSet();
  15. contextSource.setReferral("follow");
  16. // 设置连接超时时间 3s
  17. Map<String, Object> envProperties = new HashMap<>();
  18. envProperties.put("com.sun.jndi.ldap.connect.timeout", "3000");
  19. envProperties.put("com.sun.jndi.ldap.read.timeout", "3000");
  20. contextSource.setBaseEnvironmentProperties(envProperties);
  21. ldapTemplate = new LdapTemplate(contextSource);
  22. }else {
  23. LdapContextSource contextSource = new LdapContextSource();
  24. contextSource.setUrl(server.getUrl() + ":" + server.getPort());
  25. contextSource.setUserDn(server.getUserName());
  26. contextSource.setPassword(server.getPassword());
  27. contextSource.setPooled(false);
  28. contextSource.setBase(server.getBaseDN());
  29. contextSource.afterPropertiesSet(); // important
  30. contextSource.setReferral("follow");
  31. // 设置连接超时时间 3s
  32. Map<String,Object> envProperties = new HashMap<>();
  33. envProperties.put("com.sun.jndi.ldap.connect.timeout","3000");
  34. envProperties.put("com.sun.jndi.ldap.read.timeout","3000");
  35. contextSource.setBaseEnvironmentProperties(envProperties);
  36. ldapTemplate = new LdapTemplate(contextSource);
  37. }
  38. ldapTemplate.setIgnorePartialResultException(true);
  39. return this;
  40. }

测试认证连接:

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

一次查询所有人员:

  1.   /**
  2. * 查询所有人员
  3. */
  4. public List findAll(LdapQuery ldapQuery) {
  5. List<BasicAttributes> basicAttributesList = (List) ldapTemplate.search(ldapQuery, new AttributesMapper<Object>() {
  6. @Override
  7. public Object mapFromAttributes(Attributes attributes) throws NamingException {
  8. BasicAttributes basicAttributes = (BasicAttributes) attributes;
  9. return basicAttributes;
  10. }
  11. });
  12. return basicAttributesList;
  13. }

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

  1. public List<BasicAttributes> findAllPageNew(LdapQuery ldapQuery) {
  2. String searchFilter = "(&(objectClass=person)(!(objectclass=computer)))";
  3. List<BasicAttributes> attributesList = new ArrayList<>();
  4. ldapTemplate.setIgnorePartialResultException(true);
  5. SearchControls searchControls = new SearchControls();
  6. /**
  7. * 0:OBJECT_SCOPE,搜索指定的命名对象。
  8. * 1:ONELEVEL_SCOPE,只搜索指定命名对象的一个级别,这是缺省值。
  9. * 2:SUBTREE_SCOPE,搜索以指定命名对象为根结点的整棵树
  10. */
  11. searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  12. // 每次查询条数:默认1000条
  13. PagedResultsDirContextProcessor processor = new PagedResultsDirContextProcessor(1000);
  14. //返回的参数
  15. AttributesMapper CN_ATTRIBUTES_MAPPER = attributes -> {
  16. BasicAttributes basicAttributes = (BasicAttributes) attributes;
  17. return basicAttributes;
  18. };
  19. do {
  20. List<BasicAttributes> searchList = (List) ldapTemplate.search("",
  21. searchFilter,
  22. searchControls,
  23. CN_ATTRIBUTES_MAPPER,
  24. processor);
  25. attributesList.addAll(searchList);
  26. } while(processor.hasMore());
  27.  
  28. return attributesList;
  29. }

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. 《深入理解JAVA虚拟机》(一) JVM 结构 + 栈帧 详解

    ​ 1.程序计数器(Program Counter Register)         线程独有,每个线程都有自己的计数器:由于CPU的任意时刻只能执行所有线程中的一条,所以需要使用程序计数器来支持J ...

  2. 使用 Hugging Face 微调 Gemma 模型

    我们最近宣布了,来自 Google Deepmind 开放权重的语言模型 Gemma现已通过 Hugging Face 面向更广泛的开源社区开放.该模型提供了两个规模的版本:20 亿和 70 亿参数, ...

  3. 一个Git Commit Message模板

    一个统一的commit消息模板可以约束团队成员使用一致的方式提交变更信息,这样也方便集成工具进行合规检查. 通常来讲,commit信息应该包含如下内容: <type>(<scope& ...

  4. Python嵌套绘图并为条形图添加自定义标注

    论文绘图时经常需要多图嵌套,正好最近绘图用到了,记录一下使用Python实现多图嵌套的过程. 首先,实现 Seaborn 分别绘制折线图和柱状图. '''绘制折线图''' import seaborn ...

  5. django中从你的代码运行管理命令call_command

    # 主要用法就是调用django自定义的Command命令 # 语法 django.core.management.call_command(name,*args,**options) - name ...

  6. ABP开发需要用到的命令

    0.命令行在哪里执行? 在Visual Studio的"解决方案资源管理器"的解决方案或者项目上点鼠标右键,选择"在终端中打开". 1.安装abp的命令行 官网 ...

  7. 【Azure 应用服务】Azure Function 不能被触发

    问题描述 Azure Function 不能被Postman 触发,错误信息如下: Error: write EPROTO 4020778632:error:100000f7:SSL routines ...

  8. 开源.NET8.0小项目伪微服务框架(分布式、EFCore、Redis、RabbitMQ、Mysql等)

    1.前言 为什么说是伪微服务框架,常见微服务框架可能还包括服务容错.服务间的通信.服务追踪和监控.服务注册和发现等等,而我这里为了在使用中的更简单,将很多东西进行了简化或者省略了. 年前到现在在开发一 ...

  9. 如何避免MYSQL主从延迟带来的读写问题?

    在MYSQL 部署架构选型上,许多公司都会用到主从读写分离的架构,如下是一个一主一从的架构,主库master负责写入,从库slave进行读取. 但是既然是读写分离,必然会面临这样一个问题,当在主库上进 ...

  10. folder-alias vscode左侧目录树 起别名 插件 (git decorations)

    folder-alias vscode左侧目录树 起别名 插件 插件 效果 不足 文件路径或目录路径中包含中文 会挂不上别名,纯英文路径没问题 有修改后,git会覆盖,不显示别名 个人意见 我的项目都 ...