Merge pull request #13143 from vpavic:improve-ldap-condition

* pr/13143:
  Polish "Improve LDAP auto-configuration conditions"
  Improve LDAP auto-configuration conditions
pull/13036/merge
Stephane Nicoll 7 years ago
commit 70f492afd4

@ -53,7 +53,7 @@ public class LdapAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ContextSource ldapContextSource() { public LdapContextSource ldapContextSource() {
LdapContextSource source = new LdapContextSource(); LdapContextSource source = new LdapContextSource();
source.setUserDn(this.properties.getUsername()); source.setUserDn(this.properties.getUsername());
source.setPassword(this.properties.getPassword()); source.setPassword(this.properties.getPassword());

@ -58,7 +58,6 @@ import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -101,7 +100,7 @@ public class EmbeddedLdapAutoConfiguration {
@Bean @Bean
@DependsOn("directoryServer") @DependsOn("directoryServer")
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ContextSource ldapContextSource() { public LdapContextSource ldapContextSource() {
LdapContextSource source = new LdapContextSource(); LdapContextSource source = new LdapContextSource();
if (hasCredentials(this.embeddedProperties.getCredential())) { if (hasCredentials(this.embeddedProperties.getCredential())) {
source.setUserDn(this.embeddedProperties.getCredential().getUsername()); source.setUserDn(this.embeddedProperties.getCredential().getUsername());

@ -20,9 +20,14 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.pool2.factory.PoolConfig;
import org.springframework.ldap.pool2.factory.PooledContextSource;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -54,7 +59,8 @@ public class LdapAutoConfigurationTests {
public void contextSourceWithSingleUrl() { public void contextSourceWithSingleUrl() {
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123") this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123")
.run((context) -> { .run((context) -> {
ContextSource contextSource = context.getBean(ContextSource.class); ContextSource contextSource = context
.getBean(LdapContextSource.class);
String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, String[] urls = (String[]) ReflectionTestUtils.getField(contextSource,
"urls"); "urls");
assertThat(urls).containsExactly("ldap://localhost:123"); assertThat(urls).containsExactly("ldap://localhost:123");
@ -67,7 +73,8 @@ public class LdapAutoConfigurationTests {
.withPropertyValues( .withPropertyValues(
"spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123") "spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123")
.run((context) -> { .run((context) -> {
ContextSource contextSource = context.getBean(ContextSource.class); ContextSource contextSource = context
.getBean(LdapContextSource.class);
LdapProperties ldapProperties = context.getBean(LdapProperties.class); LdapProperties ldapProperties = context.getBean(LdapProperties.class);
String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, String[] urls = (String[]) ReflectionTestUtils.getField(contextSource,
"urls"); "urls");
@ -104,4 +111,32 @@ public class LdapAutoConfigurationTests {
.run(context -> assertThat(context).hasSingleBean(LdapTemplate.class)); .run(context -> assertThat(context).hasSingleBean(LdapTemplate.class));
} }
@Test
public void contextSourceWithUserProvidedPooledContextSource() {
this.contextRunner.withUserConfiguration(PooledContextSourceConfig.class)
.run((context) -> {
LdapContextSource contextSource = context
.getBean(LdapContextSource.class);
String[] urls = (String[]) ReflectionTestUtils.getField(contextSource,
"urls");
assertThat(urls).containsExactly("ldap://localhost:389");
assertThat(contextSource.isAnonymousReadOnly()).isFalse();
});
}
@Configuration
static class PooledContextSourceConfig {
@Bean
@Primary
public PooledContextSource pooledContextSource(
LdapContextSource ldapContextSource) {
PooledContextSource pooledContextSource = new PooledContextSource(
new PoolConfig());
pooledContextSource.setContextSource(ldapContextSource);
return pooledContextSource;
}
}
} }

@ -4539,6 +4539,11 @@ URLs of your server in your application.properties, as shown in the following ex
If you need to customize connection settings, you can use the `spring.ldap.base` and If you need to customize connection settings, you can use the `spring.ldap.base` and
`spring.ldap.base-environment` properties. `spring.ldap.base-environment` properties.
A `LdapContextSource` is auto-configured based on these settings. If you need to customize
it, for instance to use a `PooledContextSource`, you can still inject the auto-configured
`LdapContextSource`. Make sure to flag your customized `ContextSource` as `@Primary` so
that the auto-configured `LdapTemplate` uses it.
[[boot-features-ldap-spring-data-repositories]] [[boot-features-ldap-spring-data-repositories]]

Loading…
Cancel
Save