32
32
import org .springframework .security .authentication .BadCredentialsException ;
33
33
import org .springframework .security .authentication .CredentialsExpiredException ;
34
34
import org .springframework .security .authentication .DisabledException ;
35
+ import org .springframework .security .authentication .InternalAuthenticationServiceException ;
35
36
import org .springframework .security .authentication .LockedException ;
36
37
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
37
38
import org .springframework .security .core .Authentication ;
58
59
* @author Rob Winch
59
60
*/
60
61
public class ActiveDirectoryLdapAuthenticationProviderTests {
62
+ public static final String EXISTING_LDAP_PROVIDER = "ldap://192.168.1.200/" ;
63
+ public static final String NON_EXISTING_LDAP_PROVIDER = "ldap://192.168.1.201/" ;
64
+
61
65
@ Rule
62
66
public ExpectedException thrown = ExpectedException .none ();
63
67
@@ -378,16 +382,29 @@ public void errorWithNoSubcodeIsHandledCleanly() {
378
382
}
379
383
380
384
@ Test (expected = org .springframework .ldap .CommunicationException .class )
381
- public void nonAuthenticationExceptionIsConvertedToSpringLdapException () {
382
- provider .contextFactory = createContextFactoryThrowing (new CommunicationException (
383
- msg ));
384
- provider .authenticate (joe );
385
+ public void nonAuthenticationExceptionIsConvertedToSpringLdapException () throws Throwable {
386
+ try {
387
+ provider .contextFactory = createContextFactoryThrowing (new CommunicationException (
388
+ msg ));
389
+ provider .authenticate (joe );
390
+ } catch (InternalAuthenticationServiceException e ) {
391
+ // Since GH-8418 ldap communication exception is wrapped into InternalAuthenticationServiceException.
392
+ // This test is about the wrapped exception, so we throw it.
393
+ throw e .getCause ();
394
+ }
395
+ }
396
+
397
+ @ Test (expected = org .springframework .security .authentication .InternalAuthenticationServiceException .class )
398
+ public void connectionExceptionIsWrappedInInternalException () throws Exception {
399
+ ActiveDirectoryLdapAuthenticationProvider noneReachableProvider = new ActiveDirectoryLdapAuthenticationProvider (
400
+ "mydomain.eu" , NON_EXISTING_LDAP_PROVIDER , "dc=ad,dc=eu,dc=mydomain" );
401
+ noneReachableProvider .doAuthentication (joe );
385
402
}
386
403
387
404
@ Test
388
405
public void rootDnProvidedSeparatelyFromDomainAlsoWorks () throws Exception {
389
406
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider (
390
- "mydomain.eu" , "ldap://192.168.1.200/" , "dc=ad,dc=eu,dc=mydomain" );
407
+ "mydomain.eu" , EXISTING_LDAP_PROVIDER , "dc=ad,dc=eu,dc=mydomain" );
391
408
checkAuthentication ("dc=ad,dc=eu,dc=mydomain" , provider );
392
409
393
410
}
@@ -413,8 +430,11 @@ public void contextEnvironmentPropertiesUsed() {
413
430
provider .authenticate (joe );
414
431
fail ("CommunicationException was expected with a root cause of ClassNotFoundException" );
415
432
}
416
- catch (org .springframework .ldap .CommunicationException expected ) {
417
- assertThat (expected .getRootCause ()).isInstanceOf (ClassNotFoundException .class );
433
+ catch (InternalAuthenticationServiceException expected ) {
434
+ assertThat (expected .getCause ()).isInstanceOf (org .springframework .ldap .CommunicationException .class );
435
+ org .springframework .ldap .CommunicationException cause =
436
+ (org .springframework .ldap .CommunicationException ) expected .getCause ();
437
+ assertThat (cause .getRootCause ()).isInstanceOf (ClassNotFoundException .class );
418
438
}
419
439
}
420
440
0 commit comments