Apache HTTP BasicScheme.authenticate已弃用?

E.S*_*.S. 7 java apache post http apache-httpcomponents

在Apache HTTP Component 4类org.apache.http.impl.auth.BasicScheme中,我注意到了该方法:

public static Header authenticate(
            final Credentials credentials,
            final String charset,
            final boolean proxy)
Run Code Online (Sandbox Code Playgroud)

不推荐使用以下信息:

/**
 * 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
Run Code Online (Sandbox Code Playgroud)

但是,我没有看到解释如何从deprated函数迁移到新函数的文档.虽然已弃用的功能有效,但我宁愿以"正确"的方式做事.以下是我使用不推荐使用的函数的方法:

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));
Run Code Online (Sandbox Code Playgroud)

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

ok2*_*k2c 5

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


Mat*_* S. 5

补充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); 
Run Code Online (Sandbox Code Playgroud)