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 ;
@@ -140,12 +142,15 @@ protected DirContextOperations doAuthentication(
140
142
UsernamePasswordAuthenticationToken auth ) {
141
143
String username = auth .getName ();
142
144
String password = (String ) auth .getCredentials ();
143
-
144
- DirContext ctx = bindAsUser (username , password );
145
+ DirContext ctx = null ;
145
146
146
147
try {
148
+ ctx = bindAsUser (username , password );
147
149
return searchForUser (ctx , username );
148
150
}
151
+ catch (CommunicationException e ) {
152
+ throw badLdapConnection (e );
153
+ }
149
154
catch (NamingException e ) {
150
155
logger .error ("Failed to locate directory entry for authenticated user: "
151
156
+ username , e );
@@ -207,8 +212,7 @@ private DirContext bindAsUser(String username, String password) {
207
212
|| (e instanceof OperationNotSupportedException )) {
208
213
handleBindException (bindPrincipal , e );
209
214
throw badCredentials (e );
210
- }
211
- else {
215
+ } else {
212
216
throw LdapUtils .convertLdapException (e );
213
217
}
214
218
}
@@ -300,6 +304,12 @@ private BadCredentialsException badCredentials(Throwable cause) {
300
304
return (BadCredentialsException ) badCredentials ().initCause (cause );
301
305
}
302
306
307
+ private InternalAuthenticationServiceException badLdapConnection (Throwable cause ) {
308
+ return new InternalAuthenticationServiceException (messages .getMessage (
309
+ "LdapAuthenticationProvider.badLdapConnection" ,
310
+ "Connection to LDAP server failed." ), cause );
311
+ }
312
+
303
313
private DirContextOperations searchForUser (DirContext context , String username )
304
314
throws NamingException {
305
315
SearchControls searchControls = new SearchControls ();
@@ -314,6 +324,9 @@ private DirContextOperations searchForUser(DirContext context, String username)
314
324
searchControls , searchRoot , searchFilter ,
315
325
new Object [] { bindPrincipal , username });
316
326
}
327
+ catch (CommunicationException ldapCommunicationException ) {
328
+ throw badLdapConnection (ldapCommunicationException );
329
+ }
317
330
catch (IncorrectResultSizeDataAccessException incorrectResults ) {
318
331
// Search should never return multiple results if properly configured - just
319
332
// rethrow
0 commit comments