java - Apache HTTP BasicScheme.authenticate 已弃用?

标签 java http post basic-authentication apache-httpcomponents

在 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 的“正确”方法?

最佳答案

补充 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); 

关于java - Apache HTTP BasicScheme.authenticate 已弃用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19797601/

相关文章:

java - 使用 Java 读取名称中带有空格的文件?

ios - 如何使用 Alamofire 发送带有 gzip 内容的 HTTP POST 请求?

java - 缓存在 JAX-RS 中是如何工作的?

PHP - 表单提交后未设置 $_POST 变量

javascript - 如何使用 Javascript 发布 Content-Type header 中同时包含 "application/x-www-form-urlencoded"和 "charset=UTF-8"的 HTML 表单

java - Jsoup - 为 POST 传递原始正文

java - Spring JDK 动态代理和 CGLIB - 实现细节

java - Jackson csv writer 没有使用正确的列分隔符

java - 使用@Transactional注释方法会导致 "No unique bean of type is defined: expected single bean but found 0"

php - 为什么我会收到 HTTP 400 错误请求错误?