1616package org .springframework .security .ldap .authentication .ad ;
1717
1818import org .springframework .dao .IncorrectResultSizeDataAccessException ;
19+ import org .springframework .ldap .CommunicationException ;
1920import org .springframework .ldap .core .DirContextOperations ;
2021import org .springframework .ldap .core .DistinguishedName ;
2122import org .springframework .ldap .core .support .DefaultDirObjectFactory ;
2425import org .springframework .security .authentication .BadCredentialsException ;
2526import org .springframework .security .authentication .CredentialsExpiredException ;
2627import org .springframework .security .authentication .DisabledException ;
28+ import org .springframework .security .authentication .InternalAuthenticationServiceException ;
2729import org .springframework .security .authentication .LockedException ;
2830import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
2931import org .springframework .security .core .GrantedAuthority ;
@@ -141,12 +143,15 @@ protected DirContextOperations doAuthentication(
141143 UsernamePasswordAuthenticationToken auth ) {
142144 String username = auth .getName ();
143145 String password = (String ) auth .getCredentials ();
144-
145- DirContext ctx = bindAsUser (username , password );
146+ DirContext ctx = null ;
146147
147148 try {
149+ ctx = bindAsUser (username , password );
148150 return searchForUser (ctx , username );
149151 }
152+ catch (CommunicationException e ) {
153+ throw badLdapConnection (e );
154+ }
150155 catch (NamingException e ) {
151156 logger .error ("Failed to locate directory entry for authenticated user: "
152157 + username , e );
@@ -208,8 +213,7 @@ private DirContext bindAsUser(String username, String password) {
208213 || (e instanceof OperationNotSupportedException )) {
209214 handleBindException (bindPrincipal , e );
210215 throw badCredentials (e );
211- }
212- else {
216+ } else {
213217 throw LdapUtils .convertLdapException (e );
214218 }
215219 }
@@ -311,6 +315,12 @@ private BadCredentialsException badCredentials(Throwable cause) {
311315 return (BadCredentialsException ) badCredentials ().initCause (cause );
312316 }
313317
318+ private InternalAuthenticationServiceException badLdapConnection (Throwable cause ) {
319+ return new InternalAuthenticationServiceException (messages .getMessage (
320+ "LdapAuthenticationProvider.badLdapConnection" ,
321+ "Connection to LDAP server failed." ), cause );
322+ }
323+
314324 private DirContextOperations searchForUser (DirContext context , String username )
315325 throws NamingException {
316326 SearchControls searchControls = new SearchControls ();
@@ -325,6 +335,9 @@ private DirContextOperations searchForUser(DirContext context, String username)
325335 searchControls , searchRoot , searchFilter ,
326336 new Object [] { bindPrincipal , username });
327337 }
338+ catch (CommunicationException ldapCommunicationException ) {
339+ throw badLdapConnection (ldapCommunicationException );
340+ }
328341 catch (IncorrectResultSizeDataAccessException incorrectResults ) {
329342 // Search should never return multiple results if properly configured - just
330343 // rethrow
0 commit comments