From 6853320661547694ba58cf830592124fb7b5c0d1 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Fri, 1 May 2020 13:16:52 +0200 Subject: [PATCH 1/2] Add properties to control exceptions ignored by LdapTemplate See gh-21289 --- .../ldap/LdapAutoConfiguration.java | 12 ++++- .../autoconfigure/ldap/LdapProperties.java | 40 ++++++++++++++++ .../ldap/LdapAutoConfigurationTests.java | 22 ++++++++- .../ldap/LdapPropertiesTests.java | 46 +++++++++++++++++++ 4 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapPropertiesTests.java 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 c981c8d780..58dbedc194 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 @@ -64,8 +64,16 @@ public class LdapAutoConfiguration { @Bean @ConditionalOnMissingBean(LdapOperations.class) - public LdapTemplate ldapTemplate(ContextSource contextSource) { - return new LdapTemplate(contextSource); + public LdapTemplate ldapTemplate(LdapProperties properties, ContextSource contextSource) { + PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull(); + LdapTemplate ldapTemplate = new LdapTemplate(contextSource); + propertyMapper.from(properties.isIgnorePartialResultException()) + .to(ldapTemplate::setIgnorePartialResultException); + propertyMapper.from(properties.isIgnoreNameNotFoundException()) + .to(ldapTemplate::setIgnoreNameNotFoundException); + propertyMapper.from(properties.isIgnoreSizeLimitExceededException()) + .to(ldapTemplate::setIgnoreSizeLimitExceededException); + return ldapTemplate; } } 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 6425099fe0..8ba4bed505 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 @@ -66,6 +66,22 @@ public class LdapProperties { */ private final Map baseEnvironment = new HashMap<>(); + /** + * Whether PartialResultException should be ignored in searches via the LdapTemplate. + */ + private boolean ignorePartialResultException = false; + + /** + * Whether NameNotFoundException should be ignored in searches via the LdapTemplate. + */ + private boolean ignoreNameNotFoundException = false; + + /** + * Whether SizeLimitExceededException should be ignored in searches via the + * LdapTemplate. + */ + private boolean ignoreSizeLimitExceededException = true; + public String[] getUrls() { return this.urls; } @@ -110,6 +126,30 @@ public class LdapProperties { return this.baseEnvironment; } + public boolean isIgnorePartialResultException() { + return this.ignorePartialResultException; + } + + public void setIgnorePartialResultException(boolean ignorePartialResultException) { + this.ignorePartialResultException = ignorePartialResultException; + } + + public boolean isIgnoreNameNotFoundException() { + return this.ignoreNameNotFoundException; + } + + public void setIgnoreNameNotFoundException(boolean ignoreNameNotFoundException) { + this.ignoreNameNotFoundException = ignoreNameNotFoundException; + } + + public boolean isIgnoreSizeLimitExceededException() { + return this.ignoreSizeLimitExceededException; + } + + public void setIgnoreSizeLimitExceededException(Boolean ignoreSizeLimitExceededException) { + this.ignoreSizeLimitExceededException = ignoreSizeLimitExceededException; + } + public String[] determineUrls(Environment environment) { if (ObjectUtils.isEmpty(this.urls)) { return new String[] { "ldap://localhost:" + determinePort(environment) }; 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 579944dce9..6c7355db7b 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 @@ -112,8 +112,26 @@ class LdapAutoConfigurationTests { @Test void templateExists() { - this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389") - .run((context) -> assertThat(context).hasSingleBean(LdapTemplate.class)); + this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389").run((context) -> { + assertThat(context).hasSingleBean(LdapTemplate.class); + LdapTemplate ldapTemplate = context.getBean(LdapTemplate.class); + assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignorePartialResultException", false); + assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreNameNotFoundException", false); + assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreSizeLimitExceededException", true); + }); + } + + @Test + void templateWithCustomConfigurationExists() { + this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389", + "spring.ldap.ignorePartialResultException=true", "spring.ldap.ignoreNameNotFoundException=true", + "spring.ldap.ignoreSizeLimitExceededException=false").run((context) -> { + assertThat(context).hasSingleBean(LdapTemplate.class); + LdapTemplate ldapTemplate = context.getBean(LdapTemplate.class); + assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignorePartialResultException", true); + assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreNameNotFoundException", true); + assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreSizeLimitExceededException", false); + }); } @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapPropertiesTests.java new file mode 100644 index 0000000000..6163b48e66 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapPropertiesTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.ldap; + +import org.junit.jupiter.api.Test; + +import org.springframework.ldap.core.LdapTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link LdapProperties} + * + * @author Filip Hrisafov + */ +class LdapPropertiesTests { + + private final LdapProperties properties = new LdapProperties(); + + @Test + void ldapTemplatePropertiesUseConsistentLdapTemplateDefaultValues() { + LdapTemplate ldapTemplate = new LdapTemplate(); + + assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignorePartialResultException", + this.properties.isIgnorePartialResultException()); + assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreNameNotFoundException", + this.properties.isIgnoreNameNotFoundException()); + assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreSizeLimitExceededException", + this.properties.isIgnoreSizeLimitExceededException()); + } + +} From 8c341df73b8253eaacbbc2af130c60731cf08bc5 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 1 Jul 2020 10:03:34 +0100 Subject: [PATCH 2/2] Polish "Add properties to control exceptions ignored by LdapTemplate" See gh-21289 --- .../ldap/LdapAutoConfiguration.java | 11 +-- .../autoconfigure/ldap/LdapProperties.java | 90 +++++++++++-------- .../ldap/LdapAutoConfigurationTests.java | 7 +- .../ldap/LdapPropertiesTests.java | 11 ++- 4 files changed, 68 insertions(+), 51 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 58dbedc194..a4707e6364 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import org.springframework.beans.factory.ObjectProvider; 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.Template; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.context.annotation.Bean; @@ -65,13 +66,13 @@ public class LdapAutoConfiguration { @Bean @ConditionalOnMissingBean(LdapOperations.class) public LdapTemplate ldapTemplate(LdapProperties properties, ContextSource contextSource) { + Template template = properties.getTemplate(); PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull(); LdapTemplate ldapTemplate = new LdapTemplate(contextSource); - propertyMapper.from(properties.isIgnorePartialResultException()) + propertyMapper.from(template.isIgnorePartialResultException()) .to(ldapTemplate::setIgnorePartialResultException); - propertyMapper.from(properties.isIgnoreNameNotFoundException()) - .to(ldapTemplate::setIgnoreNameNotFoundException); - propertyMapper.from(properties.isIgnoreSizeLimitExceededException()) + propertyMapper.from(template.isIgnoreNameNotFoundException()).to(ldapTemplate::setIgnoreNameNotFoundException); + propertyMapper.from(template.isIgnoreSizeLimitExceededException()) .to(ldapTemplate::setIgnoreSizeLimitExceededException); return ldapTemplate; } 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 8ba4bed505..5b16e4403a 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 java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.env.Environment; +import org.springframework.ldap.core.LdapTemplate; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; @@ -66,21 +67,7 @@ public class LdapProperties { */ private final Map baseEnvironment = new HashMap<>(); - /** - * Whether PartialResultException should be ignored in searches via the LdapTemplate. - */ - private boolean ignorePartialResultException = false; - - /** - * Whether NameNotFoundException should be ignored in searches via the LdapTemplate. - */ - private boolean ignoreNameNotFoundException = false; - - /** - * Whether SizeLimitExceededException should be ignored in searches via the - * LdapTemplate. - */ - private boolean ignoreSizeLimitExceededException = true; + private Template template = new Template(); public String[] getUrls() { return this.urls; @@ -126,28 +113,8 @@ public class LdapProperties { return this.baseEnvironment; } - public boolean isIgnorePartialResultException() { - return this.ignorePartialResultException; - } - - public void setIgnorePartialResultException(boolean ignorePartialResultException) { - this.ignorePartialResultException = ignorePartialResultException; - } - - public boolean isIgnoreNameNotFoundException() { - return this.ignoreNameNotFoundException; - } - - public void setIgnoreNameNotFoundException(boolean ignoreNameNotFoundException) { - this.ignoreNameNotFoundException = ignoreNameNotFoundException; - } - - public boolean isIgnoreSizeLimitExceededException() { - return this.ignoreSizeLimitExceededException; - } - - public void setIgnoreSizeLimitExceededException(Boolean ignoreSizeLimitExceededException) { - this.ignoreSizeLimitExceededException = ignoreSizeLimitExceededException; + public Template getTemplate() { + return this.template; } public String[] determineUrls(Environment environment) { @@ -166,4 +133,53 @@ public class LdapProperties { return DEFAULT_PORT; } + /** + * {@link LdapTemplate settings}. + */ + public static class Template { + + /** + * Whether PartialResultException should be ignored in searches via the + * LdapTemplate. + */ + private boolean ignorePartialResultException = false; + + /** + * Whether NameNotFoundException should be ignored in searches via the + * LdapTemplate. + */ + private boolean ignoreNameNotFoundException = false; + + /** + * Whether SizeLimitExceededException should be ignored in searches via the + * LdapTemplate. + */ + private boolean ignoreSizeLimitExceededException = true; + + public boolean isIgnorePartialResultException() { + return this.ignorePartialResultException; + } + + public void setIgnorePartialResultException(boolean ignorePartialResultException) { + this.ignorePartialResultException = ignorePartialResultException; + } + + public boolean isIgnoreNameNotFoundException() { + return this.ignoreNameNotFoundException; + } + + public void setIgnoreNameNotFoundException(boolean ignoreNameNotFoundException) { + this.ignoreNameNotFoundException = ignoreNameNotFoundException; + } + + public boolean isIgnoreSizeLimitExceededException() { + return this.ignoreSizeLimitExceededException; + } + + public void setIgnoreSizeLimitExceededException(Boolean ignoreSizeLimitExceededException) { + this.ignoreSizeLimitExceededException = ignoreSizeLimitExceededException; + } + + } + } 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 6c7355db7b..db4aa5da81 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 @@ -122,10 +122,11 @@ class LdapAutoConfigurationTests { } @Test - void templateWithCustomConfigurationExists() { + void templateConfigurationCanBeCustomized() { this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389", - "spring.ldap.ignorePartialResultException=true", "spring.ldap.ignoreNameNotFoundException=true", - "spring.ldap.ignoreSizeLimitExceededException=false").run((context) -> { + "spring.ldap.template.ignorePartialResultException=true", + "spring.ldap.template.ignoreNameNotFoundException=true", + "spring.ldap.template.ignoreSizeLimitExceededException=false").run((context) -> { assertThat(context).hasSingleBean(LdapTemplate.class); LdapTemplate ldapTemplate = context.getBean(LdapTemplate.class); assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignorePartialResultException", true); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapPropertiesTests.java index 6163b48e66..c8d4a295e5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapPropertiesTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.ldap; import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.ldap.LdapProperties.Template; import org.springframework.ldap.core.LdapTemplate; import static org.assertj.core.api.Assertions.assertThat; @@ -29,18 +30,16 @@ import static org.assertj.core.api.Assertions.assertThat; */ class LdapPropertiesTests { - private final LdapProperties properties = new LdapProperties(); - @Test void ldapTemplatePropertiesUseConsistentLdapTemplateDefaultValues() { + Template templateProperties = new LdapProperties().getTemplate(); LdapTemplate ldapTemplate = new LdapTemplate(); - assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignorePartialResultException", - this.properties.isIgnorePartialResultException()); + templateProperties.isIgnorePartialResultException()); assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreNameNotFoundException", - this.properties.isIgnoreNameNotFoundException()); + templateProperties.isIgnoreNameNotFoundException()); assertThat(ldapTemplate).hasFieldOrPropertyWithValue("ignoreSizeLimitExceededException", - this.properties.isIgnoreSizeLimitExceededException()); + templateProperties.isIgnoreSizeLimitExceededException()); } }