diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java index ca8e4e98a5a..afb14cf81cc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java @@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @@ -46,12 +47,14 @@ public class LdapAutoConfiguration { @ConditionalOnMissingBean public LdapContextSource ldapContextSource(LdapProperties properties, Environment environment) { LdapContextSource source = new LdapContextSource(); - source.setUserDn(properties.getUsername()); - source.setPassword(properties.getPassword()); - source.setAnonymousReadOnly(properties.getAnonymousReadOnly()); - source.setBase(properties.getBase()); - source.setUrls(properties.determineUrls(environment)); - source.setBaseEnvironmentProperties(Collections.unmodifiableMap(properties.getBaseEnvironment())); + PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull(); + propertyMapper.from(properties.getUsername()).to(source::setUserDn); + propertyMapper.from(properties.getPassword()).to(source::setPassword); + propertyMapper.from(properties.getAnonymousReadOnly()).to(source::setAnonymousReadOnly); + propertyMapper.from(properties.getBase()).to(source::setBase); + propertyMapper.from(properties.determineUrls(environment)).to(source::setUrls); + propertyMapper.from(properties.getBaseEnvironment()).to( + (baseEnvironment) -> source.setBaseEnvironmentProperties(Collections.unmodifiableMap(baseEnvironment))); return source; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java index 636e725ba4a..796b3a99f52 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java @@ -87,6 +87,17 @@ class LdapAutoConfigurationTests { }); } + @Test + void contextSourceWithNoCustomization() { + this.contextRunner.run((context) -> { + LdapContextSource contextSource = context.getBean(LdapContextSource.class); + assertThat(contextSource.getUserDn()).isEqualTo(""); + assertThat(contextSource.getPassword()).isEqualTo(""); + assertThat(contextSource.isAnonymousReadOnly()).isFalse(); + assertThat(contextSource.getBaseLdapPathAsString()).isEqualTo(""); + }); + } + @Test void templateExists() { this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389")