SpringPlainTextPasswordValidationCallbackHandler extends AbstractCallbackHandler implements InitializingBean { @Override protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException { if (callback instanceof PasswordValidationCallback) { PasswordValidationCallback validationCallback = (PasswordValidationCallback) callback; if (validationCallback.getRequest() instanceof PasswordValidationCallback.PlainTextPasswordRequest) { validationCallback.setValidator(new SpringSecurityPlainTextPasswordValidator()); return; } } else if (callback instanceof CleanupCallback) { SecurityContextHolder.clearContext(); return; } throw new UnsupportedCallbackException(callback); }  void setAuthenticationManager(AuthenticationManager authenticationManager); void setIgnoreFailure(boolean ignoreFailure); @Override void afterPropertiesSet();  }
@Test public void testAuthenticateUserPlainTextValid() throws Exception { Authentication authResult = new TestingAuthenticationToken(username, password, Collections.<GrantedAuthority> emptyList()); expect(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password))) .andReturn(authResult); replay(authenticationManager); callbackHandler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertTrue("Not authenticated", authenticated); Assert.assertNotNull("No Authentication created", SecurityContextHolder.getContext().getAuthentication()); verify(authenticationManager); }
@Test public void testAuthenticateUserPlainTextInvalid() throws Exception { expect(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password))) .andThrow(new BadCredentialsException("")); replay(authenticationManager); callbackHandler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertFalse("Authenticated", authenticated); Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication()); verify(authenticationManager); }
@Test public void testCleanUp() throws Exception { TestingAuthenticationToken authentication = new TestingAuthenticationToken(new Object(), new Object(), Collections.<GrantedAuthority> emptyList()); SecurityContextHolder.getContext().setAuthentication(authentication); CleanupCallback cleanupCallback = new CleanupCallback(); callbackHandler.handleInternal(cleanupCallback); Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication()); }