9

在 Apache HTTP 组件 4 类 org.apache.http.impl.auth.BasicScheme 我注意到该方法:

public static Header authenticate(
            final Credentials credentials,
            final String charset,
            final boolean proxy)

不推荐使用以下信息:

/**
 * Returns a basic <tt>Authorization</tt> header value for the given
 * {@link Credentials} and charset.
 *
 * @param credentials The credentials to encode.
 * @param charset The charset to use for encoding the credentials
 *
 * @return a basic authorization header
 *
 * @deprecated (4.3) use {@link #authenticate(Credentials, HttpRequest, HttpContext)}.
 */
@Deprecated

但是,我没有看到任何文档解释如何从废弃的函数迁移到新的函数。尽管已弃用的功能有效,但我宁愿以“正确”的方式做事。以下是我如何使用已弃用的功能:

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
URI uriLogin = URI.create("http://localhost:8161/hawtio/auth/login/");
HttpPost hpLogin = new HttpPost(uriLogin);
hpLogin.setHeader(BasicScheme.authenticate(creds, "US-ASCII", false));

我怎样才能采用相同的概念并将其应用于 BasicScheme.authenticate 的“正确”方法?

4

2 回答 2

6

补充 oleg 的答案,这是一个满足我需要的示例替换。

请注意需要从静态调用转到对象实例。

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
URI uriLogin = URI.create("http://localhost:8161/hawtio/auth/login/");
HttpPost hpPost = new HttpPost(uriLogin);
Header header = new BasicScheme(StandardCharsets.UTF_8).authenticate(creds , hpPost, null);
hpPost.addHeader( header); 
于 2016-05-28T00:48:00.507 回答
5

对我来说,弃用通知看起来很清楚应该使用什么方法。您应该使用#authenticate也将实例HttpContext作为参数的方法。在 BASIC 的情况下,上下文可以为空。更复杂的方案可能需要访问其他上下文属性才能生成身份验证请求。

于 2013-11-07T17:17:59.627 回答