Merge pull request #17861 from filiphr
* pr/17861: Map non-null LDAP properties Closes gh-17861
This commit is contained in:
commit
3a20b1d90c
|
@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
@ -55,12 +56,14 @@ public class LdapAutoConfiguration {
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public LdapContextSource ldapContextSource() {
|
public LdapContextSource ldapContextSource() {
|
||||||
LdapContextSource source = new LdapContextSource();
|
LdapContextSource source = new LdapContextSource();
|
||||||
source.setUserDn(this.properties.getUsername());
|
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||||
source.setPassword(this.properties.getPassword());
|
propertyMapper.from(this.properties.getUsername()).to(source::setUserDn);
|
||||||
source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly());
|
propertyMapper.from(this.properties.getPassword()).to(source::setPassword);
|
||||||
source.setBase(this.properties.getBase());
|
propertyMapper.from(this.properties.getAnonymousReadOnly()).to(source::setAnonymousReadOnly);
|
||||||
source.setUrls(this.properties.determineUrls(this.environment));
|
propertyMapper.from(this.properties.getBase()).to(source::setBase);
|
||||||
source.setBaseEnvironmentProperties(Collections.unmodifiableMap(this.properties.getBaseEnvironment()));
|
propertyMapper.from(this.properties.determineUrls(this.environment)).to(source::setUrls);
|
||||||
|
propertyMapper.from(this.properties.getBaseEnvironment()).to(
|
||||||
|
(baseEnvironment) -> source.setBaseEnvironmentProperties(Collections.unmodifiableMap(baseEnvironment)));
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
@Test
|
||||||
public void templateExists() {
|
public void templateExists() {
|
||||||
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389")
|
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389")
|
||||||
|
|
Loading…
Reference in New Issue