Skip to content

Commit aa82115

Browse files
committed
Polish "Detect DirContextAuthenticationStrategy bean"
See gh-19328
1 parent c108d2d commit aa82115

File tree

3 files changed

+9
-34
lines changed

3 files changed

+9
-34
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ public class LdapAutoConfiguration {
4848
@Bean
4949
@ConditionalOnMissingBean
5050
public LdapContextSource ldapContextSource(LdapProperties properties, Environment environment,
51-
ObjectProvider<DirContextAuthenticationStrategy> authenticationStrategy) {
51+
ObjectProvider<DirContextAuthenticationStrategy> dirContextAuthenticationStrategy) {
5252
LdapContextSource source = new LdapContextSource();
53+
dirContextAuthenticationStrategy.ifUnique(source::setAuthenticationStrategy);
5354
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
5455
propertyMapper.from(properties.getUsername()).to(source::setUserDn);
5556
propertyMapper.from(properties.getPassword()).to(source::setPassword);
@@ -58,8 +59,6 @@ public LdapContextSource ldapContextSource(LdapProperties properties, Environmen
5859
propertyMapper.from(properties.determineUrls(environment)).to(source::setUrls);
5960
propertyMapper.from(properties.getBaseEnvironment()).to(
6061
(baseEnvironment) -> source.setBaseEnvironmentProperties(Collections.unmodifiableMap(baseEnvironment)));
61-
62-
authenticationStrategy.ifUnique(source::setAuthenticationStrategy);
6362
return source;
6463
}
6564

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.ldap;
1818

19-
import java.util.Hashtable;
20-
21-
import javax.naming.directory.DirContext;
22-
2319
import org.junit.jupiter.api.Test;
2420

2521
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -36,6 +32,7 @@
3632
import org.springframework.test.util.ReflectionTestUtils;
3733

3834
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.mockito.Mockito.mock;
3936

4037
/**
4138
* Tests for {@link LdapAutoConfiguration}.
@@ -97,14 +94,11 @@ void contextSourceWithExtraCustomization() {
9794
@Test
9895
void contextSourceWithNoCustomization() {
9996
this.contextRunner.run((context) -> {
100-
assertThat(context).doesNotHaveBean(DirContextAuthenticationStrategy.class);
10197
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
10298
assertThat(contextSource.getUserDn()).isEqualTo("");
10399
assertThat(contextSource.getPassword()).isEqualTo("");
104100
assertThat(contextSource.isAnonymousReadOnly()).isFalse();
105101
assertThat(contextSource.getBaseLdapPathAsString()).isEqualTo("");
106-
assertThat(ReflectionTestUtils.getField(contextSource, "authenticationStrategy"))
107-
.isInstanceOf(SimpleDirContextAuthenticationStrategy.class);
108102
});
109103
}
110104

@@ -129,7 +123,7 @@ void contextSourceWithCustomUniqueDirContextAuthenticationStrategy() {
129123
assertThat(context).hasSingleBean(DirContextAuthenticationStrategy.class);
130124
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
131125
assertThat(ReflectionTestUtils.getField(contextSource, "authenticationStrategy"))
132-
.isEqualTo(context.getBean(DirContextAuthenticationStrategy.class));
126+
.isSameAs(context.getBean("customDirContextAuthenticationStrategy"));
133127
});
134128
}
135129

@@ -139,15 +133,10 @@ void contextSourceWithCustomNonUniqueDirContextAuthenticationStrategy() {
139133
AnotherCustomDirContextAuthenticationStrategy.class).run((context) -> {
140134
assertThat(context).hasBean("customDirContextAuthenticationStrategy")
141135
.hasBean("anotherCustomDirContextAuthenticationStrategy");
142-
TestDirContextAuthenticationStrategy customDirContextAuthenticationStrategy = context.getBean(
143-
"customDirContextAuthenticationStrategy", TestDirContextAuthenticationStrategy.class);
144-
TestDirContextAuthenticationStrategy anotherCustomDirContextAuthenticationStrategy = context
145-
.getBean("anotherCustomDirContextAuthenticationStrategy",
146-
TestDirContextAuthenticationStrategy.class);
147136
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
148137
assertThat(ReflectionTestUtils.getField(contextSource, "authenticationStrategy"))
149-
.isNotEqualTo(customDirContextAuthenticationStrategy)
150-
.isNotEqualTo(anotherCustomDirContextAuthenticationStrategy)
138+
.isNotSameAs(context.getBean("customDirContextAuthenticationStrategy"))
139+
.isNotSameAs(context.getBean("anotherCustomDirContextAuthenticationStrategy"))
151140
.isInstanceOf(SimpleDirContextAuthenticationStrategy.class);
152141
});
153142
}
@@ -170,7 +159,7 @@ static class CustomDirContextAuthenticationStrategy {
170159

171160
@Bean
172161
DirContextAuthenticationStrategy customDirContextAuthenticationStrategy() {
173-
return new TestDirContextAuthenticationStrategy();
162+
return mock(DirContextAuthenticationStrategy.class);
174163
}
175164

176165
}
@@ -180,21 +169,7 @@ static class AnotherCustomDirContextAuthenticationStrategy {
180169

181170
@Bean
182171
DirContextAuthenticationStrategy anotherCustomDirContextAuthenticationStrategy() {
183-
return new TestDirContextAuthenticationStrategy();
184-
}
185-
186-
}
187-
188-
static class TestDirContextAuthenticationStrategy implements DirContextAuthenticationStrategy {
189-
190-
@Override
191-
public void setupEnvironment(Hashtable<String, Object> env, String userDn, String password) {
192-
193-
}
194-
195-
@Override
196-
public DirContext processContextAfterCreation(DirContext ctx, String userDn, String password) {
197-
return ctx;
172+
return mock(DirContextAuthenticationStrategy.class);
198173
}
199174

200175
}

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4445,6 +4445,7 @@ To connect to an LDAP server, make sure you declare a dependency on the `spring-
44454445
If you need to customize connection settings, you can use the `spring.ldap.base` and `spring.ldap.base-environment` properties.
44464446

44474447
An `LdapContextSource` is auto-configured based on these settings.
4448+
If a `DirContextAuthenticationStrategy` bean is available, it is associated to the auto-configured `LdapContextSource`.
44484449
If you need to customize it, for instance to use a `PooledContextSource`, you can still inject the auto-configured `LdapContextSource`.
44494450
Make sure to flag your customized `ContextSource` as `@Primary` so that the auto-configured `LdapTemplate` uses it.
44504451

0 commit comments

Comments
 (0)