以下 MyCallback() 类创建用户认证信息(域、用户登录名和密码)并返回到提供程序。
// Callback class, needed to create a provider. private static class MyCallback implements Callback { // Get a WVCM Authentication object. // This implementation of the authentication // callback returns the specified username and password. // The Provider calls getAuthentication to authenticate the current user. public Authentication getAuthentication(final String realm, int retryCount) { if (retryCount>0) throw UnsupportedOperationException("Bad credentials"); return new Authentication() { public String loginName() { return "<the_domain>\\<the_username>"; } public String password() { return "<the_password>"; } }; } }
对每个 Provider 实例提供了一个 Provider.Callback 对象,该对象用于获取客户机通过该 Provider 实例访问的任何存储库的凭证。
realm 参数是一个字符串,它标识正在请求认证的上下文(例如,服务器 URL 或存储库名称)。每个子提供程序的字符串格式不尽相同,对于用户,这些格式应显示为助记符。
retryCount 指定向存储库进行认证的尝试次数。
在本例中,不使用 realm 和 retryCount 参数。但是,客户机应用程序应该将 retryCount 尝试次数限制为一个较小的数字,因为对认证重试次数没有限制,并且提供程序会在失败后不断进行认证尝试,除非已设置 retryCount 或 getAuthentication 方法抛出异常。
在客户机应用程序中,认证回调可以打开登录窗口以收集用户登录名和密码。窗口中可提供 realm 参数,以显示用户正在登录到哪个产品存储库(例如,服务器 URL 或用户数据库)。 如果用户对不同产品存储库使用不同用户名和密码,那么该选项可能会很有用。
当客户机使用 Provider 时,将为客户机向其发出请求的每个不同域调用 Callback。请参阅 StpProvider 类的 Javadoc 信息,以获取关于传递给 ClearQuest CM API 提供程序的回调的需求详细信息。