#251 - Add example for Spring Data LDAP.
This commit is contained in:
15
ldap/example/README.md
Normal file
15
ldap/example/README.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Spring Data LDAP - Example
|
||||||
|
|
||||||
|
This project contains samples of Spring Data (LDAP).
|
||||||
|
|
||||||
|
|
||||||
|
```java
|
||||||
|
public interface PersonRepository extends CrudRepository<Person, Name> {
|
||||||
|
|
||||||
|
List<Person> findByUid(String uid);
|
||||||
|
|
||||||
|
List<Person> findByLastnameStartsWith(String prefix);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The test cases in `PersonRepositoryIntegrationTests` show basic interaction to search, create and modify objects stored in a LDAP repository.
|
||||||
15
ldap/example/pom.xml
Normal file
15
ldap/example/pom.xml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<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>
|
||||||
|
|
||||||
|
<artifactId>spring-data-ldap-example</artifactId>
|
||||||
|
|
||||||
|
<name>Spring Data LDAP - Example</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.data.examples</groupId>
|
||||||
|
<artifactId>spring-data-ldap-examples</artifactId>
|
||||||
|
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package example.springdata.ldap;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test configuration to spin up a in-memory LDAP server (see {@code application.properties}). Also enables Spring Data
|
||||||
|
* repositories for LDAP.
|
||||||
|
*
|
||||||
|
* @author Mark Paluch
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
class ApplicationConfiguration {}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package example.springdata.ldap;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.naming.Name;
|
||||||
|
|
||||||
|
import org.springframework.ldap.odm.annotations.Attribute;
|
||||||
|
import org.springframework.ldap.odm.annotations.DnAttribute;
|
||||||
|
import org.springframework.ldap.odm.annotations.Entry;
|
||||||
|
import org.springframework.ldap.odm.annotations.Id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Person} object stored inside LDAP.
|
||||||
|
*
|
||||||
|
* @author Mark Paluch
|
||||||
|
*/
|
||||||
|
@Entry(base = "ou=people,dc=springframework,dc=org", objectClasses = "inetOrgPerson")
|
||||||
|
@Data
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
@Id private Name id;
|
||||||
|
|
||||||
|
@DnAttribute(value = "uid", index = 3) private String uid;
|
||||||
|
|
||||||
|
@Attribute(name = "cn") private String fullName;
|
||||||
|
|
||||||
|
@Attribute(name = "sn") private String lastname;
|
||||||
|
|
||||||
|
private String userPassword;
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package example.springdata.ldap;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.naming.Name;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repository interface to manage {@link Person} instances.
|
||||||
|
*
|
||||||
|
* @author Mark Paluch
|
||||||
|
*/
|
||||||
|
public interface PersonRepository extends CrudRepository<Person, Name> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find by UserId.
|
||||||
|
*
|
||||||
|
* @param uid
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<Person> findByUid(String uid);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prefix search on {@link Person#getLastname()}.
|
||||||
|
*
|
||||||
|
* @param prefix
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<Person> findByLastnameStartsWith(String prefix);
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package example.springdata.ldap;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.naming.InvalidNameException;
|
||||||
|
import javax.naming.ldap.LdapName;
|
||||||
|
|
||||||
|
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.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests for {@link PersonRepository}.
|
||||||
|
*
|
||||||
|
* @author Mark Paluch
|
||||||
|
*/
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class PersonRepositoryIntegrationTests {
|
||||||
|
|
||||||
|
@Autowired PersonRepository personRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a {@link Person} by its Id that is a full DN.
|
||||||
|
*
|
||||||
|
* @throws InvalidNameException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void findOneByName() throws InvalidNameException {
|
||||||
|
|
||||||
|
Person person = personRepository.findOne(new LdapName("uid=bob,ou=people,dc=springframework,dc=org"));
|
||||||
|
|
||||||
|
assertThat(person).isNotNull();
|
||||||
|
assertThat(person.getFullName()).isEqualTo("Bob Hamilton");
|
||||||
|
assertThat(person.getLastname()).isEqualTo("Hamilton");
|
||||||
|
assertThat(person.getUid()).isEqualTo("bob");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all entries in the base path.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void findAll() {
|
||||||
|
|
||||||
|
Iterable<Person> people = personRepository.findAll();
|
||||||
|
|
||||||
|
assertThat(people).hasSize(3).extracting("uid").contains("bob", "joe", "ben");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all {@link Person} objects starting with {@code Ham} in the field {@code lastname}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void findByLastname() {
|
||||||
|
|
||||||
|
List<Person> people = personRepository.findByLastnameStartsWith("Ham");
|
||||||
|
|
||||||
|
assertThat(people).hasSize(1).extracting("uid").contains("bob");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add and remove a user to the LDAP repository.
|
||||||
|
*
|
||||||
|
* @throws InvalidNameException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void addUser() throws InvalidNameException {
|
||||||
|
|
||||||
|
Person walter = new Person();
|
||||||
|
walter.setFullName("Walter White");
|
||||||
|
walter.setUid("heisenberg");
|
||||||
|
walter.setLastname("White");
|
||||||
|
|
||||||
|
personRepository.save(walter);
|
||||||
|
|
||||||
|
List<Person> people = personRepository.findByUid("heisenberg");
|
||||||
|
|
||||||
|
assertThat(people).hasSize(1).extracting("fullName").contains("Walter White");
|
||||||
|
|
||||||
|
personRepository.delete(people.get(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
/**
|
||||||
|
* Package showing usage of Spring Data LDAP Repositories.
|
||||||
|
*/
|
||||||
|
package example.springdata.ldap;
|
||||||
3
ldap/example/src/test/resources/application.properties
Normal file
3
ldap/example/src/test/resources/application.properties
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
spring.ldap.embedded.ldif=test-server.ldif
|
||||||
|
spring.ldap.embedded.base-dn=dc=springframework,dc=org
|
||||||
|
spring.ldap.base=dc=springframework,dc=org
|
||||||
48
ldap/example/src/test/resources/test-server.ldif
Normal file
48
ldap/example/src/test/resources/test-server.ldif
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
dn: dc=springframework,dc=org
|
||||||
|
objectClass: top
|
||||||
|
objectClass: domain
|
||||||
|
|
||||||
|
dn: ou=groups,dc=springframework,dc=org
|
||||||
|
objectclass: top
|
||||||
|
objectclass: organizationalUnit
|
||||||
|
ou: groups
|
||||||
|
|
||||||
|
dn: ou=people,dc=springframework,dc=org
|
||||||
|
objectclass: top
|
||||||
|
objectclass: organizationalUnit
|
||||||
|
ou: people
|
||||||
|
|
||||||
|
dn: ou=otherpeople,dc=springframework,dc=org
|
||||||
|
objectclass: top
|
||||||
|
objectclass: organizationalUnit
|
||||||
|
ou: otherpeople
|
||||||
|
|
||||||
|
dn: uid=ben,ou=people,dc=springframework,dc=org
|
||||||
|
objectclass: top
|
||||||
|
objectclass: person
|
||||||
|
objectclass: organizationalPerson
|
||||||
|
objectclass: inetOrgPerson
|
||||||
|
cn: Ben Alex
|
||||||
|
sn: Alex
|
||||||
|
uid: ben
|
||||||
|
userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=
|
||||||
|
|
||||||
|
dn: uid=bob,ou=people,dc=springframework,dc=org
|
||||||
|
objectclass: top
|
||||||
|
objectclass: person
|
||||||
|
objectclass: organizationalPerson
|
||||||
|
objectclass: inetOrgPerson
|
||||||
|
cn: Bob Hamilton
|
||||||
|
sn: Hamilton
|
||||||
|
uid: bob
|
||||||
|
userPassword: bobspassword
|
||||||
|
|
||||||
|
dn: uid=joe,ou=otherpeople,dc=springframework,dc=org
|
||||||
|
objectclass: top
|
||||||
|
objectclass: person
|
||||||
|
objectclass: organizationalPerson
|
||||||
|
objectclass: inetOrgPerson
|
||||||
|
cn: Joe Smeth
|
||||||
|
sn: Smeth
|
||||||
|
uid: joe
|
||||||
|
userPassword: joespassword
|
||||||
43
ldap/pom.xml
Normal file
43
ldap/pom.xml
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<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>
|
||||||
|
|
||||||
|
<artifactId>spring-data-ldap-examples</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.data.examples</groupId>
|
||||||
|
<artifactId>spring-data-examples</artifactId>
|
||||||
|
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<name>Spring Data LDAP - Examples</name>
|
||||||
|
<description>Sample projects for Spring Data LDAP</description>
|
||||||
|
<url>http://projects.spring.io/spring-data-ldap</url>
|
||||||
|
<inceptionYear>2016-2017</inceptionYear>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>example</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-ldap</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.unboundid</groupId>
|
||||||
|
<artifactId>unboundid-ldapsdk</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
Reference in New Issue
Block a user