【问题标题】:Apache HTTP BasicScheme.authenticate deprecated?Apache HTTP BasicScheme.authenticate 已弃用?
【发布时间】:2013-11-05 19:49:29
【问题描述】:

在 Apache HTTP Component 4 class 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 的“正确”方法?

【问题讨论】:

    标签: java http post basic-authentication apache-httpcomponents


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我用什么作为 HttpRequest 参数呢?
      • @Eric S:您显然正在为特定请求生成身份验证令牌,不是吗?因此,您应该将该请求传递给 #authenticate 方法。 BASIC 方案没有使用它,但其他方案如 DIGEST 使用它
      猜你喜欢
      • 1970-01-01
      • 2014-12-05
      • 2019-07-26
      • 2015-06-14
      • 1970-01-01
      • 2018-08-07
      • 2016-05-05
      • 1970-01-01
      • 2014-02-12
      相关资源
      最近更新 更多