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相关的概念,普及相关知识和相关例子实战。
3489 0
|
9月前
|
网络协议 Java 网络安全
基于Spring Boot的LDAP开发全教程
基于Spring Boot的LDAP开发全教程
728 0
|
3月前
|
Java Spring
springboot操作LDAP,查询指定ou下面的cn属性
springboot操作LDAP,查询指定ou下面的cn属性
|
存储 网络协议 Java
Spring Boot中使用LDAP来统一管理用户信息
Spring Boot中使用LDAP来统一管理用户信息
348 0
|
存储 网络协议 安全
Spring Boot 2.x基础教程:使用LDAP来管理用户与组织数据
Spring Boot 2.x基础教程:使用LDAP来管理用户与组织数据
549 0
|
4天前
|
SQL JavaScript 前端开发
vue中使用分页组件、将从数据库中查询出来的数据分页展示(前后端分离SpringBoot+Vue)
这篇文章详细介绍了如何在Vue.js中使用分页组件展示从数据库查询出来的数据,包括前端Vue页面的表格和分页组件代码,以及后端SpringBoot的控制层和SQL查询语句。
vue中使用分页组件、将从数据库中查询出来的数据分页展示(前后端分离SpringBoot+Vue)
|
3天前
|
前端开发 JavaScript Java
SpringBoot+Vue+token实现(表单+图片)上传、图片地址保存到数据库。上传图片保存位置自己定义、图片可以在前端回显(一))
这篇文章详细介绍了在SpringBoot+Vue项目中实现表单和图片上传的完整流程,包括前端上传、后端接口处理、数据库保存图片路径,以及前端图片回显的方法,同时探讨了图片资源映射、token验证、过滤器配置等相关问题。
|
3天前
|
前端开发 数据库
SpringBoot+Vue+token实现(表单+图片)上传、图片地址保存到数据库。上传图片保存位置到项目中的静态资源下、图片可以在前端回显(二))
这篇文章是关于如何在SpringBoot+Vue+token的环境下实现表单和图片上传的优化篇,主要改进是将图片保存位置从磁盘指定位置改为项目中的静态资源目录,使得图片资源可以跨环境访问,并在前端正确回显。
|
3天前
|
前端开发 数据库
SpringBoot+Vue实现商品不能重复加入购物车、购物车中展示商品的信息、删除商品重点提示等操作。如何点击图片实现图片放大
这篇文章介绍了如何在SpringBoot+Vue框架下实现购物车功能,包括防止商品重复加入、展示商品信息、删除商品时的提示,以及点击图片放大的前端实现。
SpringBoot+Vue实现商品不能重复加入购物车、购物车中展示商品的信息、删除商品重点提示等操作。如何点击图片实现图片放大