SimpleUsernamePasswordCallbackHandler extends AbstractCallbackHandler implements InitializingBean { @Override protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException { if (callback instanceof UsernameCallback) { UsernameCallback usernameCallback = (UsernameCallback) callback; usernameCallback.setUsername(username); } else if (callback instanceof PasswordCallback) { PasswordCallback passwordCallback = (PasswordCallback) callback; passwordCallback.setPassword(password); } else { throw new UnsupportedCallbackException(callback); } } SimpleUsernamePasswordCallbackHandler();  SimpleUsernamePasswordCallbackHandler(String username, String password); void setPassword(String password); void setUsername(String username); @Override void afterPropertiesSet();  }
@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()); }