From 0ebfec5df557f2490e99c3a1290922dd4e1f80ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Mon, 22 Jan 2018 22:32:08 -0500 Subject: [PATCH] Set anonymousReadOnly true when userDn is not provided Fixes gh-473 --- .../core/support/AbstractContextSource.java | 1 + .../ldap/itest/ldap473/Ldap473Test.java | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/ldap473/Ldap473Test.java diff --git a/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java b/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java index 5e120ef6..7c85c3a4 100644 --- a/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java +++ b/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java @@ -413,6 +413,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource LOG.debug("AuthenticationSource not set - " + "using default implementation"); if (!StringUtils.hasText(userDn)) { LOG.info("Property 'userDn' not set - " + "anonymous context will be used for read-write operations"); + anonymousReadOnly = true; } else if (!StringUtils.hasText(password)) { LOG.info("Property 'password' not set - " + "blank password will be used"); diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/ldap473/Ldap473Test.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/ldap473/Ldap473Test.java new file mode 100644 index 00000000..c2883626 --- /dev/null +++ b/test/integration-tests/src/test/java/org/springframework/ldap/itest/ldap473/Ldap473Test.java @@ -0,0 +1,61 @@ +package org.springframework.ldap.itest.ldap473; + +import javax.naming.directory.DirContext; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.ldap.core.ContextSource; +import org.springframework.ldap.core.support.LdapContextSource; +import org.springframework.ldap.test.EmbeddedLdapServerFactoryBean; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author EddĂș MelĂ©ndez + */ +@RunWith(SpringJUnit4ClassRunner.class) +public class Ldap473Test { + + @Autowired + private ContextSource contextSource; + + @Test + public void anonymous() { + DirContext readOnlyContext = contextSource.getReadOnlyContext(); + assertThat(readOnlyContext).isNotNull(); + } + + @Configuration + static class ContextSourceConfig { + + @Bean + public ContextSource contextSource() { + LdapContextSource contextSource = new LdapContextSource(); + contextSource.setUrl("ldap://localhost:9321"); + contextSource.setUserDn(null); + contextSource.setPassword(null); + return contextSource; + } + + } + + @Configuration + static class EmbeddedLdapConfig { + + @Bean + public EmbeddedLdapServerFactoryBean embeddedLdapServer() { + EmbeddedLdapServerFactoryBean embeddedLdapServer = new EmbeddedLdapServerFactoryBean(); + embeddedLdapServer.setPartitionName("example"); + embeddedLdapServer.setPartitionSuffix("dc=261consulting,dc=com"); + embeddedLdapServer.setPort(9321); + return embeddedLdapServer; + } + + } + +}