Skip to content

Commit 5b3f6cc

Browse files
support for bind and search authentication
1 parent b12c003 commit 5b3f6cc

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Test.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static Pair<String, DirContext> getContext(final Properties props) {
5757
try {
5858
return new Pair<String, DirContext>(ldapUrl, new InitialDirContext(env));
5959
} catch (Exception e) {
60-
theLogger.info("Failed to connext to ldap instance [" + ldapUrl.trim() + "]. Trying next...\n");
60+
theLogger.info("Failed to connect to ldap instance [" + ldapUrl.trim() + "]. Trying next...\n");
6161
}
6262
}
6363
}
@@ -78,7 +78,7 @@ private static void connect(final Properties props) throws Exception {
7878
throw new IllegalArgumentException("Could not connect to any of the provided ldap urls based on the given credentials.");
7979
}
8080

81-
DirContext ctx = pair.getSecond();
81+
DirContext ctx = null;
8282

8383
try {
8484
ctx = pair.getSecond();
@@ -104,6 +104,20 @@ private static void connect(final Properties props) throws Exception {
104104
theLogger.info("User name: " + result.getName());
105105
theLogger.info("User full name: " + result.getNameInNamespace());
106106

107+
String authnPsw = props.getProperty("ldap.authn.password");
108+
if (authnPsw != null) {
109+
theLogger.info("Attempting to authenticate " + result.getName() + " with password " + authnPsw);
110+
111+
final Hashtable<String, String> env = new Hashtable<String, String>(6);
112+
env.put(Context.INITIAL_CONTEXT_FACTORY, props.getProperty("ldap.factory"));
113+
env.put(Context.PROVIDER_URL, pair.getFirst().trim());
114+
env.put(Context.SECURITY_AUTHENTICATION, props.getProperty("ldap.authentication"));
115+
env.put(Context.SECURITY_PRINCIPAL, result.getNameInNamespace());
116+
env.put(Context.SECURITY_CREDENTIALS, authnPsw);
117+
env.put("com.sun.jndi.ldap.connect.timeout", props.getProperty("ldap.timeout"));
118+
DirContext userCtx = new InitialDirContext(env);
119+
theLogger.info("Successfully authenticated " + result.getName() + " with password " + authnPsw + " at " + pair.getFirst());
120+
}
107121
final NamingEnumeration<String> attrs = result.getAttributes().getIDs();
108122

109123
while (attrs.hasMoreElements()) {

Test.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
ldap.url1 = ldap://1.2.3.4
21
ldap.url2 =
32
ldap.url3 =
43
ldap.url4 =
@@ -15,4 +14,6 @@ ldap.attributes = cn,givenName
1514

1615
ldap.factory = com.sun.jndi.ldap.LdapCtxFactory
1716
ldap.authentication = simple
18-
ldap.timeout = 3000
17+
ldap.timeout = 3000
18+
19+
ldap.authn.password = Dobby1757!

0 commit comments

Comments
 (0)