@@ -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 ()) {
0 commit comments