From 62aecfdfb9c0d16f6224fb2224d125896260fb75 Mon Sep 17 00:00:00 2001 From: "Andras, Dobrosi" Date: Sun, 23 Mar 2025 10:28:28 +0100 Subject: [PATCH 1/2] New `LdapAutoConfiguration.referral` property in `LdapAutoConfiguration.java` Signed-off-by: Andras, Dobrosi --- .../autoconfigure/ldap/LdapAutoConfiguration.java | 1 + .../boot/autoconfigure/ldap/LdapProperties.java | 13 +++++++++++++ .../ldap/LdapAutoConfigurationTests.java | 9 +++++++++ 3 files changed, 23 insertions(+) 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 92e52dcb0992..e8e6dbae4f83 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 @@ -67,6 +67,7 @@ public LdapContextSource ldapContextSource(LdapConnectionDetails connectionDetai propertyMapper.from(connectionDetails.getUsername()).to(source::setUserDn); propertyMapper.from(connectionDetails.getPassword()).to(source::setPassword); propertyMapper.from(properties.getAnonymousReadOnly()).to(source::setAnonymousReadOnly); + propertyMapper.from(properties.getReferral()).to(source::setReferral); propertyMapper.from(connectionDetails.getBase()).to(source::setBase); propertyMapper.from(connectionDetails.getUrls()).to(source::setUrls); propertyMapper.from(properties.getBaseEnvironment()) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java index 2db8c69fcfae..5b931910155b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java @@ -62,6 +62,11 @@ public class LdapProperties { */ private Boolean anonymousReadOnly; + /** + * Set the method to handle referrals. + */ + private String referral; + /** * LDAP specification settings. */ @@ -109,6 +114,14 @@ public void setAnonymousReadOnly(Boolean anonymousReadOnly) { this.anonymousReadOnly = anonymousReadOnly; } + public String getReferral() { + return this.referral; + } + + public void setReferral(String referral) { + this.referral = referral; + } + public Map getBaseEnvironment() { return this.baseEnvironment; } 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 6bdbb2352718..7c2adb243b36 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 @@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.springframework.test.util.ReflectionTestUtils.getField; /** * Tests for {@link LdapAutoConfiguration}. @@ -88,6 +89,14 @@ void contextSourceWithUserDoesNotEnableAnonymousReadOnly() { }); } + @Test + void contextSourceWithReferral() { + this.contextRunner.withPropertyValues("spring.ldap.referral:ignore").run((context) -> { + LdapContextSource contextSource = context.getBean(LdapContextSource.class); + assertThat(getField(contextSource, "referral")).isEqualTo("ignore"); + }); + } + @Test void contextSourceWithExtraCustomization() { this.contextRunner From 00d61a00dc80fb909373914b39309ffcfed215d4 Mon Sep 17 00:00:00 2001 From: Andras Dobrosi Date: Mon, 24 Mar 2025 15:38:35 +0100 Subject: [PATCH 2/2] Enum for referral field in LdapProperties.java Signed-off-by: Andras, Dobrosi --- .../ldap/LdapAutoConfiguration.java | 4 +- .../autoconfigure/ldap/LdapProperties.java | 39 +++++++++++++++++-- 2 files changed, 39 insertions(+), 4 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 e8e6dbae4f83..bad5dc1950f6 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 @@ -17,12 +17,14 @@ package org.springframework.boot.autoconfigure.ldap; import java.util.Collections; +import java.util.Optional; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.ldap.LdapProperties.Referral; import org.springframework.boot.autoconfigure.ldap.LdapProperties.Template; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.PropertyMapper; @@ -67,7 +69,7 @@ public LdapContextSource ldapContextSource(LdapConnectionDetails connectionDetai propertyMapper.from(connectionDetails.getUsername()).to(source::setUserDn); propertyMapper.from(connectionDetails.getPassword()).to(source::setPassword); propertyMapper.from(properties.getAnonymousReadOnly()).to(source::setAnonymousReadOnly); - propertyMapper.from(properties.getReferral()).to(source::setReferral); + Optional.ofNullable(properties.getReferral()).map(Referral::getMode).ifPresent(source::setReferral); propertyMapper.from(connectionDetails.getBase()).to(source::setBase); propertyMapper.from(connectionDetails.getUrls()).to(source::setUrls); propertyMapper.from(properties.getBaseEnvironment()) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java index 5b931910155b..cbb7edbdc24f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java @@ -21,6 +21,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.env.Environment; +import org.springframework.ldap.ReferralException; import org.springframework.ldap.core.LdapTemplate; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; @@ -65,7 +66,7 @@ public class LdapProperties { /** * Set the method to handle referrals. */ - private String referral; + private Referral referral; /** * LDAP specification settings. @@ -114,11 +115,11 @@ public void setAnonymousReadOnly(Boolean anonymousReadOnly) { this.anonymousReadOnly = anonymousReadOnly; } - public String getReferral() { + public Referral getReferral() { return this.referral; } - public void setReferral(String referral) { + public void setReferral(Referral referral) { this.referral = referral; } @@ -195,4 +196,36 @@ public void setIgnoreSizeLimitExceededException(Boolean ignoreSizeLimitExceededE } + /** + * Enum to define how referrals encountered by the service provider are to be processed. + */ + public enum Referral { + + /** + * follow referrals automatically + */ + FOLLOW("follow"), + + /** + * ignore referrals + */ + IGNORE("ignore"), + + /** + * throw a {@link ReferralException} for each referral + */ + THROW("throw"); + + private final String mode; + + Referral(String mode) { + this.mode = mode; + } + + public String getMode() { + return this.mode; + } + + } + }