From a25354b3cdd78908329a40b2194f313a69b20027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Mon, 22 Jan 2018 23:18:42 -0500 Subject: [PATCH] Add support for anonymousReadOnly in LdapProperties See gh-11722 --- .../ldap/LdapAutoConfiguration.java | 3 ++- .../autoconfigure/ldap/LdapProperties.java | 15 ++++++++++++++- .../ldap/LdapAutoConfigurationTests.java | 18 +++++++++++++++++- 3 files changed, 33 insertions(+), 3 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 acf6f939293a..c3ad246e6443 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-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -58,6 +58,7 @@ public ContextSource ldapContextSource() { source.setUrls(this.properties.determineUrls(this.environment)); source.setBaseEnvironmentProperties( Collections.unmodifiableMap(this.properties.getBaseEnvironment())); + source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly()); return source; } 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 fcc9aba07188..70b9697ebcc1 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -60,6 +60,11 @@ public class LdapProperties { */ private Map baseEnvironment = new HashMap<>(); + /** + * Whether read-only operations should use an anonymous environment. + */ + private boolean anonymousReadOnly; + public String[] getUrls() { return this.urls; } @@ -100,6 +105,14 @@ public void setBaseEnvironment(Map baseEnvironment) { this.baseEnvironment = baseEnvironment; } + public boolean getAnonymousReadOnly() { + return this.anonymousReadOnly; + } + + public void setAnonymousReadOnly(boolean anonymousReadOnly) { + this.anonymousReadOnly = anonymousReadOnly; + } + 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 82614df4ffc5..384495d07f1d 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -23,6 +23,7 @@ import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.ldap.core.ContextSource; +import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -80,6 +81,21 @@ public void testContextSourceWithMoreProperties() { .containsEntry("java.naming.security.authentication", "DIGEST-MD5"); } + @Test + public void testContextSourceWithDefaultAnonymousReadOnly() { + load("spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123"); + LdapContextSource contextSource = context.getBean(LdapContextSource.class); + assertThat(contextSource.isAnonymousReadOnly()).isFalse(); + } + + @Test + public void testContextSourceWithAnonymousReadOnly() { + load("spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123", + "spring.ldap.anonymousReadOnly:true"); + LdapContextSource contextSource = context.getBean(LdapContextSource.class); + assertThat(contextSource.isAnonymousReadOnly()).isTrue(); + } + private void load(String... properties) { this.context = new AnnotationConfigApplicationContext(); TestPropertyValues.of(properties).applyTo(this.context);