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 ;
@@ -140,12 +142,15 @@ protected DirContextOperations doAuthentication(
140142 UsernamePasswordAuthenticationToken auth ) {
141143 String username = auth .getName ();
142144 String password = (String ) auth .getCredentials ();
143-
144- DirContext ctx = bindAsUser (username , password );
145+ DirContext ctx = null ;
145146
146147 try {
148+ ctx = bindAsUser (username , password );
147149 return searchForUser (ctx , username );
148150 }
151+ catch (CommunicationException e ) {
152+ throw badLdapConnection (e );
153+ }
149154 catch (NamingException e ) {
150155 logger .error ("Failed to locate directory entry for authenticated user: "
151156 + username , e );
@@ -207,8 +212,7 @@ private DirContext bindAsUser(String username, String password) {
207212 || (e instanceof OperationNotSupportedException )) {
208213 handleBindException (bindPrincipal , e );
209214 throw badCredentials (e );
210- }
211- else {
215+ } else {
212216 throw LdapUtils .convertLdapException (e );
213217 }
214218 }
@@ -300,6 +304,12 @@ private BadCredentialsException badCredentials(Throwable cause) {
300304 return (BadCredentialsException ) badCredentials ().initCause (cause );
301305 }
302306
307+ private InternalAuthenticationServiceException badLdapConnection (Throwable cause ) {
308+ return new InternalAuthenticationServiceException (messages .getMessage (
309+ "LdapAuthenticationProvider.badLdapConnection" ,
310+ "Connection to LDAP server failed." ), cause );
311+ }
312+
303313 private DirContextOperations searchForUser (DirContext context , String username )
304314 throws NamingException {
305315 SearchControls searchControls = new SearchControls ();
@@ -314,6 +324,9 @@ private DirContextOperations searchForUser(DirContext context, String username)
314324 searchControls , searchRoot , searchFilter ,
315325 new Object [] { bindPrincipal , username });
316326 }
327+ catch (CommunicationException ldapCommunicationException ) {
328+ throw badLdapConnection (ldapCommunicationException );
329+ }
317330 catch (IncorrectResultSizeDataAccessException incorrectResults ) {
318331 // Search should never return multiple results if properly configured - just
319332 // rethrow
0 commit comments