SpringSecurityLoginModule extends AbstractLoginModule { @Override protected Authentication getAuthentication( final Principal principal, final Credentials credentials ) throws RepositoryException { if ( !( credentials instanceof SimpleCredentials ) ) { logger.debug( "credentials not instance of SimpleCredentials; returning null" ); return null; } SimpleCredentials simpleCredentials = (SimpleCredentials) credentials; UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( simpleCredentials.getUserID(), String.valueOf( simpleCredentials .getPassword() ) ); boolean authenticated = false; try { org.springframework.security.core.Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if ( authentication != null && authentication.getName().equals( simpleCredentials.getUserID() ) ) { authenticated = true; } else { getAuthenticationManager().authenticate( token ); authenticated = true; } } catch ( AuthenticationException e ) { logger.debug( "authentication exception", e ); } final boolean authenticateResult = authenticated; return new Authentication() { public boolean canHandle( Credentials credentials ) { return true; } public boolean authenticate( Credentials credentials ) throws RepositoryException { return authenticateResult; } }; } SpringSecurityLoginModule();   }
@Test public void testNullAuthenticationManager() throws Exception { SecurityContextHolder.clearContext(); org.apache.jackrabbit.core.security.authentication.Authentication authentication = loginModule.getAuthentication( mock( Principal.class ), credentials ); assertFalse( authentication.authenticate( credentials ) ); Authentication springAuth = mock( Authentication.class ); AuthenticationManager authenticationManager = mock( AuthenticationManager.class ); when( authenticationManager.authenticate( any( Authentication.class ) ) ).thenReturn( springAuth ); org.apache.jackrabbit.core.security.authentication.Authentication mockAuthentication = mock( org.apache.jackrabbit.core.security.authentication.Authentication.class ); when( mockAuthentication.authenticate( credentials ) ).thenReturn( Boolean.TRUE ); PentahoSystem.registerObject( authenticationManager ); PentahoSystem.init(); org.apache.jackrabbit.core.security.authentication.Authentication jackrabbitAuth = loginModule.getAuthentication( mock( Principal.class ), credentials ); assertTrue( jackrabbitAuth.authenticate( credentials ) ); }