Commit 10de30ff authored by Phillip Webb's avatar Phillip Webb

Polish LDAP contribution

See gh-7733
parent 6a84c369
...@@ -31,8 +31,7 @@ import org.springframework.data.repository.config.RepositoryConfigurationExtensi ...@@ -31,8 +31,7 @@ import org.springframework.data.repository.config.RepositoryConfigurationExtensi
* @author Eddú Meléndez * @author Eddú Meléndez
* @since 1.5.0 * @since 1.5.0
*/ */
class LdapRepositoriesRegistrar class LdapRepositoriesRegistrar extends AbstractRepositoryConfigurationSourceSupport {
extends AbstractRepositoryConfigurationSourceSupport {
@Override @Override
protected Class<? extends Annotation> getAnnotation() { protected Class<? extends Annotation> getAnnotation() {
......
...@@ -39,27 +39,26 @@ import org.springframework.ldap.core.support.LdapContextSource; ...@@ -39,27 +39,26 @@ import org.springframework.ldap.core.support.LdapContextSource;
@EnableConfigurationProperties(LdapProperties.class) @EnableConfigurationProperties(LdapProperties.class)
public class LdapAutoConfiguration { public class LdapAutoConfiguration {
private LdapProperties properties; private final LdapProperties properties;
private Environment environment; private final Environment environment;
public LdapAutoConfiguration(LdapProperties properties, public LdapAutoConfiguration(LdapProperties properties, Environment environment) {
Environment environment) {
this.properties = properties; this.properties = properties;
this.environment = environment; this.environment = environment;
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ContextSource contextSource() { public ContextSource ldapContextSource() {
LdapContextSource contextSource = new LdapContextSource(); LdapContextSource source = new LdapContextSource();
contextSource.setUserDn(this.properties.getUsername()); source.setUserDn(this.properties.getUsername());
contextSource.setPassword(this.properties.getPassword()); source.setPassword(this.properties.getPassword());
contextSource.setBase(this.properties.getBase()); source.setBase(this.properties.getBase());
contextSource.setUrls(this.properties.determineUrls(this.environment)); source.setUrls(this.properties.determineUrls(this.environment));
contextSource.setBaseEnvironmentProperties(Collections source.setBaseEnvironmentProperties(Collections
.<String, Object>unmodifiableMap(this.properties.getBaseEnvironment())); .<String, Object>unmodifiableMap(this.properties.getBaseEnvironment()));
return contextSource; return source;
} }
} }
...@@ -22,6 +22,9 @@ import java.util.Map; ...@@ -22,6 +22,9 @@ import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.LdapTemplate;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/** /**
* Configuration properties to configure {@link LdapTemplate}. * Configuration properties to configure {@link LdapTemplate}.
* *
...@@ -36,7 +39,7 @@ public class LdapProperties { ...@@ -36,7 +39,7 @@ public class LdapProperties {
/** /**
* LDAP urls. * LDAP urls.
*/ */
private String[] urls = new String[0]; private String[] urls;
/** /**
* Base suffix from which all operations should originate. * Base suffix from which all operations should originate.
...@@ -99,29 +102,19 @@ public class LdapProperties { ...@@ -99,29 +102,19 @@ public class LdapProperties {
} }
public String[] determineUrls(Environment environment) { public String[] determineUrls(Environment environment) {
if (this.urls.length == 0) { if (ObjectUtils.isEmpty(this.urls)) {
String protocol = "ldap://"; return new String[] { "ldap://localhost:" + determinePort(environment) };
String host = "localhost";
int port = determinePort(environment);
String[] ldapUrls = new String[1];
ldapUrls[0] = protocol + host + ":" + port;
return ldapUrls;
} }
return this.urls; return this.urls;
} }
private int determinePort(Environment environment) { private int determinePort(Environment environment) {
if (environment != null) { Assert.state(environment != null, "No local LDAP port configured");
String localPort = environment.getProperty("local.ldap.port"); String localPort = environment.getProperty("local.ldap.port");
if (localPort != null) { if (localPort != null) {
return Integer.valueOf(localPort); return Integer.valueOf(localPort);
}
else {
return DEFAULT_PORT;
}
} }
throw new IllegalStateException( return DEFAULT_PORT;
"No local ldap port configuration is available");
} }
} }
...@@ -16,9 +16,6 @@ ...@@ -16,9 +16,6 @@
package org.springframework.boot.autoconfigure.ldap.embedded; package org.springframework.boot.autoconfigure.ldap.embedded;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -30,14 +27,15 @@ import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; ...@@ -30,14 +27,15 @@ import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig; import com.unboundid.ldap.listener.InMemoryListenerConfig;
import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldif.LDIFReader; import com.unboundid.ldif.LDIFReader;
import org.apache.commons.io.IOUtils;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration; import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration;
import org.springframework.boot.autoconfigure.ldap.LdapProperties; import org.springframework.boot.autoconfigure.ldap.LdapProperties;
import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapProperties.Credential;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
...@@ -63,22 +61,24 @@ import org.springframework.util.StringUtils; ...@@ -63,22 +61,24 @@ import org.springframework.util.StringUtils;
@EnableConfigurationProperties({ LdapProperties.class, EmbeddedLdapProperties.class }) @EnableConfigurationProperties({ LdapProperties.class, EmbeddedLdapProperties.class })
@AutoConfigureBefore(LdapAutoConfiguration.class) @AutoConfigureBefore(LdapAutoConfiguration.class)
@ConditionalOnClass(InMemoryDirectoryServer.class) @ConditionalOnClass(InMemoryDirectoryServer.class)
@ConditionalOnProperty(prefix = "spring.ldap.embedded", name = "base-dn")
public class EmbeddedLdapAutoConfiguration { public class EmbeddedLdapAutoConfiguration {
private InMemoryDirectoryServer server; private static final String PROPERTY_SOURCE_NAME = "ldap.ports";
private final EmbeddedLdapProperties embeddedProperties;
private EmbeddedLdapProperties embeddedProperties; private final LdapProperties properties;
private LdapProperties properties; private final ConfigurableApplicationContext applicationContext;
private ConfigurableApplicationContext applicationContext; private final Environment environment;
private Environment environment; private InMemoryDirectoryServer server;
public EmbeddedLdapAutoConfiguration(EmbeddedLdapProperties embeddedProperties, public EmbeddedLdapAutoConfiguration(EmbeddedLdapProperties embeddedProperties,
LdapProperties properties, LdapProperties properties, ConfigurableApplicationContext applicationContext,
ConfigurableApplicationContext applicationContext, Environment environment) {
Environment environment) {
this.embeddedProperties = embeddedProperties; this.embeddedProperties = embeddedProperties;
this.properties = properties; this.properties = properties;
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
...@@ -88,100 +88,83 @@ public class EmbeddedLdapAutoConfiguration { ...@@ -88,100 +88,83 @@ public class EmbeddedLdapAutoConfiguration {
@Bean @Bean
@DependsOn("directoryServer") @DependsOn("directoryServer")
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ContextSource contextSource() { public ContextSource ldapContextSource() {
LdapContextSource contextSource = new LdapContextSource(); LdapContextSource source = new LdapContextSource();
if (hasCredentials(this.embeddedProperties.getCredential())) {
EmbeddedLdapProperties.Credential credential = this.embeddedProperties source.setUserDn(this.embeddedProperties.getCredential().getUsername());
.getCredential(); source.setPassword(this.embeddedProperties.getCredential().getPassword());
if (StringUtils.hasText(credential.getUsername()) &&
StringUtils.hasText(credential.getPassword())) {
contextSource.setUserDn(credential.getUsername());
contextSource.setPassword(credential.getPassword());
} }
contextSource.setUrls(this.properties.determineUrls(this.environment)); source.setUrls(this.properties.determineUrls(this.environment));
return contextSource; return source;
} }
@Bean @Bean
public InMemoryDirectoryServer directoryServer() throws LDAPException { public InMemoryDirectoryServer directoryServer() throws LDAPException {
InMemoryDirectoryServerConfig config = InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(
new InMemoryDirectoryServerConfig(this.embeddedProperties this.embeddedProperties.getBaseDn());
.getPartitionSuffix()); if (hasCredentials(this.embeddedProperties.getCredential())) {
config.addAdditionalBindCredentials(
EmbeddedLdapProperties.Credential credential = this.embeddedProperties this.embeddedProperties.getCredential().getUsername(),
.getCredential(); this.embeddedProperties.getCredential().getPassword());
if (StringUtils.hasText(credential.getUsername()) &&
StringUtils.hasText(credential.getPassword())) {
config.addAdditionalBindCredentials(credential
.getUsername(), credential.getPassword());
} }
InMemoryListenerConfig listenerConfig = InMemoryListenerConfig
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", .createLDAPConfig("LDAP", this.embeddedProperties.getPort());
this.embeddedProperties.getPort())); config.setListenerConfigs(listenerConfig);
this.server = new InMemoryDirectoryServer(config); this.server = new InMemoryDirectoryServer(config);
importLdif();
populateDirectoryServer();
this.server.startListening(); this.server.startListening();
publishPortInfo(this.server.getListenPort()); setPortProperty(this.applicationContext, this.server.getListenPort());
return this.server; return this.server;
} }
private void publishPortInfo(int port) { private boolean hasCredentials(Credential credential) {
setPortProperty(this.applicationContext, port); return StringUtils.hasText(credential.getUsername())
} && StringUtils.hasText(credential.getPassword());
private void setPortProperty(ApplicationContext currentContext,
int port) {
if (currentContext instanceof ConfigurableApplicationContext) {
MutablePropertySources sources = ((ConfigurableApplicationContext)
currentContext).getEnvironment().getPropertySources();
getLdapPorts(sources).put("local.ldap.port", port);
}
if (currentContext.getParent() != null) {
setPortProperty(currentContext.getParent(), port);
}
}
private Map<String, Object> getLdapPorts(MutablePropertySources sources) {
PropertySource<?> propertySource = sources.get("ldap.ports");
if (propertySource == null) {
propertySource = new MapPropertySource("ldap.ports",
new HashMap<String, Object>());
sources.addFirst(propertySource);
}
return (Map<String, Object>) propertySource.getSource();
} }
private void populateDirectoryServer() throws LDAPException { private void importLdif() throws LDAPException {
String location = this.embeddedProperties.getLdif(); String location = this.embeddedProperties.getLdif();
if (StringUtils.hasText(location)) { if (StringUtils.hasText(location)) {
try { try {
Resource resource = this.applicationContext.getResource( Resource resource = this.applicationContext.getResource(location);
this.embeddedProperties.getLdif());
if (resource.exists()) { if (resource.exists()) {
File tempFile = File.createTempFile("ldap_test_data", ".ldif"); InputStream inputStream = resource.getInputStream();
try { try {
InputStream inputStream = resource.getInputStream(); this.server.importFromLDIF(true, new LDIFReader(inputStream));
IOUtils.copy(inputStream, new FileOutputStream(tempFile));
this.server.importFromLDIF(true, new LDIFReader(tempFile));
}
catch (LDAPException e) {
e.printStackTrace();
} }
finally { finally {
tempFile.delete(); inputStream.close();
} }
} }
} }
catch (IOException ex) { catch (Exception ex) {
throw new IllegalStateException( throw new IllegalStateException("Unable to load LDIF " + location, ex);
"Unable to load resource from " + location, ex);
} }
} }
} }
private void setPortProperty(ApplicationContext context, int port) {
if (context instanceof ConfigurableApplicationContext) {
MutablePropertySources sources = ((ConfigurableApplicationContext) context)
.getEnvironment().getPropertySources();
getLdapPorts(sources).put("local.ldap.port", port);
}
if (context.getParent() != null) {
setPortProperty(context.getParent(), port);
}
}
@SuppressWarnings("unchecked")
private Map<String, Object> getLdapPorts(MutablePropertySources sources) {
PropertySource<?> propertySource = sources.get(PROPERTY_SOURCE_NAME);
if (propertySource == null) {
propertySource = new MapPropertySource(PROPERTY_SOURCE_NAME,
new HashMap<String, Object>());
sources.addFirst(propertySource);
}
return (Map<String, Object>) propertySource.getSource();
}
@PreDestroy @PreDestroy
public void close() { public void close() {
if (this.server != null) { if (this.server != null) {
......
...@@ -38,14 +38,14 @@ public class EmbeddedLdapProperties { ...@@ -38,14 +38,14 @@ public class EmbeddedLdapProperties {
private Credential credential = new Credential(); private Credential credential = new Credential();
/** /**
* LDAP partition suffix. * Base DNs.
*/ */
private String partitionSuffix; private String baseDn;
/** /**
* Schema (LDIF) script resource reference. * Schema (LDIF) script resource reference.
*/ */
private String ldif; private String ldif = "classpath:schema.ldif";
public int getPort() { public int getPort() {
return this.port; return this.port;
...@@ -63,12 +63,12 @@ public class EmbeddedLdapProperties { ...@@ -63,12 +63,12 @@ public class EmbeddedLdapProperties {
this.credential = credential; this.credential = credential;
} }
public String getPartitionSuffix() { public String getBaseDn() {
return this.partitionSuffix; return this.baseDn;
} }
public void setPartitionSuffix(String partitionSuffix) { public void setBaseDn(String baseDn) {
this.partitionSuffix = partitionSuffix; this.baseDn = baseDn;
} }
public String getLdif() { public String getLdif() {
......
...@@ -22,4 +22,5 @@ import org.springframework.boot.autoconfigure.data.ldap.person.Person; ...@@ -22,4 +22,5 @@ import org.springframework.boot.autoconfigure.data.ldap.person.Person;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
public interface PersonLdapRepository extends Repository<Person, Name> { public interface PersonLdapRepository extends Repository<Person, Name> {
} }
...@@ -49,8 +49,7 @@ public class LdapDataAutoConfigurationTests { ...@@ -49,8 +49,7 @@ public class LdapDataAutoConfigurationTests {
this.context.register(PropertyPlaceholderAutoConfiguration.class, this.context.register(PropertyPlaceholderAutoConfiguration.class,
LdapAutoConfiguration.class, LdapDataAutoConfiguration.class); LdapAutoConfiguration.class, LdapDataAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(LdapTemplate.class).length) assertThat(this.context.getBeanNamesForType(LdapTemplate.class)).hasSize(1);
.isEqualTo(1);
} }
} }
...@@ -52,16 +52,13 @@ public class LdapRepositoriesAutoConfigurationTests { ...@@ -52,16 +52,13 @@ public class LdapRepositoriesAutoConfigurationTests {
@Test @Test
public void testDefaultRepositoryConfiguration() throws Exception { public void testDefaultRepositoryConfiguration() throws Exception {
load(TestConfiguration.class); load(TestConfiguration.class);
assertThat(this.context.getBean(PersonRepository.class)).isNotNull(); assertThat(this.context.getBean(PersonRepository.class)).isNotNull();
} }
@Test @Test
public void testNoRepositoryConfiguration() throws Exception { public void testNoRepositoryConfiguration() throws Exception {
load(EmptyConfiguration.class); load(EmptyConfiguration.class);
assertThat(this.context.getBeanNamesForType(PersonRepository.class)).isEmpty();
assertThat(this.context.getBeanNamesForType(PersonRepository.class).length)
.isEqualTo(0);
} }
@Test @Test
...@@ -76,8 +73,7 @@ public class LdapRepositoriesAutoConfigurationTests { ...@@ -76,8 +73,7 @@ public class LdapRepositoriesAutoConfigurationTests {
"spring.ldap.urls:ldap://localhost:389"); "spring.ldap.urls:ldap://localhost:389");
this.context.register(configurationClasses); this.context.register(configurationClasses);
this.context.register(LdapAutoConfiguration.class, this.context.register(LdapAutoConfiguration.class,
LdapDataAutoConfiguration.class, LdapDataAutoConfiguration.class, LdapRepositoriesAutoConfiguration.class,
LdapRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
} }
......
...@@ -23,7 +23,7 @@ import org.springframework.ldap.odm.annotations.DnAttribute; ...@@ -23,7 +23,7 @@ import org.springframework.ldap.odm.annotations.DnAttribute;
import org.springframework.ldap.odm.annotations.Entry; import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id; import org.springframework.ldap.odm.annotations.Id;
@Entry(objectClasses = {"person", "top"}, base = "ou=someOu") @Entry(objectClasses = { "person", "top" }, base = "ou=someOu")
public class Person { public class Person {
@Id @Id
......
...@@ -52,52 +52,44 @@ public class LdapAutoConfigurationTests { ...@@ -52,52 +52,44 @@ public class LdapAutoConfigurationTests {
@Test @Test
public void testDefaultUrl() { public void testDefaultUrl() {
load(); load();
assertThat(this.context.getBeanNamesForType(ContextSource.class).length) ContextSource contextSource = this.context.getBean(ContextSource.class);
.isEqualTo(1); String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, "urls");
assertThat(ReflectionTestUtils.getField(this.context.getBean(ContextSource.class), assertThat(urls).containsExactly("ldap://localhost:389");
"urls")).isEqualTo(new String[]{"ldap://localhost:389"});
} }
@Test @Test
public void testContextSourceSetOneUrl() { public void testContextSourceSetOneUrl() {
load("spring.ldap.urls:ldap://localhost:123"); load("spring.ldap.urls:ldap://localhost:123");
assertThat(this.context.getBeanNamesForType(ContextSource.class).length) ContextSource contextSource = this.context.getBean(ContextSource.class);
.isEqualTo(1); String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, "urls");
assertThat(ReflectionTestUtils.getField(this.context.getBean(ContextSource.class), assertThat(urls).containsExactly("ldap://localhost:123");
"urls")).isEqualTo(new String[]{"ldap://localhost:123"});
} }
@Test @Test
public void testContextSourceSetTwoUrls() { public void testContextSourceSetTwoUrls() {
load("spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123"); load("spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123");
assertThat(this.context.getBeanNamesForType(ContextSource.class).length) ContextSource contextSource = this.context.getBean(ContextSource.class);
.isEqualTo(1); LdapProperties ldapProperties = this.context.getBean(LdapProperties.class);
assertThat(this.context.getBean(LdapProperties.class).getUrls().length) String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, "urls");
.isEqualTo(2); assertThat(urls).containsExactly("ldap://localhost:123", "ldap://mycompany:123");
assertThat(ReflectionTestUtils.getField(this.context.getBean(ContextSource.class), assertThat(ldapProperties.getUrls()).hasSize(2);
"urls"))
.isEqualTo(new String[]{"ldap://localhost:123", "ldap://mycompany:123"});
} }
@Test @Test
public void testContextSourceWithMoreProperties() { public void testContextSourceWithMoreProperties() {
load("spring.ldap.urls:ldap://localhost:123", load("spring.ldap.urls:ldap://localhost:123", "spring.ldap.username:root",
"spring.ldap.username:root", "spring.ldap.password:root", "spring.ldap.base:cn=SpringDevelopers",
"spring.ldap.password:root", "spring.ldap.baseEnvironment.java.naming.security"
"spring.ldap.base:cn=SpringDevelopers", + ".authentication:DIGEST-MD5");
"spring.ldap.baseEnvironment.java.naming.security" + LdapProperties ldapProperties = this.context.getBean(LdapProperties.class);
".authentication:DIGEST-MD5"); assertThat(ldapProperties.getBaseEnvironment())
assertThat(this.context.getBeanNamesForType(ContextSource.class).length)
.isEqualTo(1);
assertThat(this.context.getBean(LdapProperties.class).getBaseEnvironment())
.containsEntry("java.naming.security.authentication", "DIGEST-MD5"); .containsEntry("java.naming.security.authentication", "DIGEST-MD5");
} }
private void load(String... properties) { private void load(String... properties) {
EnvironmentTestUtils.addEnvironment(this.context, properties); EnvironmentTestUtils.addEnvironment(this.context, properties);
this.context this.context.register(LdapAutoConfiguration.class,
.register(LdapAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
} }
......
...@@ -61,7 +61,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -61,7 +61,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testSetDefaultPort() throws LDAPException { public void testSetDefaultPort() throws LDAPException {
load("spring.ldap.embedded.port:1234", load("spring.ldap.embedded.port:1234",
"spring.ldap.embedded.partitionSuffix:dc=spring,dc=org"); "spring.ldap.embedded.base-dn:dc=spring,dc=org");
InMemoryDirectoryServer server = this.context InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
assertThat(server.getListenPort()).isEqualTo(1234); assertThat(server.getListenPort()).isEqualTo(1234);
...@@ -69,7 +69,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -69,7 +69,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testRandomPortWithEnvironment() throws LDAPException { public void testRandomPortWithEnvironment() throws LDAPException {
load("spring.ldap.embedded.partitionSuffix:dc=spring,dc=org"); load("spring.ldap.embedded.base-dn:dc=spring,dc=org");
InMemoryDirectoryServer server = this.context InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
assertThat(server.getListenPort()).isEqualTo(this.context.getEnvironment() assertThat(server.getListenPort()).isEqualTo(this.context.getEnvironment()
...@@ -79,21 +79,19 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -79,21 +79,19 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testRandomPortWithValueAnnotation() throws LDAPException { public void testRandomPortWithValueAnnotation() throws LDAPException {
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.ldap.embedded.partitionSuffix:dc=spring,dc=org"); "spring.ldap.embedded.base-dn:dc=spring,dc=org");
this.context.register(EmbeddedLdapAutoConfiguration.class, this.context.register(EmbeddedLdapAutoConfiguration.class,
LdapClientConfiguration.class, LdapClientConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
LDAPConnection connection = this.context LDAPConnection connection = this.context.getBean(LDAPConnection.class);
.getBean(LDAPConnection.class); assertThat(connection.getConnectedPort()).isEqualTo(this.context.getEnvironment()
assertThat(connection.getConnectedPort())
.isEqualTo(this.context.getEnvironment()
.getProperty("local.ldap.port", Integer.class)); .getProperty("local.ldap.port", Integer.class));
} }
@Test @Test
public void testSetCredentials() throws LDAPException { public void testSetCredentials() throws LDAPException {
load("spring.ldap.embedded.partitionSuffix:dc=spring,dc=org", load("spring.ldap.embedded.base-dn:dc=spring,dc=org",
"spring.ldap.embedded.credential.username:uid=root", "spring.ldap.embedded.credential.username:uid=root",
"spring.ldap.embedded.credential.password:boot"); "spring.ldap.embedded.credential.password:boot");
InMemoryDirectoryServer server = this.context InMemoryDirectoryServer server = this.context
...@@ -104,7 +102,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -104,7 +102,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testSetPartitionSuffix() throws LDAPException { public void testSetPartitionSuffix() throws LDAPException {
load("spring.ldap.embedded.partitionSuffix:dc=spring,dc=org"); load("spring.ldap.embedded.base-dn:dc=spring,dc=org");
InMemoryDirectoryServer server = this.context InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
assertThat(server.getBaseDNs()).containsExactly(new DN("dc=spring,dc=org")); assertThat(server.getBaseDNs()).containsExactly(new DN("dc=spring,dc=org"));
...@@ -112,35 +110,31 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -112,35 +110,31 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testSetLdifFile() throws LDAPException { public void testSetLdifFile() throws LDAPException {
load("spring.ldap.embedded.partitionSuffix:dc=spring,dc=org", load("spring.ldap.embedded.base-dn:dc=spring,dc=org");
"spring.ldap.embedded.ldif:classpath:schema.ldif");
InMemoryDirectoryServer server = this.context InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
assertThat(server.countEntriesBelow("ou=company1,c=Sweden,dc=spring,dc=org")) assertThat(server.countEntriesBelow("ou=company1,c=Sweden,dc=spring,dc=org"))
.isEqualTo(5); .isEqualTo(5);
} }
@Test @Test
public void testQueryEmbeddedLdap() throws LDAPException { public void testQueryEmbeddedLdap() throws LDAPException {
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.ldap.embedded.partitionSuffix:dc=spring,dc=org", "spring.ldap.embedded.base-dn:dc=spring,dc=org");
"spring.ldap.embedded.ldif:classpath:schema.ldif");
this.context.register(EmbeddedLdapAutoConfiguration.class, this.context.register(EmbeddedLdapAutoConfiguration.class,
LdapAutoConfiguration.class, LdapAutoConfiguration.class, LdapDataAutoConfiguration.class,
LdapDataAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(LdapTemplate.class).length) assertThat(this.context.getBeanNamesForType(LdapTemplate.class).length)
.isEqualTo(1); .isEqualTo(1);
LdapTemplate ldapTemplate = this.context LdapTemplate ldapTemplate = this.context.getBean(LdapTemplate.class);
.getBean(LdapTemplate.class); assertThat(ldapTemplate.list("ou=company1,c=Sweden,dc=spring,dc=org")).hasSize(4);
assertThat(ldapTemplate.list("ou=company1,c=Sweden,dc=spring,dc=org").size())
.isEqualTo(4);
} }
private void load(String... properties) { private void load(String... properties) {
EnvironmentTestUtils.addEnvironment(this.context, properties); EnvironmentTestUtils.addEnvironment(this.context, properties);
this.context.register(EmbeddedLdapAutoConfiguration.class, this.context.register(EmbeddedLdapAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
} }
......
...@@ -345,6 +345,11 @@ ...@@ -345,6 +345,11 @@
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version> <version>1.5.0.BUILD-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId> <artifactId>spring-boot-starter-data-mongodb</artifactId>
......
...@@ -315,13 +315,6 @@ content into your application; rather pick only the properties that you need. ...@@ -315,13 +315,6 @@ content into your application; rather pick only the properties that you need.
spring.jersey.servlet.load-on-startup=-1 # Load on startup priority of the Jersey servlet. spring.jersey.servlet.load-on-startup=-1 # Load on startup priority of the Jersey servlet.
spring.jersey.type=servlet # Jersey integration type. spring.jersey.type=servlet # Jersey integration type.
# EMBEDDED LDAP ({sc-spring-boot-autoconfigure}/ldap/embedded/EmbeddedLdapProperties.{sc-ext}[EmbeddedLdapProperties])
spring.ldap.embedded.port= # Embedded LDAP port.
spring.ldap.embedded.credential.username= # Embedded LDAP username.
spring.ldap.embedded.credential.password= # Embedded LDAP password.
spring.ldap.embedded.partition-suffix= # LDAP partition suffix.
spring.ldap.embedded.ldif= # Schema (LDIF) script resource reference.
# SPRING LDAP ({sc-spring-boot-autoconfigure}/ldap/LdapProperties.{sc-ext}[LdapProperties]) # SPRING LDAP ({sc-spring-boot-autoconfigure}/ldap/LdapProperties.{sc-ext}[LdapProperties])
spring.ldap.urls= # LDAP url of the server. spring.ldap.urls= # LDAP url of the server.
spring.ldap.base= # Base suffix from which all operations should originate. spring.ldap.base= # Base suffix from which all operations should originate.
...@@ -329,6 +322,13 @@ content into your application; rather pick only the properties that you need. ...@@ -329,6 +322,13 @@ content into your application; rather pick only the properties that you need.
spring.ldap.password= # Login password of the server. spring.ldap.password= # Login password of the server.
spring.ldap.base-environment.*= # Ldap specification settings. spring.ldap.base-environment.*= # Ldap specification settings.
# EMBEDDED LDAP ({sc-spring-boot-autoconfigure}/ldap/embedded/EmbeddedLdapProperties.{sc-ext}[EmbeddedLdapProperties])
spring.ldap.embedded.port= # Embedded LDAP port.
spring.ldap.embedded.credential.username= # Embedded LDAP username.
spring.ldap.embedded.credential.password= # Embedded LDAP password.
spring.ldap.embedded.base-dn= # The base DN
spring.ldap.embedded.ldif= # Schema (LDIF) script resource reference.
# SPRING MOBILE DEVICE VIEWS ({sc-spring-boot-autoconfigure}/mobile/DeviceDelegatingViewResolverAutoConfiguration.{sc-ext}[DeviceDelegatingViewResolverAutoConfiguration]) # SPRING MOBILE DEVICE VIEWS ({sc-spring-boot-autoconfigure}/mobile/DeviceDelegatingViewResolverAutoConfiguration.{sc-ext}[DeviceDelegatingViewResolverAutoConfiguration])
spring.mobile.devicedelegatingviewresolver.enable-fallback=false # Enable support for fallback resolution. spring.mobile.devicedelegatingviewresolver.enable-fallback=false # Enable support for fallback resolution.
spring.mobile.devicedelegatingviewresolver.enabled=false # Enable device view resolver. spring.mobile.devicedelegatingviewresolver.enabled=false # Enable device view resolver.
......
<?xml version="1.0" encoding="UTF-8"?> <?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"> <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> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>spring-boot-starters</artifactId> <artifactId>spring-boot-starters</artifactId>
...@@ -27,6 +28,26 @@ ...@@ -27,6 +28,26 @@
<artifactId>spring-data-ldap</artifactId> <artifactId>spring-data-ldap</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
</project> <plugin>
\ No newline at end of file <groupId>org.basepom.maven</groupId>
<artifactId>duplicate-finder-maven-plugin</artifactId>
<executions>
<execution>
<id>duplicate-dependencies</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<ignoredResourcePatterns>
<ignoredResourcePattern>changelog.txt</ignoredResourcePattern>
</ignoredResourcePatterns>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment