Skip to content

Commit 3a20b1d

Browse files
committed
Merge pull request #17861 from filiphr
* pr/17861: Map non-null LDAP properties Closes gh-17861
2 parents fe63865 + 1ebbe9f commit 3a20b1d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2424
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.boot.context.properties.PropertyMapper;
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
2728
import org.springframework.core.env.Environment;
@@ -55,12 +56,14 @@ public LdapAutoConfiguration(LdapProperties properties, Environment environment)
5556
@ConditionalOnMissingBean
5657
public LdapContextSource ldapContextSource() {
5758
LdapContextSource source = new LdapContextSource();
58-
source.setUserDn(this.properties.getUsername());
59-
source.setPassword(this.properties.getPassword());
60-
source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly());
61-
source.setBase(this.properties.getBase());
62-
source.setUrls(this.properties.determineUrls(this.environment));
63-
source.setBaseEnvironmentProperties(Collections.unmodifiableMap(this.properties.getBaseEnvironment()));
59+
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
60+
propertyMapper.from(this.properties.getUsername()).to(source::setUserDn);
61+
propertyMapper.from(this.properties.getPassword()).to(source::setPassword);
62+
propertyMapper.from(this.properties.getAnonymousReadOnly()).to(source::setAnonymousReadOnly);
63+
propertyMapper.from(this.properties.getBase()).to(source::setBase);
64+
propertyMapper.from(this.properties.determineUrls(this.environment)).to(source::setUrls);
65+
propertyMapper.from(this.properties.getBaseEnvironment()).to(
66+
(baseEnvironment) -> source.setBaseEnvironmentProperties(Collections.unmodifiableMap(baseEnvironment)));
6467
return source;
6568
}
6669

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ public void contextSourceWithExtraCustomization() {
9090
});
9191
}
9292

93+
@Test
94+
public void contextSourceWithNoCustomization() {
95+
this.contextRunner.run((context) -> {
96+
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
97+
assertThat(contextSource.getUserDn()).isEqualTo("");
98+
assertThat(contextSource.getPassword()).isEqualTo("");
99+
assertThat(contextSource.isAnonymousReadOnly()).isFalse();
100+
assertThat(contextSource.getBaseLdapPathAsString()).isEqualTo("");
101+
});
102+
}
103+
93104
@Test
94105
public void templateExists() {
95106
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389")

0 commit comments

Comments
 (0)