Skip to content

Commit c404120

Browse files
committed
Use alwaysApplyingWhenNonNull PropertyMapper to set the values in the LdapContextSource
The userDn and password in LdapContextSource are not nullable. The default values for userDn and password in LdapProperties are null. When the values are set to null there will eventually be a NullPointerException during AbstractContextSource#setupAuthenticatedEnvironment since HashTable doesn't allow null for values
1 parent 3515ec1 commit c404120

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

Lines changed: 9 additions & 6 deletions
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

Lines changed: 11 additions & 0 deletions
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)