zoukankan      html  css  js  c++  java
  • SpringBoot:整合LDAP

    依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-ldap</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    

    配置

    application.yml

    spring:
      ldap:
        urls: ldap://192.168.1.53:389
        username: cn=Manager,${spring.ldap.base}
        password: hadoop
        base: dc=haohaozhu,dc=com
    

    实体类和Dao

    /**
     * @author wen.jie
     * @date 2021/5/8 12:31
     */
    @Data@ToString
    @Entry(base = "ou=people,dc=haohaozhu,dc=com", objectClasses = "inetOrgPerson")
    public class Person {
    
        @Id
        private Name id;
        @DnAttribute(value = "uid")
        private String uid;
        @Attribute(name = "cn")
        private String cn;
        @Attribute(name = "sn")
        private String sn;
        @Attribute(name="mail")
        private String mail;
        @Attribute(name = "homedirectory")
        private String homedirectory;
        @Attribute(name = "gidnumber")
        private String gidnumber;
        @Attribute(name = "uidnumber")
        private String uidnumber;
    
    }
    
    public interface PersonRepository extends LdapRepository<Person> {
    }
    

    测试

    @SpringBootTest
    class BootLdapApplicationTests {
    
        @Autowired
        private PersonRepository personRepository;
    
        @Autowired
        private LdapTemplate template;
    
        @Test
        public void findAll() {
            personRepository.findAll().forEach(System.out::println);
        }
    
        @Test
        public void findAll2() {
            Person person = template.findOne(LdapQueryBuilder.query().where("uid").is("ldapuser2"), Person.class);
            System.out.println(person);
        }
    
        @Test
        public void authenticationTest() {
            String uid = "ldapuser2";
            Person authenticate = template.authenticate(
                    LdapQueryBuilder.query().where("uid").is(uid),
                    "hadoop",
                    (dirContext, ldapEntryIdentification) ->
                            template.findOne(LdapQueryBuilder.query().where("uid").is(uid), Person.class));
            System.out.println(authenticate);
        }
    }
    

    findAll:

    image-20210508134514490

    findAll2:

    image-20210508134535472

    authenticationTest:

    image-20210508134602142

  • 相关阅读:
    Avoiding first chance exception messages when the exception is safely handled
    网上邻居解决方法
    ******Can anyone explain me what do you mean by CreateControl
    一個界面設計比較好的外國網站
    How to create a Vertical ProgressBar
    bat如何批量删除指定部分文件夹名的文件夹
    ColumnView.ShownEditor Event
    Apache+php+mysql官方网站下载地址
    根据JAVA的POJO类生成AS3的VALUEOBJECT类
    Spring BlazeDS Integration简介与入门
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/14744639.html
Copyright © 2011-2022 走看看