SpringUsernamePasswordCallbackHandler extends AbstractCallbackHandler { @Override protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException { if (callback instanceof UsernameCallback) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null && authentication.getName() != null) { UsernameCallback usernameCallback = (UsernameCallback) callback; usernameCallback.setUsername(authentication.getName()); return; } else { logger.warn("Cannot handle UsernameCallback: Spring Security SecurityContext contains no Authentication"); } } else if (callback instanceof PasswordCallback) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null && authentication.getName() != null) { PasswordCallback passwordCallback = (PasswordCallback) callback; passwordCallback.setPassword(authentication.getCredentials().toString()); return; } else { logger.warn("Canot handle PasswordCallback: Spring Security SecurityContext contains no Authentication"); } } throw new UnsupportedCallbackException(callback); }    }
@Test public void testUsernameCallback() throws Exception { UsernameCallback usernameCallback = new UsernameCallback(); handler.handleInternal(usernameCallback); Assert.assertEquals("Invalid username", "Bert", usernameCallback.getUsername()); }
@Test public void testPasswordCallback() throws Exception { PasswordCallback passwordCallback = new PasswordCallback(); handler.handleInternal(passwordCallback); Assert.assertEquals("Invalid username", "Ernie", passwordCallback.getPassword()); }