Skip to content

Commit 279c110

Browse files
committed
Check That Credentials Are Non-Null
Closes gh-538
1 parent 359a91c commit 279c110

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,24 @@
1616

1717
package org.springframework.ldap.core.support;
1818

19+
import java.net.URI;
20+
import java.net.URISyntaxException;
21+
import java.util.Hashtable;
22+
import java.util.ListIterator;
23+
import java.util.Map;
24+
25+
import javax.naming.Context;
26+
import javax.naming.NamingEnumeration;
27+
import javax.naming.NamingException;
28+
import javax.naming.directory.Attribute;
29+
import javax.naming.directory.Attributes;
30+
import javax.naming.directory.DirContext;
31+
import javax.naming.ldap.LdapName;
32+
import javax.naming.ldap.Rdn;
33+
1934
import org.slf4j.Logger;
2035
import org.slf4j.LoggerFactory;
36+
2137
import org.springframework.beans.factory.InitializingBean;
2238
import org.springframework.ldap.UncategorizedLdapException;
2339
import org.springframework.ldap.core.AuthenticationSource;
@@ -28,21 +44,6 @@
2844
import org.springframework.util.ObjectUtils;
2945
import org.springframework.util.StringUtils;
3046

31-
import javax.naming.Context;
32-
import javax.naming.NamingEnumeration;
33-
import javax.naming.NamingException;
34-
import javax.naming.directory.Attribute;
35-
import javax.naming.directory.Attributes;
36-
import javax.naming.directory.DirContext;
37-
import javax.naming.ldap.LdapName;
38-
import javax.naming.ldap.Rdn;
39-
40-
import java.net.URI;
41-
import java.net.URISyntaxException;
42-
import java.util.Hashtable;
43-
import java.util.ListIterator;
44-
import java.util.Map;
45-
4647
/**
4748
* Abstract implementation of the {@link ContextSource} interface. By default,
4849
* returns an authenticated
@@ -424,8 +425,13 @@ public void afterPropertiesSet() {
424425
LOG.info("Property 'userDn' not set - " + "anonymous context will be used for read-write operations");
425426
anonymousReadOnly = true;
426427
}
427-
else if (!StringUtils.hasText(password)) {
428-
LOG.info("Property 'password' not set - " + "blank password will be used");
428+
if (!anonymousReadOnly) {
429+
if (password == null) {
430+
throw new IllegalArgumentException("Property 'password' cannot be null. To use a blank password, please ensure it is set to \"\"");
431+
}
432+
if (!StringUtils.hasText(password)) {
433+
LOG.info("Property 'password' not set - " + "blank password will be used");
434+
}
429435
}
430436
authenticationSource = new SimpleAuthenticationSource();
431437
}

core/src/test/java/org/springframework/ldap/core/support/LdapContextSourceTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ public void testAfterPropertiesSet_NoUrl() throws Exception {
4646
tested.afterPropertiesSet();
4747
}
4848

49-
@Test
49+
// gh-538
50+
@Test(expected = IllegalArgumentException.class)
51+
public void testAfterPropertiesSet_NullPassword() {
52+
tested.setUrl("ldap://ldap.example.com:389");
53+
tested.setUserDn("value");
54+
tested.setPassword(null);
55+
tested.afterPropertiesSet();
56+
}
57+
58+
@Test
5059
public void testGetAnonymousEnv() throws Exception {
5160
tested.setBase("dc=some example,dc=se");
5261
tested.setUrl("ldap://ldap.example.com:389");

0 commit comments

Comments
 (0)