【MAC】Spring Boot 集成OpenLDAP(含本地嵌入式服务器方式)

目录

一、添加springboot ldap依赖:

二、本地嵌入式服务器模式

1.yml配置

 2.创建数据库文件:.ldif

3.实体类

4.测试工具类 

 5.执行测试

三、正常连接服务器模式

1.yml配置

2.连接LDAP服务器配置类,初始化连接,创建LdapTemplate 

3.创建admin用户条目映射类

 4.LdapUtil中添加测试方法

5.执行测试


一、添加springboot ldap依赖:

        <!--spring security ldap支持 做认证用,若不做认证只是测试连一下,可以不依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!--ldap依赖,版本建议和boot版本一致-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-ldap</artifactId>
        </dependency>
        <!--  unboundid-ldapsdk主要是为了在这里使用嵌入式的LDAP服务端来进行测试操作,所以scope设置为了test,
        实际应用中,会连接真实的、独立部署的LDAP服务器,不需要此项依赖。      -->
        <dependency>
            <groupId>com.unboundid</groupId>
            <artifactId>unboundid-ldapsdk</artifactId>
            <scope>test</scope>
        </dependency>

二、本地嵌入式服务器模式

在 Spring Boot 应用程序中,您可以使用 application.propertiesapplication.yml 来配置嵌入式 LDAP 服务器。当您使用 spring.ldap.embedded 配置属性时,Spring Boot 会尝试启动一个内嵌的 LDAP 服务器,并加载指定的 LDIF 文件作为初始数据。

 application.yml 配置如下内容:

spring:
  ldap:
    embedded:
      ldif: classpath:ldap-server.ldif
      base-dn: dc=testldap,dc=com
#classpath 意味着LDIF文件可以在任何标记为类路径的位置找到,而src/main/resources目录下的内容在构建过程中会被自动加入到类路径中。

这里是如何读取配置并启动内嵌的 LDAP 服务器:

  1. ldif: 这个字段指向一个 LDIF (LDAP Data Interchange Format) 文件。在这个例子中,它指向类路径下的 ldap-server.ldif 文件。Spring Boot 将会在应用启动时从这个文件加载数据到内嵌的 LDAP 服务器中。请确保该 LDIF 文件存在于您项目的资源目录下,例如 src/main/resources/ldap-server.ldif

  2. base-dn: 这是内嵌 LDAP 服务器使用的基本目录名称(Base DN)。在此示例中,所有的 LDAP 数据都将被加载至根目录 dc=testldap,dc=com 下。

当 Spring Boot 应用程序启动时,它会自动配置一个内嵌的 LDAP 服务器并加载 ldap-server.ldif 文件中定义的数据。不需要编写任何额外的代码来启动或初始化 LDAP 服务器;Spring Boot 的自动配置功能会处理这一切。

需要引入两个相关依赖:

unboundid-ldapsdk
spring-boot-starter-data-ldap

通过这种配置,您就可以在本地开发环境中模拟 LDAP 服务,而无需连接到真实的 LDAP 服务器。这在进行集成测试或本地开发时非常有用

1.yml配置

spring: #springboot的配置
  ldap:
    embedded:
      ldif: classpath:ldap-server.ldif
      base-dn: dc=testldap,dc=com
      port:8389 #默认839

如果ldap-server.ldif和application.yml在相同文件夹下,可以用相对路径,可以不加classpath

我这里配置的时候使用的是nacos 肯定不在相同文件夹下,需要加classpath

 2.创建数据库文件:.ldif

src/test/resources目录下创建ldap-server.ldif文件

文件内容

dn: dc=testldap,dc=com
objectClass: top
objectClass: domain

dn: ou=users,dc=testldap,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people

dn: uid=1,ou=users,dc=testldap,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: zhangsan
sn: zhang
uid: 1
userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=

3.实体类(条目映射)


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;

/**
 * @version v1.0
 * @className LdapUser
 * @description  与 LADP存储内容进行映射
 */
@Entry(
        base = "ou=users,dc=testldap,dc=com",
        objectClasses = {"top", "person", "organizationalPerson", "inetOrgPerson"}
)
@Data
public class LdapUserinfo {
    @Id
    @JsonIgnore
    private Name id; // LDAP Distinguished Name (DN)

    @Attribute(name = "cn") // Common Name
    private String realName;

    @Attribute(name = "mobile") // Mobile number
    private String phone;

    @Attribute(name = "userPassword")
    private String password;

    @Attribute(name = "mail") // E-mail address
    private String emailAddr;

    @Attribute(name = "uid")
    private String userId;

}

4.测试工具类 


import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.query.ContainerCriteria;
import org.springframework.ldap.query.LdapQueryBuilder;
import org.springframework.stereotype.Component;

/**
 * @version v1.0
 * @className LdapUtil
 * @date 2024/5/14
 */
@Slf4j
@Component
public class LdapUtil {
    @Autowired
    private LdapTemplate ldapTemplate;
    @Value("${spring.ldap.embedded.base-dn}") 
    private String baseRoot;//从nacos读取的 为了验证能否正常读取nacos,可以不写


    public LdapUserinfo findUser(String base,String userId, String password) {
        // 根据uid 和密码 查询用户是否存在
        log.info("---findUser---userName={} password={} baseRoot={}", userId,password,baseRoot);
        LdapUserinfo userinfo = null;
        EqualsFilter filter = new EqualsFilter("uid", userId);
        base = "ou=users,dc=testldap,dc=com";
       boolean bool = ldapTemplate.authenticate(baseRoot, filter.toString(), password);
        if (bool) {
            // 构建查询条件
            LdapQueryBuilder builder = LdapQueryBuilder.query();
            // 根据 uid查询
            builder.where("uid").is(userId);
            // 注意LdapUserinfo 类,一定要跟 ldap协议中的属性名称对应
            userinfo = ldapTemplate.findOne(builder, LdapUserinfo.class);
            log.info("LdapUserinfo:" + JSONUtil.toJsonStr(userinfo));
        } else {
            log.info("未查询到用户");
        }
        return userinfo;
    }

    public void findUser() {
        // 构建查询条件
        LdapQueryBuilder builder = LdapQueryBuilder.query();
        //builder.base("dc=testldap,dc=com"); yml中已经指定了,这里不需要再指定,重复指定会导致路径解析问题
        // 根据 uid查询
        builder.where("cn").is("admin");
        // 注意LdapUserinfo 类,一定要跟 ldap协议中的属性名称对应
        AdminUser userinfo = ldapTemplate.findOne(builder, AdminUser.class);
        log.info("LdapUserinfo:" + JSONUtil.toJsonStr(userinfo));
    }

}

注:yml中已经指定了base,不需要再指定,重复指定会导致路径解析问题。尽量为空

 5.执行测试

将testldap.ldif中的内容打印出来了

debug时测试内置服务器连接,此时本地的嵌入式服务器是联通的

参考链接:Spring Boot中使用LDAP来统一管理用户信息_ldapquery-CSDN博客 

三、正常连接服务器模式

mac安装openldap详见:Mac上安装OpenLDAP服务器详细教程(Homebrew安装和自带的ldap)-CSDN博客

1.yml配置

spring: #springboot的配置
  ldap:
    urls: ldap://localhost:389
    base: dc=testldap,dc=com
    username: cn=admin,dc=testldap,dc=com
    password: secret

2.连接LDAP服务器配置类,初始化连接,创建LdapTemplate 


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version v1.0
 * @className LdapConfig
 * @date 2024/5/21
 * @description 开发配置类LdapConfiguration,初始化连接,创建LdapTemplate
 */
@Configuration
public class LdapConfig {
    private LdapTemplate ldapTemplate;
    @Value("${spring.ldap.urls}")
    private String url;
    @Value("${spring.ldap.base}")
    private String base;
    @Value("${spring.ldap.username}")
    private String username;
    @Value("${spring.ldap.password}")
    private String password;
    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        Map<String, Object> config = new HashMap();
        contextSource.setUrl(url);
        contextSource.setBase(base);
        contextSource.setUserDn(username);// 例:管理员DN
        contextSource.setPassword(password);// 例:管理员密码
        //  解决乱码
        config.put("java.naming.ldap.attributes.binary", "objectGUID");
        // 启动时不立即初始化(lazy connection initializing),直到第一次LDAP操作
        contextSource.setPooled(true);
        // 调用afterPropertiesSet方法是必须的,它会处理配置并创建初始连接
        contextSource.setBaseEnvironmentProperties(config);
        return contextSource;
    }
    @Bean
    public LdapTemplate ldapTemplate() {
        if (null == ldapTemplate) {
            ldapTemplate = new LdapTemplate(contextSource());
        }
        return ldapTemplate;
    }
}

3.创建admin用户条目映射类

根据LDAP 数据结构,你可以创建一个映射到 admin 用户条目的 Java 类。这个类需要使用 Spring LDAP ODM(Object-Directory Mapping)来映射 LDAP 条目的属性到 Java 对象的属性。

根据ldif文件条目设置字段

# testldap.com
dn: dc=testldap,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: Example Organization
dc: testldap

# admin, testldap.com
dn: cn=admin,dc=testldap,dc=com
objectClass: organizationalRole
cn: admin

@Entry(objectClasses = {"organizationalRole"})


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.ldap.LdapName;

@Entry(objectClasses = {"organizationalRole"})
@Data
public class AdminUser {
    @Id
    private LdapName dn; // LDAP Distinguished Name (DN)

    @Attribute(name = "cn")
    private String commonName;
}

 4.LdapUtil中添加测试方法

    public void findUser() {
        // 构建查询条件
        LdapQueryBuilder builder = LdapQueryBuilder.query();
        //builder.base("dc=testldap,dc=com");
        // 根据 uid查询
        builder.where("cn").is("admin");
        // 注意LdapUserinfo 类,一定要跟 ldap协议中的属性名称对应
        AdminUser userinfo = ldapTemplate.findOne(builder, AdminUser.class);
        log.info("LdapUserinfo:" + JSONUtil.toJsonStr(userinfo));
    }

5.执行测试

打印结果如下:

参考链接:springboot 整合 LDAP-阿里云开发者社区

  • 13
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值