springboot 整合 LDAP

简介: springboot 整合 LDAP

springboot 整合 LDAP

1、添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-ldap</artifactId>
        </dependency>

2、配置 yml文件

spring:
  ldap:
    urls: ldap://6.1.105.350:389
    base: dc=test,dc=com
    username: cn=service,dc=test,dc=com
    password: hidata!@#

3、开发配置类LdapConfiguration,初始化连接,创建LdapTemplate

package com.ruoyi.web.core.config;
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;
@Configuration
public class LdapConfiguration {
    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);
        contextSource.setPassword(password);
        //  解决乱码
        config.put("java.naming.ldap.attributes.binary", "objectGUID");
        contextSource.setPooled(true);
        contextSource.setBaseEnvironmentProperties(config);
        return contextSource;
    }
    @Bean
    public LdapTemplate ldapTemplate() {
        if (null == ldapTemplate) {
            ldapTemplate = new LdapTemplate(contextSource());
        }
        return ldapTemplate;
    }
}

4、测试类

package com.hidata.hidbm;
import com.ruoyi.RuoYiApplication;
import com.ruoyi.dbm.ldap.entity.DemoPerson;
import com.ruoyi.dbm.ldap.entity.Organization;
import com.ruoyi.dbm.ldap.entity.PersonAttributeMapper;
import com.ruoyi.dbm.ldap.entity.PersonRepository;
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.DirContextAdapter;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.naming.directory.DirContext;
import java.util.List;
import static org.springframework.ldap.query.LdapQueryBuilder.query;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class LdapTest {
    @Autowired
    private LdapTemplate ldapTemplate;
    @Autowired
    private PersonRepository personRepository;
    @Test
    public void findUser() {
        DirContextAdapter obj = (DirContextAdapter) ldapTemplate.lookup("uid=user01,ou=People");
        System.out.println(obj);
    }
    @Test
    public void t1(){
        Organization organization = ldapTemplate.findOne(query().where("ou").is("People"), Organization.class);
        System.out.println(organization);
    }
    @Test
    public void t2(){
        DirContextAdapter obj = (DirContextAdapter) ldapTemplate.lookup("uid=ben,ou=People");
        System.out.println(obj);
    }
    @Test
    public void t3(){
        List<String> list = ldapTemplate.search(query().where("objectClass").is("person"), (AttributesMapper<String>) attrs -> (String) attrs.get("cn").get());
        System.out.println(list);//[faysona, user01, service]
    }
    @Test
    public void t4(){
        List<String> list = ldapTemplate.search(query().where("objectClass").is("person"), new PersonAttributeMapper());
        System.out.println(list);
    }
}

打印结果

@Test
    public void t3(){
        List<String> list = ldapTemplate.search(query().where("objectClass").is("person"), (AttributesMapper<String>) attrs -> (String) attrs.get("cn").get());
        System.out.println(list);//[faysona, user01, service]
    }


相关文章
|
存储 安全 Java
SpringBoot实战(六)之使用LDAP验证用户
关于ubuntu16.04服务器安装配置LDAP参考链接为:https://www.howtoing.com/how-to-install-and-configure-openldap-and-phpldapadmin-on-ubuntu-16-04/ 本文主要讲LDAP相关的概念,普及相关知识和相关例子实战。
4038 0
SpringBoot实战(六)之使用LDAP验证用户
|
网络协议 Java 网络安全
基于Spring Boot的LDAP开发全教程
基于Spring Boot的LDAP开发全教程
3136 0
|
Java Spring
springboot操作LDAP,查询指定ou下面的cn属性
springboot操作LDAP,查询指定ou下面的cn属性
361 0
|
存储 网络协议 Java
Spring Boot中使用LDAP来统一管理用户信息
Spring Boot中使用LDAP来统一管理用户信息
530 0
|
存储 网络协议 安全
Spring Boot 2.x基础教程:使用LDAP来管理用户与组织数据
Spring Boot 2.x基础教程:使用LDAP来管理用户与组织数据
866 0
|
11月前
|
JavaScript Java 关系型数据库
基于springboot的项目管理系统
本文探讨项目管理系统在现代企业中的应用与实现,分析其研究背景、意义及现状,阐述基于SSM、Java、MySQL和Vue等技术构建系统的关键方法,展现其在提升管理效率、协同水平与风险管控方面的价值。
|
11月前
|
搜索推荐 JavaScript Java
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
11月前
|
JavaScript Java 关系型数据库
基于springboot的古树名木保护管理系统
本研究针对古树保护面临的严峻挑战,构建基于Java、Vue、MySQL与Spring Boot技术的信息化管理系统,实现古树资源的动态监测、数据管理与科学保护,推动生态、文化与经济可持续发展。