class SpringDigestPasswordValidationCallbackHandler extends AbstractCallbackHandler implements InitializingBean {

	@Override
	protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
		if (callback instanceof PasswordValidationCallback) {
			PasswordValidationCallback passwordCallback = (PasswordValidationCallback) callback;
			if (passwordCallback.getRequest() instanceof PasswordValidationCallback.DigestPasswordRequest) {
				PasswordValidationCallback.DigestPasswordRequest request = (PasswordValidationCallback.DigestPasswordRequest) passwordCallback
						.getRequest();
				String username = request.getUsername();
				UserDetails user = loadUserDetails(username);
				if (user != null) {
					SpringSecurityUtils.checkUserValidity(user);
					request.setPassword(user.getPassword());
				}
				SpringSecurityDigestPasswordValidator validator = new SpringSecurityDigestPasswordValidator(user);
				passwordCallback.setValidator(validator);
				return;
			}
		} else if (callback instanceof TimestampValidationCallback) {
			TimestampValidationCallback timestampCallback = (TimestampValidationCallback) callback;
			timestampCallback.setValidator(new DefaultTimestampValidator());

		} else if (callback instanceof CleanupCallback) {
			SecurityContextHolder.clearContext();
			return;
		}
		throw new UnsupportedCallbackException(callback);
	}

}