Spring LDAP + Spring Boot Embedded LDAP Configuration Example
In this tutorial we demonstrate how to use spring boot and spring ldap to configure an embedded LDAP server.
LDAP (Lightweight Directory Access Protocol) is an open, vendor-neutral, industry standard application for accesing and maintaining distributed directory information services over an IP network.
Spring LDAP makes it easy to build spring based applications that use the Lightweight Directory Access Protocol. Spring Boot offers auto-configuration for any compliant LDAP server as well as support for the embedded in-memory LDAP server.
Maven Configuration
We use Apache Maven to manage our project dependencies. Make sure the following dependencies are added to your project. To use an embedded ldap server, make sure the com.unboundid:unboundid-ldapsdk resides on the class-path.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.memorynotfound.ldap.configuration</groupId> <artifactId>spring-boot</artifactId> <version>1.0.0-SNAPSHOT</version> <url>https://memorynotfound.com</url> <name>Spring LDAP - ${project.artifactId}</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-ldap</artifactId> </dependency> <dependency> <groupId>com.unboundid</groupId> <artifactId>unboundid-ldapsdk</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
Spring LDAP + Spring Boot Embedded LDAP Configuration
In this example we configure an embedded ldap server. We can configure and create the embedded ldap server using the application.properties or the application.yml files.
application.yml
# Spring Boot + Spring LDAP configuration application.yml spring: ldap: # Spring LDAP # # In this example we use an embedded ldap server. When using a real one, # you can configure the settings here. # # urls: ldap://localhost:12345 # base: dc=memorynotfound,dc=com # username: uid=admin # password: secret # Embedded Spring LDAP embedded: base-dn: dc=memorynotfound,dc=com credential: username: uid=admin password: secret ldif: classpath:schema.ldif port: 12345 validation: enabled: false
application.properties
# Spring Boot + Spring LDAP configuration application.properties # Spring LDAP # # In this example we use an embedded ldap server. When using a real one, # you can configure the settings here. # # urls: ldap://localhost:12345 # base: dc=memorynotfound,dc=com # username: uid=admin # password: secret # spring.ldap.urls= ldap://localhost:12345 # spring.ldap.base= dc=memorynotfound,dc=com # spring.ldap.username= uid=admin # spring.ldap.password= secret # Embedded Spring LDAP spring.ldap.embedded.base-dn= dc=memorynotfound,dc=com spring.ldap.embedded.credential.username= uid=admin spring.ldap.embedded.credential.password= secret spring.ldap.embedded.ldif=classpath:schema.ldif spring.ldap.embedded.port= 12345 spring.ldap.embedded.validation.enabled=false
Populate LDAP with LDIF
We can populate the embedded LDAP server using a .ldif file. The following file populates the embedded LDAP server with organizational units, persons and groups.
dn: dc=memorynotfound,dc=com objectclass: top objectclass: domain objectclass: extensibleObject dc: memorynotfound # Organizational Units dn: ou=groups,dc=memorynotfound,dc=com objectclass: top objectclass: organizationalUnit ou: groups dn: ou=people,dc=memorynotfound,dc=com objectclass: top objectclass: organizationalUnit ou: people # Create People dn: uid=john,ou=people,dc=memorynotfound,dc=com objectclass: top objectclass: person objectclass: organizationalPerson objectclass: inetOrgPerson cn: John Doe sn: John uid: john password: secret dn: uid=jihn,ou=people,dc=memorynotfound,dc=com objectclass: top objectclass: person objectclass: organizationalPerson objectclass: inetOrgPerson cn: Jihn Die sn: Jihn uid: jihn password: secret dn: uid=jahn,ou=people,dc=memorynotfound,dc=com objectclass: top objectclass: person objectclass: organizationalPerson objectclass: inetOrgPerson cn: Jahn Dae sn: Jahn uid: jahn password: secret # Create Groups dn: cn=developers,ou=groups,dc=memorynotfound,dc=com objectclass: top objectclass: groupOfUniqueNames cn: developers ou: developer uniqueMember: uid=john,ou=people,dc=memorynotfound,dc=com uniqueMember: uid=jihn,ou=people,dc=memorynotfound,dc=com dn: cn=managers,ou=groups,dc=memorynotfound,dc=com objectclass: top objectclass: groupOfUniqueNames cn: managers ou: manager uniqueMember: uid=jahn,ou=people,dc=memorynotfound,dc=com
Bootstrap Application
We use spring boot to bootstrap our application. Spring boot configures the embedded ldap server and populates it with corresponding entries found in the .ldif file. After our application is initialized we invoke a simple method, that retrieves all the person names inside the ldap.
package com.memorynotfound.ldap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.annotation.PostConstruct; import java.util.List; @SpringBootApplication public class Application { private static Logger log = LoggerFactory.getLogger(Application.class); @Autowired private PersonRepository personRepository; public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } @PostConstruct public void setup(){ log.info("Spring LDAP + Spring Boot Configuration Example"); List<String> names = personRepository.getAllPersonNames(); log.info("names: " + names); System.exit(-1); } }
Example LDAP Query
We can use the LdapTemplate to perform LdapQueries on the LDAP server. In the following example we simply retrieve a list of persons.
package com.memorynotfound.ldap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; import org.springframework.stereotype.Service; import javax.naming.NamingException; import javax.naming.directory.Attributes; import java.util.List; import static org.springframework.ldap.query.LdapQueryBuilder.query; @Service public class PersonRepository { @Autowired private LdapTemplate ldapTemplate; /** * Retrieves all the persons in the ldap server * @return list of person names */ public List<String> getAllPersonNames() { return ldapTemplate.search( query().where("objectclass").is("person"), new AttributesMapper<String>() { public String mapFromAttributes(Attributes attrs) throws NamingException { return (String) attrs.get("cn").get(); } }); } }
Output
When we run the application, the following output is printed to the console.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.7.RELEASE) [main] com.memorynotfound.ldap.Application: Spring LDAP + Spring Boot Configuration Example [main] com.memorynotfound.ldap.Application: names: [Jahn Dae, Jihn Die, John Doe]
Download
From:一号门
COMMENTS