Polish "Detect DirContextAuthenticationStrategy bean"

See gh-19328
This commit is contained in:
Stephane Nicoll 2019-12-09 15:58:44 +01:00
parent c108d2d011
commit aa821151bb
3 changed files with 9 additions and 34 deletions

View File

@ -48,8 +48,9 @@ public class LdapAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public LdapContextSource ldapContextSource(LdapProperties properties, Environment environment,
ObjectProvider<DirContextAuthenticationStrategy> authenticationStrategy) {
ObjectProvider<DirContextAuthenticationStrategy> dirContextAuthenticationStrategy) {
LdapContextSource source = new LdapContextSource();
dirContextAuthenticationStrategy.ifUnique(source::setAuthenticationStrategy);
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
propertyMapper.from(properties.getUsername()).to(source::setUserDn);
propertyMapper.from(properties.getPassword()).to(source::setPassword);
@ -58,8 +59,6 @@ public class LdapAutoConfiguration {
propertyMapper.from(properties.determineUrls(environment)).to(source::setUrls);
propertyMapper.from(properties.getBaseEnvironment()).to(
(baseEnvironment) -> source.setBaseEnvironmentProperties(Collections.unmodifiableMap(baseEnvironment)));
authenticationStrategy.ifUnique(source::setAuthenticationStrategy);
return source;
}

View File

@ -16,10 +16,6 @@
package org.springframework.boot.autoconfigure.ldap;
import java.util.Hashtable;
import javax.naming.directory.DirContext;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -36,6 +32,7 @@ import org.springframework.ldap.pool2.factory.PooledContextSource;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link LdapAutoConfiguration}.
@ -97,14 +94,11 @@ class LdapAutoConfigurationTests {
@Test
void contextSourceWithNoCustomization() {
this.contextRunner.run((context) -> {
assertThat(context).doesNotHaveBean(DirContextAuthenticationStrategy.class);
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
assertThat(contextSource.getUserDn()).isEqualTo("");
assertThat(contextSource.getPassword()).isEqualTo("");
assertThat(contextSource.isAnonymousReadOnly()).isFalse();
assertThat(contextSource.getBaseLdapPathAsString()).isEqualTo("");
assertThat(ReflectionTestUtils.getField(contextSource, "authenticationStrategy"))
.isInstanceOf(SimpleDirContextAuthenticationStrategy.class);
});
}
@ -129,7 +123,7 @@ class LdapAutoConfigurationTests {
assertThat(context).hasSingleBean(DirContextAuthenticationStrategy.class);
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
assertThat(ReflectionTestUtils.getField(contextSource, "authenticationStrategy"))
.isEqualTo(context.getBean(DirContextAuthenticationStrategy.class));
.isSameAs(context.getBean("customDirContextAuthenticationStrategy"));
});
}
@ -139,15 +133,10 @@ class LdapAutoConfigurationTests {
AnotherCustomDirContextAuthenticationStrategy.class).run((context) -> {
assertThat(context).hasBean("customDirContextAuthenticationStrategy")
.hasBean("anotherCustomDirContextAuthenticationStrategy");
TestDirContextAuthenticationStrategy customDirContextAuthenticationStrategy = context.getBean(
"customDirContextAuthenticationStrategy", TestDirContextAuthenticationStrategy.class);
TestDirContextAuthenticationStrategy anotherCustomDirContextAuthenticationStrategy = context
.getBean("anotherCustomDirContextAuthenticationStrategy",
TestDirContextAuthenticationStrategy.class);
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
assertThat(ReflectionTestUtils.getField(contextSource, "authenticationStrategy"))
.isNotEqualTo(customDirContextAuthenticationStrategy)
.isNotEqualTo(anotherCustomDirContextAuthenticationStrategy)
.isNotSameAs(context.getBean("customDirContextAuthenticationStrategy"))
.isNotSameAs(context.getBean("anotherCustomDirContextAuthenticationStrategy"))
.isInstanceOf(SimpleDirContextAuthenticationStrategy.class);
});
}
@ -170,7 +159,7 @@ class LdapAutoConfigurationTests {
@Bean
DirContextAuthenticationStrategy customDirContextAuthenticationStrategy() {
return new TestDirContextAuthenticationStrategy();
return mock(DirContextAuthenticationStrategy.class);
}
}
@ -180,21 +169,7 @@ class LdapAutoConfigurationTests {
@Bean
DirContextAuthenticationStrategy anotherCustomDirContextAuthenticationStrategy() {
return new TestDirContextAuthenticationStrategy();
}
}
static class TestDirContextAuthenticationStrategy implements DirContextAuthenticationStrategy {
@Override
public void setupEnvironment(Hashtable<String, Object> env, String userDn, String password) {
}
@Override
public DirContext processContextAfterCreation(DirContext ctx, String userDn, String password) {
return ctx;
return mock(DirContextAuthenticationStrategy.class);
}
}

View File

@ -4445,6 +4445,7 @@ To connect to an LDAP server, make sure you declare a dependency on the `spring-
If you need to customize connection settings, you can use the `spring.ldap.base` and `spring.ldap.base-environment` properties.
An `LdapContextSource` is auto-configured based on these settings.
If a `DirContextAuthenticationStrategy` bean is available, it is associated to the auto-configured `LdapContextSource`.
If you need to customize it, for instance to use a `PooledContextSource`, you can still inject the auto-configured `LdapContextSource`.
Make sure to flag your customized `ContextSource` as `@Primary` so that the auto-configured `LdapTemplate` uses it.