SpringBoot 集成 LDAP
LDAP协议具体是什么可以自行查看,简单来说就是单点登录的一种实现方式
LDAP只是一种协议,实现的有 openLDAP ,Microsoft active directory 等
openLDAP部署
本文主要采用openLDAP部署,正常参考以下两篇文章即可部署完成
[LDAP部署卸载问题解决]([Openldap从安装到卸载 | textworld | Hugo Blog](https://www.textworld.cn/post/20201-08-03-openlad从安装到卸载/#:~:text=接着教大家怎么卸载openldap %24 systemctl stop slapd %24 systemctl disable,userdel ldap %23 删除openldap目录 %24 rm -rf %2Fetc%2Fopenldap))
[LDAP部署](2020年,手把手教你如何在CentOS7上一步一步搭建LDAP服务器的最新教程 - 知乎 (zhihu.com))
openLDAP 可视化
采用 docker 部署 phpldapadmin,改下端口号映射和IP即可
docker run -d --privileged -p 9090:80 --name myphpldapadmin \
--env PHPLDAPADMIN_HTTPS=false --env PHPLDAPADMIN_LDAP_HOSTS=192.168.10.7 \
--detach osixia/phpldapadmin
访问
http://192.168.10.7:9090
以上完成后会看到如下页面

重点:
此处账号其实为部署时的 cn=Manager,dc=ricman,dc=localhost
密码就是你设置的密码
创建LDAP账号



看这个创建好的账号,下文的 person 类的 @Entry(objectClasses = {"inetOrgPerson"}) 查询的就是 objectClasses 为 inetOrgPerson 的账号,也可添加其他两个条件
看属性后面凡是带有 alias 的 ,点点看前面的属性名称有惊喜哦
以 User Name 举例,它在ldap中的字段名称其实是 uid 对应下文person类的 @Attribute(name = "uid")
SpringBoot 集成 LDAP
<!-- LDAP集成 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
spring:
ldap:
# ldap 服务
urls: 'ldap://192.168.10.7:389'
# ldap 管理员账号
username: 'cn=Manager,dc=ricman,dc=localhost'
password: 123456
# ldap 根目录
base: 'dc=ricman,dc=localhost'
一定要注意这个 Person类
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import javax.naming.Name;
/**
* 注解属性对应 ldap 协议中的属性名称
* objectClasses 查询 ldap协议中 objectClasses 为 inetOrgPerson 的账号
*/
@Entry(objectClasses = {"inetOrgPerson"})
@Data
public class Person {
@Id
@JsonIgnore // 必写
private Name distinguishedName;
@Attribute(name = "cn")
private String cn;
@Attribute(name = "mail")
private String mail;
@Attribute(name = "gidNumber")
private Integer gidNumber;
@Attribute(name = "homeDirectory")
private String homeDirectory;
@Attribute(name = "sn")
private String sn;
@Attribute(name = "uidNumber")
private Integer uidNumber;
@Attribute(name = "sAMAccountName")
private String loginName;
@Attribute(name = "uid")
private String uid;
@Attribute(name="givenName")
private String givenName;
}
查询
package com.service.single.example.ldap;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.query.LdapQueryBuilder;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 集成 ldap协议 实现用户认证
*/
@Slf4j
@Service
public class LdapService {
@Autowired
private LdapTemplate ldapTemplate;
public void test() {
// 根据uid 和密码 查询用户是否存在
EqualsFilter filter = new EqualsFilter("uid", "test1");
boolean bool = ldapTemplate.authenticate("", filter.toString(), "123456");
if (bool) {
// 构建查询条件
LdapQueryBuilder builder = LdapQueryBuilder.query();
// 根据 uid查询
builder.where("uid").is("test1");
// 注意person 类,一定要跟 ldap协议中的属性名称对应
Person person = ldapTemplate.findOne(builder, Person.class);
System.out.println(JSON.toJSONString(person));
} else {
System.out.println("校验失败");
}
}
public void test1() {
// 查询 所有用户,注意 person类查询条件
List<Person> ps = ldapTemplate.findAll(Person.class);
if (ps.size() > 0) {
System.out.println(JSON.toJSONString(ps));
}
}
}
SpringBoot 集成 LDAP的更多相关文章
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- hive、impala集成ldap
1.概要 1.1 环境信息 hadoop:cdh5.10 os:centos6.7 user:root hive.impala已集成sentry 1.2 访问控制权限 这里通过使用openldap来控 ...
- SpringBoot集成security
本文就SpringBoot集成Security的使用步骤做出解释说明.
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
- SpringBoot集成Shiro并用MongoDB做Session存储
之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...
- SpringBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- springboot集成redis(mybatis、分布式session)
安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...
- SpringBoot集成jsp
一.springBoot集成jsp: 1.修改pom文件 <!--集成jsp所需jar包--> <!--jsp页面使用jstl标签--> <dependency> ...
随机推荐
- AI时代你应聚焦的领域在哪里
1. 每个人都应该拥抱AI 随着AI的飞速发展,把我们带到了一个全新的时代.在这个时代,AI将逐步渗透到各个方面,比如:自动驾驶.智能家居.医疗诊断.大模型等等.每个人都应该积极拥抱AI,让AI给我们 ...
- c++17 auto非类型模板参数
//用auto非类型模板参数 #include <iostream> using namespace std; template<auto c> auto foot() { c ...
- java8interface的新特性:default,static,funcation
default:默认方法 在类接口中可以直接定义的方法,实现接口的类可以直接使用 使用案例: public interface MyInterface { default void display() ...
- 【Spring Data JPA】08 多表关系 Part1 一对多关系操作
表关系概述: 1.一 对应 一 一对夫妻,一个男人只能有一个老婆,一个女人只能有一个老公,即这种对应关系 2.一 对应 多 [多对一] 一个年级具有多个班级,一个班级具有对应的所属年级,即这种上下层级 ...
- 使用 C# 和 ONNX 來玩转Phi-3 SLM
LLM 席卷世界刷新 AI 的认知之后,由于 LLM 需要的硬件要求实在太高,很难在普通设备上运行,因此 SLM 逐漸受到重視,Phi-3 SLM 是由 Microsoft 所开发的模型,可以在你的电 ...
- 学术写作: These authors contributed equally to this work. —— 共同一作
早些年很少看到论文里面有: These authors contributed equally to this work. 不过现在这种方法在论文中出现的还是比较多的,说白了,这种共同一作的声明其实是 ...
- mujoco安装报错:mujoco_py/cymj.pyx:67:5: Exception check on 'c_warning_callback' will always require the GIL to be acquired.
参考: https://blog.csdn.net/weixin_49373427/article/details/131981583 https://blog.csdn.net/CCCDeric/a ...
- Java项目生产启动、关闭脚本
1.直接启动 #!/bin/bash #这里可替换为你自己的执行程序,其他代码无需更改 APP_NAME=XXXX-api-1.0.jar #使用说明,用来提示输入参数 usage() { echo ...
- Python 计算几月几号是某年的第几天
闰年需要同时满足以下条件: 1.年份能被4整除: 2.年份若是 100 的整数倍的话需被400整除,否则是平年. #!/usr/bin/python # -*- coding: UTF-8 -*- # ...
- 如何在Spring Cloud中实现Nacos客户端登录密码加密
背景 公司规范要求配置文件里不能出现明文的密码.最近项目引入了Nacos作为服务的配置中心,使用的是spring-cloud-starter-alibaba-nacos-config这个包. 基本的b ...