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]
    }
Logo

苏州本地的技术开发者社区,在这里可以交流本地的好吃好玩的,可以交流技术,可以交流招聘等等,没啥限制。

更多推荐