当前位置: 首页 > news >正文

Spring Boot集成Ldap快速入门Demo

1.Ldap介绍

LDAP,Lightweight Directory Access Protocol,轻量级目录访问协议.

  1. LDAP是一种特殊的服务器,可以存储数据
  2. 数据的存储是目录形式的,或者可以理解为树状结构(一层套一层)
  3. 一般存储关于用户、用户认证信息、组、用户成员,通常用于用户认证与授权

LDAP简称对应

  • o:organization(组织-公司)
  • ou:organization unit(组织单元-部门)
  • c:countryName(国家)
  • dc:domainComponent(域名)
  • sn:surname(姓氏)
  • cn:common name(常用名称)

2.环境搭建

docker-compose-ldap.yaml

version: '3'services:openldap:container_name: openldapimage: osixia/openldap:latestports:- "8389:389"- "8636:636"volumes:- ~/ldap/backup:/data/backup- ~/ldap/data:/var/lib/openldap- ~/ldap/config:/etc/openldap/slapd.d- ~/ldap/certs:/assets/slapd/certscommand: [--copy-service,  --loglevel, debug]phpldapadmin:container_name: phpldapadminimage: osixia/phpldapadmin:latestports:- "8080:80"environment:- PHPLDAPADMIN_HTTPS="false"- PHPLDAPADMIN_LDAP_HOSTS=openldaplinks:- openldapdepends_on:- openldap

ldap setup

docker-compose -f docker-compose-ldap.yml -p ldap up -d

open http://localhost:8080/

default account

username:cn=admin,dc=example,dc=org
password:admin

init data

dn: ou=people,dc=exapmple,dc=org
objectClass: top
objectClass: organizationalUnit
ou: people

58

3.代码工程

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ldap</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--ldap--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-ldap</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>
</project>

application.yaml


spring:application:name: spring-demo-ldap# ldap configurationldap:urls: ldap://127.0.0.1:8389base: dc=example,dc=orgusername: cn=admin,${spring.ldap.base}password: adminserver:port: 8088

Person.java

package com.et.ldap.entity;import lombok.Data;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.DnAttribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;import javax.naming.Name;
import java.io.Serializable;@Data
@Entry(base = "ou=people", objectClasses="inetOrgPerson")
public class Person implements Serializable {private static final long serialVersionUID = -337113594734127702L;/***neccesary*/@Idprivate Name id;@DnAttribute(value = "uid", index = 3)private String uid;@Attribute(name = "cn")private String commonName;@Attribute(name = "sn")private String suerName;private String userPassword;}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • https://github.com/Harries/springboot-demo

4.测试

package com.et.ldap;import com.et.ldap.entity.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.ldap.NamingException;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.test.context.junit4.SpringRunner;import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import java.util.List;import static org.springframework.ldap.query.LdapQueryBuilder.query;@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {@Autowiredprivate LdapTemplate ldapTemplate;/*** add person*/@Testpublic void addPerson() {Person person = new Person();person.setUid("uid:14");person.setSuerName("LISI");person.setCommonName("lisi");person.setUserPassword("123456");ldapTemplate.create(person);}/*** filter search*/@Testpublic void filterSearch() {// Get the domain list. If you want to get a certain domain, the filter can be written like this: (&(objectclass=dcObject)&(dc=example))// String filter = "(&(objectclass=dcObject))";// Get the list of organizations. If you want to get a specific organization, the filter can be written like this: (&(objectclass=organizationalUnit)&(ou=people)// String filter = "(&(objectclass=organizationalUnit))";//Get the people list. If you want to get a certain person, the filter can be written like this: (&(objectclass=inetOrgPerson)&(uid=uid:13))String filter = "(&(objectclass=inetOrgPerson))";List<Person> list = ldapTemplate.search("", filter, new AttributesMapper() {@Overridepublic Object mapFromAttributes(Attributes attributes) throws NamingException, javax.naming.NamingException {//如果不知道ldap中有哪些属性,可以使用下面这种方式打印NamingEnumeration<? extends Attribute> att = attributes.getAll();while (att.hasMore()) {Attribute a = att.next();System.out.println(a.getID() + "=" + a.get());}Person p = new Person();Attribute a = attributes.get("cn");if (a != null) p.setCommonName((String) a.get());a = attributes.get("uid");if (a != null) p.setUid((String) a.get());a = attributes.get("sn");if (a != null) p.setSuerName((String) a.get());a = attributes.get("userPassword");if (a != null) p.setUserPassword(a.get().toString());return p;}});list.stream().forEach(System.out::println);}/*** query search*/@Testpublic void querySearch() {// You can also use filter query method, filter is (&(objectClass=user)(!(objectClass=computer))List<Person> personList = ldapTemplate.search(query().where("objectClass").is("inetOrgPerson").and("uid").is("uid:14"),new AttributesMapper() {@Overridepublic Person mapFromAttributes(Attributes attributes) throws NamingException, javax.naming.NamingException {//If you don’t know what attributes are in ldap, you can print them in the following way// NamingEnumeration<? extends Attribute> att = attr.getAll();//while (att.hasMore()) {//  Attribute a = att.next();// System.out.println(a.getID());//}Person p = new Person();Attribute a = attributes.get("cn");if (a != null) p.setCommonName((String) a.get());a = attributes.get("uid");if (a != null) p.setUid((String) a.get());a = attributes.get("sn");if (a != null) p.setSuerName((String) a.get());a = attributes.get("userPassword");if (a != null) p.setUserPassword(a.get().toString());return p;}});personList.stream().forEach(System.out::println);}
}

运行单元测试类,查看数据,可以看到新增一个人

80

5.引用参考

  • Spring Boot集成Ldap快速入门Demo | Harries Blog™
  • Getting Started | Authenticating a User with LDAP
  • Docker安装LDAP并集成Springboot测试LDAP_ladp dockers-CSDN博客

相关文章:

Spring Boot集成Ldap快速入门Demo

1.Ldap介绍 LDAP&#xff0c;Lightweight Directory Access Protocol&#xff0c;轻量级目录访问协议. LDAP是一种特殊的服务器&#xff0c;可以存储数据数据的存储是目录形式的&#xff0c;或者可以理解为树状结构&#xff08;一层套一层&#xff09;一般存储关于用户、用户…...

杨辉三角的打印

题目内容&#xff1a; 在屏幕上打印杨辉三角。 思路&#xff1a; 首先我们通过观察发现&#xff0c;每一步的打印都与行列数有关&#xff0c;中间的数据由这一列和上一行的前一列数据控制。所以我们可以使用二维数组进行操作&#xff1a; &#xff08;&#xff11;&#xff…...

贪吃蛇(下)游戏的实现

感谢大佬的光临各位&#xff0c;希望和大家一起进步&#xff0c;望得到你的三连&#xff0c;互三支持&#xff0c;一起进步 个人主页&#xff1a;LaNzikinh-CSDN博客 文章目录 前言一.蛇和食物的打印二.游戏的运行逻辑三.结束游戏 &#xff08;善后工作&#xff09;四.游戏的测…...

偏微分方程算法之椭圆型方程差分格式编程示例

目录 一、示例1-五点菱形格式 1.1 C代码 1.2 计算结果 二、示例2-九点紧差分格式 2.1 C代码 2.2 计算结果 三、示例3-二阶混合边值 3.1 C代码 3.2 计算结果 本专栏对椭圆型偏微分方程的三种主要差分方法进行了介绍&#xff0c;并给出相应格式的理论推导过程。为加深对…...

PCIe协议之-TLP路由基础

✨前言&#xff1a; 在PCI Express (PCIe) 技术中&#xff0c;数据包的路由方式对于确保信息能够高效、准确地传送至目标设备至关重要。PCIe定义了几种路由方式&#xff0c;主要有以下几种。 &#x1f31f;地址路由&#xff08;Address Based Routing&#xff09; 这是最基本…...

inline内联函数-虚函数(virtual)可以是内联函数(inline)吗?

目录标题 inline内联函数特征&#xff1a;使用&#xff1a;编译器对inline函数的处理步骤优点&#xff1a;缺点&#xff1a; 虚函数&#xff08;virtual&#xff09;可以是内联函数&#xff08;inline&#xff09;吗&#xff1f;特征&#xff1a;使用&#xff1a; inline内联函…...

Spring Boot | Spring Boot 消息管理 ( 消息中间件 ) 、RabbitMQ“消息中间件“

目录: 一、"消息服务" 概述 :1.1 为什么要使用 "消息服务" ( 消息中间件 ) &#xff1f;① 异步处理② 应用解耦③ 流量削峰④ 分布式事务管理 1.2 常用 "消息中间件" 介绍 :ActiveMQ ( 广泛应用于中小型企业 )RabbitMQ ( 没有特别要求的场景下…...

二层交换机与路由器连通上网实验

华为二层交换机与路由器连通上网实验 二层交换机是一种网络设备&#xff0c;用于在局域网&#xff08;LAN&#xff09;中转发数据帧。它工作在OSI模型的第二层&#xff0c;即数据链路层。二层交换机通过学习和维护MAC地址表&#xff0c;实现了数据的快速转发和广播域的隔离。 实…...

AJAX知识点(前后端交互技术)

原生AJAX AJAX全称为Asynchronous JavaScript And XML,就是异步的JS和XML&#xff0c;通过AJAX可以在浏览器中向服务器发送异步请求&#xff0c;最大的优势&#xff1a;无需刷新就可获取数据。 AJAX不是新的编程语言&#xff0c;而是一种将现有的标准组合在一起使用的新方式 …...

用wordpress为外贸进出口公司搭建多语言国际站

使用WordPress为外贸进出口公司搭建多语言国际站是一个很好的选择&#xff0c;因为WordPress不仅易于使用&#xff0c;而且具有丰富的插件和主题&#xff0c;可以支持多语言内容。以下是搭建多语言国际站的步骤和建议&#xff1a; 安装WordPress&#xff1a;首先&#xff0c;您…...

雷军-2022.8小米创业思考-6-互联网七字诀之口碑:口碑即定位,超预期才有口碑,品牌建设

第六章 互联网七字诀 专注、极致、口碑、快&#xff0c;这就是我总结的互联网七字诀&#xff0c;也是我对互联网思维的高度概括。 口碑 用户口碑是所有产品成功的关键因素&#xff0c;这是不言而喻的公理。 资源永远有限&#xff0c;对于创业公司尤其如此。只有专注&#xf…...

欧盟MDR法规对医疗器械网络安全都有哪些要求?

MDR&#xff0c;欧盟医疗器械法规&#xff08;Medical Device REGULATION (EU) 2017/745&#xff0c;简称“MDR”&#xff09;&#xff0c;当医疗器械办理欧盟CE认证时&#xff0c;需满足新法规 MDR (EU) 2017/745要求。 M DR符合性评估 医械网络安全咨询与相关文件出具&#x…...

Linux —— 信号初识

Linux —— 信号初识 什么是信号测试几个信号signal函数函数原型参数说明返回值注意事项示例 后台程序前台转后台检测输入中断向量表 我们今天来继续学习Linux的内容&#xff0c;今天我们要了解的是Linux操作系统中的信号&#xff1a; 什么是信号 信号是操作系统内核与进程之…...

webpack进阶 -- 自定义Plugin,Loader封装打包优化

介绍 Webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler)。在 Webpack 处理应用程序时&#xff0c;它会在内部构建一个依赖图(dependency graph)&#xff0c;这个依赖图对应映射到项目所需的每个模块&#xff0c;并生成一个或多个 bundle。在这个过程中…...

《Decoupled Optimisation for Long-Tailed Visual Recognition》阅读笔记

论文标题 《Decoupled Optimisation for Long-Tailed Visual Recognition》 长尾视觉识别的解耦优化 作者 Cong Cong、Shiyu Xuan、Sidong Liu、Shiliang Zhang、Maurice Pagnucco 和 Yang Song、 来自新南威尔士大学计算机科学与工程学院、北京大学计算机学院多媒体信息处…...

Springboot+Vue项目-基于Java+MySQL的毕业就业信息管理系统(附源码+演示视频+LW)

大家好&#xff01;我是程序猿老A&#xff0c;感谢您阅读本文&#xff0c;欢迎一键三连哦。 &#x1f49e;当前专栏&#xff1a;Java毕业设计 精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; &#x1f380; Python毕业设计 &…...

条件平差——以水准网平差为例 (python详细过程版)

目录 一、原理概述二、案例分析三、代码实现四、结果展示本文由CSDN点云侠原创,原文链接。如果你不是在点云侠的博客中看到该文章,那么此处便是不要脸的爬虫与GPT。 一、原理概述 条件平差的函数模型和随机模型为: A V + W = 0...

mysql -- WITH RECURSIVE 语法

引言 在 SQL 中&#xff0c;WITH RECURSIVE 是一个用于创建递归查询的语句。它允许你定义一个 Common Table Expression (CTE)&#xff0c;该 CTE 可以引用自身的输出。递归 CTE 非常适合于查询具有层次结构或树状结构的数据&#xff0c;例如组织结构、文件系统或任何其他具有…...

洗地机什么品牌好?洗地机怎么选?618洗地机选购指南

随着科技的飞速发展&#xff0c;洗地机以其高效的清洁能力、稳定的性能和用户友好的设计而闻名&#xff0c;不仅可以高效吸尘、拖地&#xff0c;还不用手动洗滚布&#xff0c;已经逐渐成为现代家庭不可或缺的清洁助手。然而&#xff0c;在众多品牌和型号中&#xff0c;如何选择…...

nginx负载均衡配置

1.nginx负载均衡配置 upstream lbs {server 192.168.1.12:8080;server 192.168.1.12:8081; }server {listen 80;server_name localhost a.com;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}locatio…...

用PyTorch和ECANet18搞定RAF-DB表情分类:从数据集下载到模型部署的保姆级教程

基于ECANet18的RAF-DB表情识别实战&#xff1a;从零构建高精度分类模型 人脸表情识别&#xff08;FER&#xff09;作为计算机视觉领域的重要分支&#xff0c;在情感计算、智能交互等领域展现出巨大潜力。本文将带您完整实现一个基于PyTorch和ECANet18的端到端表情识别系统&…...

Claude API企业准入最后窗口期:2024Q3起强制启用OAuth 2.1+硬件级密钥绑定,现在不升级将无法续签

更多请点击&#xff1a; https://intelliparadigm.com 第一章&#xff1a;Claude API企业准入政策的演进与合规紧迫性 随着Anthropic对Claude模型商用边界的持续收束&#xff0c;企业级API接入正从“技术可用性”转向“治理可验证性”。2024年Q2起&#xff0c;所有新注册企业账…...

基于RAG的智能知识库问答系统:从原理到部署实战

1. 项目概述&#xff1a;当AI大模型遇见知识库&#xff0c;一个开源的智能问答解决方案 最近在折腾一个很有意思的开源项目&#xff0c;叫 zhimaAi/chatwiki 。光看名字&#xff0c;你大概能猜到它的核心&#xff1a; chat 代表对话&#xff0c; wiki 代表知识库。没错&a…...

PAC技术演进与核心趋势:从多域控制到边缘智能的工业自动化平台

1. 项目概述&#xff1a;为什么今天还要聊PAC&#xff1f;如果你在工业自动化、楼宇控制或者任何涉及逻辑控制的领域工作&#xff0c;那么“PAC”这个词对你来说应该不陌生。但很多时候&#xff0c;它就像一个熟悉的陌生人——大家好像都知道它&#xff0c;但真要细说它现在发展…...

微服务架构实战:从DDD设计到K8s部署的完整指南

1. 项目概述与核心价值最近几年&#xff0c;微服务架构的热度一直居高不下&#xff0c;从互联网大厂到初创团队&#xff0c;几乎人人都在谈微服务。但说实话&#xff0c;真正能把微服务玩转、落地&#xff0c;并且能稳定支撑业务发展的团队&#xff0c;其实并不多。很多项目要么…...

终极指南:如何在英雄联盟国服免费解锁所有皮肤?R3nzSkin国服特供版完全解析

终极指南&#xff1a;如何在英雄联盟国服免费解锁所有皮肤&#xff1f;R3nzSkin国服特供版完全解析 【免费下载链接】R3nzSkin-For-China-Server Skin changer for League of Legends (LOL) 项目地址: https://gitcode.com/gh_mirrors/r3/R3nzSkin-For-China-Server 还在…...

低成本接入GPT-4级能力:从开源模型自建到安全API实践

1. 项目概述与核心价值最近在GitHub上看到一个挺有意思的项目&#xff0c;叫a37836323/-chatgpt4.0-api-key。光看这个标题&#xff0c;很多朋友可能会立刻联想到“免费API密钥”、“共享资源”之类的。确实&#xff0c;在AI工具日益普及的今天&#xff0c;如何高效、低成本地使…...

基于RP2040与Santroller固件,复活旧吉他控制器玩转现代音游

1. 项目概述&#xff1a;让尘封的“神器”重获新生如果你和我一样&#xff0c;是个从《吉他英雄》、《摇滚乐队》时代走过来的老玩家&#xff0c;家里大概率还躺着一两把当年斥“巨资”购入的专用吉他控制器。它们手感扎实&#xff0c;造型酷炫&#xff0c;但最大的悲哀莫过于&…...

告别小白恐惧!用PyCharm+PyQt6从零打造你的第一个桌面应用(附打包exe避坑指南)

告别小白恐惧&#xff01;用PyCharmPyQt6从零打造你的第一个桌面应用&#xff08;附打包exe避坑指南&#xff09; 你是否曾遇到过这样的场景&#xff1a;精心编写的Python脚本需要交给同事使用&#xff0c;但对方却被命令行界面吓退&#xff1f;或是作为数据分析师&#xff0c;…...

高危场所专用防爆门 符合建筑消防标准

在化工车间、危险品仓库、油气厂区、锅炉房、粉尘车间等高危作业场所&#xff0c;爆炸、明火、冲击波隐患时刻存在&#xff0c;普通门窗无法起到安全防护作用&#xff0c;高危场所专用防爆门成为场地安防必备设施。 这款专业防爆门严格遵循国家建筑消防规范生产制造&#xff0…...