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: openldap
    image: osixia/openldap:latest
    ports:
      - "8389:389"
      - "8636:636"
    volumes:
      - ~/ldap/backup:/data/backup
      - ~/ldap/data:/var/lib/openldap
      - ~/ldap/config:/etc/openldap/slapd.d
      - ~/ldap/certs:/assets/slapd/certs
    command: [--copy-service,  --loglevel, debug]
  phpldapadmin:
    container_name: phpldapadmin
    image: osixia/phpldapadmin:latest
    ports:
      - "8080:80"
    environment:
      - PHPLDAPADMIN_HTTPS="false"
      - PHPLDAPADMIN_LDAP_HOSTS=openldap
    links:
      - openldap
    depends_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 configuration
  ldap:
    urls: ldap://127.0.0.1:8389
    base: dc=example,dc=org
    username: cn=admin,${spring.ldap.base}
    password: admin


server:
  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
     */
    @Id
    private 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 {

    @Autowired
    private LdapTemplate ldapTemplate;

    /**
     * add person
     */
    @Test
    public void addPerson() {
        Person person = new Person();
        person.setUid("uid:14");
        person.setSuerName("LISI");
        person.setCommonName("lisi");
        person.setUserPassword("123456");
        ldapTemplate.create(person);
    }
    /**
     * filter search
     */
    @Test
    public 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() {
            @Override
            public 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
     */
    @Test
    public 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() {
                    @Override
                    public 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博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/606540.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Linux实验 系统管理(一)

实验目的&#xff1a; 掌握Linux系统文件检索、排序、查找命令&#xff1b;掌握Linux系统文件的特殊权限及文件默认权限umask掩码&#xff1b;掌握Linux系统用户和组管理、配置文件和常用命令。 实验内容&#xff1a; 在VMware中启动已经安装好的CentOS&#xff0c;本地登录r…

Docker Compose常用命令与属性

大家好&#xff0c;今天给大家分享Docker Compose的常用命令&#xff0c;以及docker-compose文件的属性。Docker Compose 是一个用于定义和运行多容器 Docker 应用应用的重要工具。它通过一个配置文件&#xff08;docker-compose.yml&#xff09;来详细定义多个容器之间的关联、…

拼多多投产比高了好还是低了好

投产比是衡量店铺经济效益和可行性的重要指标&#xff0c;它通过比较投入和产出&#xff08;销售额&#xff09;来反映店铺的盈利能力&#xff0c;一个高的投产比意味着相对较小的投入获得了较大的销售额&#xff0c;表明店铺的经济效益较好。要提升投产比&#xff0c;商家可以…

模型查询器在使用别名后不能使用tp6

在我们定义了模型的查询器时&#xff0c;再通过模型进行连表加别名的时候&#xff0c;使用查询器&#xff0c;查询器会没办法使用&#xff1b; 那我们可以将查询器前缀增加表名或者__TABLE__ 以上两种方式都可以&#xff0c;个人建议使用__TABLE__&#xff0c;因为这个查询器可…

蜜蜂收卡系统 加油卡充值卡礼品卡自定义回收系统源码 前后端开源uniapp可打包app

本文来自&#xff1a;蜜蜂收卡系统 加油卡充值卡礼品卡自定义回收系统源码 前后端开源uniapp可打包app - 源码1688 卡券绿色循环计划—— 一项旨在构建卡券价值再利用生态的社会责任感项目。在当前数字化消费日益普及的背景下&#xff0c;大量礼品卡、优惠券因各种原因未能有效…

网络工程师必备:静态路由实验指南

大家好&#xff0c;这里是G-LAB IT实验室。今天带大家学习一下华为静态路由实验配置 01、实验拓扑 02、实验需求 1.R1环回口11,1,1.1模拟PC1 2.R2建立2个环回口模拟Server server-1: 22,1,1.1 server-2: 44.1.1.1 3.要求使用静态路由实现全网互通 PC1去往server-1从R3走…

HA-MAc,透明质酸-甲基丙烯酸酯可用于制备具有交联能力的透明质酸基材料

【基本信息】 Hyaluronate Methacrylate&#xff08;甲基丙烯酸酯化透明质酸&#xff0c;简称HA-MAc&#xff09;是一种重要的生物材料 中文名称&#xff1a;甲基丙烯酸酯化透明质酸、透明质酸-甲基丙烯酸酯 英文名称&#xff1a;Hyaluronate Methacrylate、HA-MAc 分子量&…

电能表采用无线通信的好处

电能表采用无线通信的好处涉及多个方面&#xff0c;从提高数据传输的效率和实时性、增强系统的灵活性和可扩展性&#xff0c;到优化能源管理和提高安全性等方面&#xff0c;都展现了其独特的优势。 一、提高数据传输的效率和实时性 传统的有线通信方式在电能表数据传输中存在…

阿赵UE引擎C++编程学习笔记——信息打印输出

大家好&#xff0c;我是阿赵。   在之前介绍HelloWorld的时候&#xff0c;使用了一个打印的命令&#xff0c;把HelloWorld输出到输出日志里面。   对于我们编写程序代码来说&#xff0c;有2个手段是对程序差错非常重要的&#xff0c;一个是断点&#xff0c;另外一个是输出日…

地下水位自动监测设备:矿井水文监测系统

TH-DSW2地下水位自动监测设备具有实时性、连续性和自动化等特点&#xff0c;广泛应用于地下水深井水位测量、矿山深井水位测量、地热井水位测量。可以实现对地下水位的24小时不间断监测。它在城市供水和水资源管理、农业灌溉、地质和地下探测等领域具有广泛的应用。 在城市供水…

【python】基于岭回归算法对学生成绩进行预测

前言 在数据分析和机器学习领域&#xff0c;回归分析是一种预测连续数值的监督学习技术。当数据特征与目标变量之间存在线性关系时&#xff0c;线性回归模型尤其有用。然而&#xff0c;当特征数量多于样本数量&#xff0c;或者特征之间存在多重共线性时&#xff0c;普通最小二…

液压阀比例电磁铁控制放大器

液压阀比例电磁铁控制放大器是一种将电信号精确转换为液压动力的技术&#xff0c;用于实现对液压系统的精细控制。与传统的开关型电磁铁不同&#xff0c;比例电磁铁可以实现连续控制&#xff0c;允许进行微调以适应不同的控制需求。比例液压阀包括比例压力阀、比例流量阀等&…

融知财经:期货交易原理是怎样的?期货交易有哪些特征?

期货的原理是基于对某期货品种未来走势的判断而形成对其合约的买卖交易&#xff0c;因此期货可以解释为买涨或买跌。买涨&#xff0c;即看多交易&#xff0c;预期某期货品种未来价格上涨而进行的买入开仓交易&#xff1b;买跌&#xff0c;即看空交易&#xff0c;预期某期货品种…

【Python深度学习(第二版)(4)】神经网络的基本数据结构-张量概念、阶数、使用NumPy操作张量、张量种类、样本轴等概念

文章目录 一. 张量概念二. 张量的阶数1. 标量&#xff08;0阶张量&#xff09;2. 向量&#xff08;1阶张量&#xff09;2. 矩阵&#xff08;2阶张量&#xff09;3. 3阶张量与更高阶的张量 三. 张量属性及操作张量1. 张量的关键属性2. 在NumPy中操作张量2.1. 选择某个图片数据2.…

班级综合测评|基于Springboot+vue的班级综合测评管理系统(源码+数据库+文档)

目录 基于Springbootvue的“智慧食堂”系统 一、前言 二、系统设计 三、系统功能设计 1 管理员功能模块 2学生功能模块 3教师功能模块 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介绍&#xff1a;✌️大…

Python应用:让生活更智能

随着科技的进步&#xff0c;Python已成为一种非常受欢迎的编程语言&#xff0c;它在数据分析、人工智能、Web开发等领域有着广泛的应用。今天&#xff0c;我们将探讨一个Python应用的例子&#xff0c;这个应用将帮助我们更智能地管理日常生活。 应用名称&#xff1a;智能生活助…

Kubernetes最小单元Pod的生命周期

1.1 Pod生命周期 1.1.1 过程及状态 Pod 的生命周期管理是 Kubernetes 集群中非常重要的一部分&#xff0c;它涉及到 Pod 从创建到销毁的整个过程。下面是 Pod 生命周期中各个阶段的简要说明&#xff1a; Pod 创建过程&#xff1a;当一个 Pod 被创建时&#xff0c;Kubernetes 会…

三层架构实验

交换部分 第一个步骤配 Eth-Trunk&#xff0c;目的是&#xff1a;将两条线绑在一起 sw和1sw2上配置 [sw1]interface Eth-Trunk 0 //创建隧道 [sw1-Eth-Trunk0]q [sw1]int g 0/0/2 [sw1-GigabitEthernet0/0/2]eth-trunk 0 //进入接口划分隧道 [sw1-GigabitEthernet0/0/2]…

JavaScript算法描述【排序与搜索】六大经典排序|搜索旋转排序数组|在排序数组中查找元素的第一个和最后一个位置、数组中的第K个|

&#x1f427;主页详情&#xff1a;Choice~的个人主页 文章目录 搜索旋转排序数组方法一 二分查询最大最小值思路详解代码方法二 二分查询中间数 在排序数组中查找元素的第一个和最后一个位置、数组中的第K个最大元素和颜色分类在排序数组中查找元素的第一个和最后一个位置方法…

【LeetCode刷题】153. 寻找旋转排序数组中的最小值

1. 题目链接2. 题目描述3. 解题方法4. 代码 1. 题目链接 153. 寻找旋转排序数组中的最小值 2. 题目描述 3. 解题方法 根据题目分析&#xff0c;可以明确一点&#xff0c;无论该数组如何旋转&#xff0c;都会有这样的一个性质&#xff0c;就是nums[0] > nums[n-1]&#xf…
最新文章