From af0bdc893b5bc08a9f93bcf0442f0f7720eac5cb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 24 Jan 2018 09:57:25 +0100 Subject: [PATCH] Add support for anonymousReadOnly in LdapProperties See gh-11744 --- .../autoconfigure/ldap/LdapAutoConfiguration.java | 3 ++- .../boot/autoconfigure/ldap/LdapProperties.java | 15 ++++++++++++++- .../ldap/LdapAutoConfigurationTests.java | 9 +++++++++ 3 files changed, 25 insertions(+), 2 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 acf6f93929..c3ad246e64 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 class LdapAutoConfiguration { 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 fcc9aba071..70b9697ebc 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 class LdapProperties { 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 85b85a121d..d6e8bac69e 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 @@ -89,4 +89,13 @@ public class LdapAutoConfigurationTests { }); } + @Test + public void testContextSourceWithDefaultAnonymousReadOnly() { + this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123") + .run(context -> { + LdapContextSource contextSource = context.getBean(LdapContextSource.class); + assertThat(contextSource.isAnonymousReadOnly()).isFalse(); + }); + } + }