Skip to content

Commit 59705ed

Browse files
dobrosisnicoll
authored andcommitted
Add support for configuring LDAP's referral property
See gh-44850 Signed-off-by: Andras, Dobrosi <[email protected]>
1 parent f8047ff commit 59705ed

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package org.springframework.boot.autoconfigure.ldap;
1818

1919
import java.util.Collections;
20+
import java.util.Optional;
2021

2122
import org.springframework.beans.factory.ObjectProvider;
2223
import org.springframework.boot.autoconfigure.AutoConfiguration;
2324
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.autoconfigure.ldap.LdapProperties.Referral;
2628
import org.springframework.boot.autoconfigure.ldap.LdapProperties.Template;
2729
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2830
import org.springframework.boot.context.properties.PropertyMapper;
@@ -67,6 +69,7 @@ public LdapContextSource ldapContextSource(LdapConnectionDetails connectionDetai
6769
propertyMapper.from(connectionDetails.getUsername()).to(source::setUserDn);
6870
propertyMapper.from(connectionDetails.getPassword()).to(source::setPassword);
6971
propertyMapper.from(properties.getAnonymousReadOnly()).to(source::setAnonymousReadOnly);
72+
Optional.ofNullable(properties.getReferral()).map(Referral::getMode).ifPresent(source::setReferral);
7073
propertyMapper.from(connectionDetails.getBase()).to(source::setBase);
7174
propertyMapper.from(connectionDetails.getUrls()).to(source::setUrls);
7275
propertyMapper.from(properties.getBaseEnvironment())

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

+46
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.boot.context.properties.ConfigurationProperties;
2323
import org.springframework.core.env.Environment;
24+
import org.springframework.ldap.ReferralException;
2425
import org.springframework.ldap.core.LdapTemplate;
2526
import org.springframework.util.Assert;
2627
import org.springframework.util.ObjectUtils;
@@ -62,6 +63,11 @@ public class LdapProperties {
6263
*/
6364
private Boolean anonymousReadOnly;
6465

66+
/**
67+
* Set the method to handle referrals.
68+
*/
69+
private Referral referral;
70+
6571
/**
6672
* LDAP specification settings.
6773
*/
@@ -109,6 +115,14 @@ public void setAnonymousReadOnly(Boolean anonymousReadOnly) {
109115
this.anonymousReadOnly = anonymousReadOnly;
110116
}
111117

118+
public Referral getReferral() {
119+
return this.referral;
120+
}
121+
122+
public void setReferral(Referral referral) {
123+
this.referral = referral;
124+
}
125+
112126
public Map<String, String> getBaseEnvironment() {
113127
return this.baseEnvironment;
114128
}
@@ -182,4 +196,36 @@ public void setIgnoreSizeLimitExceededException(Boolean ignoreSizeLimitExceededE
182196

183197
}
184198

199+
/**
200+
* Enum to define how referrals encountered by the service provider are to be processed.
201+
*/
202+
public enum Referral {
203+
204+
/**
205+
* follow referrals automatically
206+
*/
207+
FOLLOW("follow"),
208+
209+
/**
210+
* ignore referrals
211+
*/
212+
IGNORE("ignore"),
213+
214+
/**
215+
* throw a {@link ReferralException} for each referral
216+
*/
217+
THROW("throw");
218+
219+
private final String mode;
220+
221+
Referral(String mode) {
222+
this.mode = mode;
223+
}
224+
225+
public String getMode() {
226+
return this.mode;
227+
}
228+
229+
}
230+
185231
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
4040
import static org.mockito.Mockito.mock;
41+
import static org.springframework.test.util.ReflectionTestUtils.getField;
4142

4243
/**
4344
* Tests for {@link LdapAutoConfiguration}.
@@ -88,6 +89,14 @@ void contextSourceWithUserDoesNotEnableAnonymousReadOnly() {
8889
});
8990
}
9091

92+
@Test
93+
void contextSourceWithReferral() {
94+
this.contextRunner.withPropertyValues("spring.ldap.referral:ignore").run((context) -> {
95+
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
96+
assertThat(getField(contextSource, "referral")).isEqualTo("ignore");
97+
});
98+
}
99+
91100
@Test
92101
void contextSourceWithExtraCustomization() {
93102
this.contextRunner

0 commit comments

Comments
 (0)