Jetty http客户端强制执行WWW-Auhenticate

问题描述

我正在使用JettyHttp Client调用以下外部API代码

object Test extends App {

  val client = new HttpClient(new SslContextFactory.Client())
  client.start()
  val response = client.POST("https://xxx")
    .header("authorization","bearer LOL")
    .content(new StringContentProvider("{}"))
    .send()

  println("xd")

}

结果是

Exception in thread "main" java.util.concurrent.ExecutionException: org.eclipse.jetty.client.HttpResponseException: HTTP protocol violation: Authentication challenge without WWW-Authenticate header

这很好-像此处所述-https://www.eclipse.org/jetty/documentation/current/http-client-authentication.html

但是我也尝试过:

object Test extends App {

  val client = new HttpClient(new SslContextFactory.Client())
  client.getAuthenticationStore.addAuthenticationResult(new Authentication.Result {
    override def getURI: URI = URI.create("https://xxx")
    override def apply(request: Request): Unit = request.header("authorization","bearer lol")
  })
  client.start()
  val response = client.POST("https://xxx")
    .content(new StringContentProvider("{}"))
    .send()

  println("xd")

}

,结果仍然相同。我无法强制外部服务器响应401返回正确的WWW-Authenticate标头。我可以使Jetty客户端以某种方式仅发送带有Authorization标头的请求而没有此第一个401请求吗? 外部API可以正常工作-我可以卷曲API并获得正确的答案。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)