Skip to content

Commit 0ebfec5

Browse files
eddumelendezrwinch
authored andcommitted
Set anonymousReadOnly true when userDn is not provided
Fixes gh-473
1 parent f09881c commit 0ebfec5

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ public void afterPropertiesSet() {
413413
LOG.debug("AuthenticationSource not set - " + "using default implementation");
414414
if (!StringUtils.hasText(userDn)) {
415415
LOG.info("Property 'userDn' not set - " + "anonymous context will be used for read-write operations");
416+
anonymousReadOnly = true;
416417
}
417418
else if (!StringUtils.hasText(password)) {
418419
LOG.info("Property 'password' not set - " + "blank password will be used");
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.springframework.ldap.itest.ldap473;
2+
3+
import javax.naming.directory.DirContext;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.ldap.core.ContextSource;
12+
import org.springframework.ldap.core.support.LdapContextSource;
13+
import org.springframework.ldap.test.EmbeddedLdapServerFactoryBean;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
/**
19+
* @author Eddú Meléndez
20+
*/
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
public class Ldap473Test {
23+
24+
@Autowired
25+
private ContextSource contextSource;
26+
27+
@Test
28+
public void anonymous() {
29+
DirContext readOnlyContext = contextSource.getReadOnlyContext();
30+
assertThat(readOnlyContext).isNotNull();
31+
}
32+
33+
@Configuration
34+
static class ContextSourceConfig {
35+
36+
@Bean
37+
public ContextSource contextSource() {
38+
LdapContextSource contextSource = new LdapContextSource();
39+
contextSource.setUrl("ldap://localhost:9321");
40+
contextSource.setUserDn(null);
41+
contextSource.setPassword(null);
42+
return contextSource;
43+
}
44+
45+
}
46+
47+
@Configuration
48+
static class EmbeddedLdapConfig {
49+
50+
@Bean
51+
public EmbeddedLdapServerFactoryBean embeddedLdapServer() {
52+
EmbeddedLdapServerFactoryBean embeddedLdapServer = new EmbeddedLdapServerFactoryBean();
53+
embeddedLdapServer.setPartitionName("example");
54+
embeddedLdapServer.setPartitionSuffix("dc=261consulting,dc=com");
55+
embeddedLdapServer.setPort(9321);
56+
return embeddedLdapServer;
57+
}
58+
59+
}
60+
61+
}

0 commit comments

Comments
 (0)