16
16
package org .springframework .security .ldap .authentication .ad ;
17
17
18
18
import org .springframework .dao .IncorrectResultSizeDataAccessException ;
19
+ import org .springframework .ldap .CommunicationException ;
19
20
import org .springframework .ldap .core .DirContextOperations ;
20
21
import org .springframework .ldap .core .DistinguishedName ;
21
22
import org .springframework .ldap .core .support .DefaultDirObjectFactory ;
24
25
import org .springframework .security .authentication .BadCredentialsException ;
25
26
import org .springframework .security .authentication .CredentialsExpiredException ;
26
27
import org .springframework .security .authentication .DisabledException ;
28
+ import org .springframework .security .authentication .InternalAuthenticationServiceException ;
27
29
import org .springframework .security .authentication .LockedException ;
28
30
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
29
31
import org .springframework .security .core .GrantedAuthority ;
@@ -141,12 +143,15 @@ protected DirContextOperations doAuthentication(
141
143
UsernamePasswordAuthenticationToken auth ) {
142
144
String username = auth .getName ();
143
145
String password = (String ) auth .getCredentials ();
144
-
145
- DirContext ctx = bindAsUser (username , password );
146
+ DirContext ctx = null ;
146
147
147
148
try {
149
+ ctx = bindAsUser (username , password );
148
150
return searchForUser (ctx , username );
149
151
}
152
+ catch (CommunicationException e ) {
153
+ throw badLdapConnection (e );
154
+ }
150
155
catch (NamingException e ) {
151
156
logger .error ("Failed to locate directory entry for authenticated user: "
152
157
+ username , e );
@@ -208,8 +213,7 @@ private DirContext bindAsUser(String username, String password) {
208
213
|| (e instanceof OperationNotSupportedException )) {
209
214
handleBindException (bindPrincipal , e );
210
215
throw badCredentials (e );
211
- }
212
- else {
216
+ } else {
213
217
throw LdapUtils .convertLdapException (e );
214
218
}
215
219
}
@@ -311,6 +315,12 @@ private BadCredentialsException badCredentials(Throwable cause) {
311
315
return (BadCredentialsException ) badCredentials ().initCause (cause );
312
316
}
313
317
318
+ private InternalAuthenticationServiceException badLdapConnection (Throwable cause ) {
319
+ return new InternalAuthenticationServiceException (messages .getMessage (
320
+ "LdapAuthenticationProvider.badLdapConnection" ,
321
+ "Connection to LDAP server failed." ), cause );
322
+ }
323
+
314
324
private DirContextOperations searchForUser (DirContext context , String username )
315
325
throws NamingException {
316
326
SearchControls searchControls = new SearchControls ();
@@ -325,6 +335,9 @@ private DirContextOperations searchForUser(DirContext context, String username)
325
335
searchControls , searchRoot , searchFilter ,
326
336
new Object [] { bindPrincipal , username });
327
337
}
338
+ catch (CommunicationException ldapCommunicationException ) {
339
+ throw badLdapConnection (ldapCommunicationException );
340
+ }
328
341
catch (IncorrectResultSizeDataAccessException incorrectResults ) {
329
342
// Search should never return multiple results if properly configured - just
330
343
// rethrow
0 commit comments