From 1ebbe9fc556c1526cd90d20a4443311c3bcb53d6 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Wed, 14 Aug 2019 09:12:54 +0200 Subject: [PATCH] Map non-null LDAP properties 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. See gh-17861 --- .../autoconfigure/ldap/LdapAutoConfiguration.java | 15 +++++++++------ .../ldap/LdapAutoConfigurationTests.java | 11 +++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) 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 766c0bc6388..7480f2fc046 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; @@ -55,12 +56,14 @@ public class LdapAutoConfiguration { @ConditionalOnMissingBean public LdapContextSource ldapContextSource() { LdapContextSource source = new LdapContextSource(); - source.setUserDn(this.properties.getUsername()); - source.setPassword(this.properties.getPassword()); - source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly()); - source.setBase(this.properties.getBase()); - source.setUrls(this.properties.determineUrls(this.environment)); - source.setBaseEnvironmentProperties(Collections.unmodifiableMap(this.properties.getBaseEnvironment())); + PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull(); + propertyMapper.from(this.properties.getUsername()).to(source::setUserDn); + propertyMapper.from(this.properties.getPassword()).to(source::setPassword); + propertyMapper.from(this.properties.getAnonymousReadOnly()).to(source::setAnonymousReadOnly); + propertyMapper.from(this.properties.getBase()).to(source::setBase); + propertyMapper.from(this.properties.determineUrls(this.environment)).to(source::setUrls); + propertyMapper.from(this.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 578fe552409..d83f1a69f8b 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 @@ -90,6 +90,17 @@ public class LdapAutoConfigurationTests { }); } + @Test + public 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 public void templateExists() { this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389")