【问题标题】:Proxy-Authorization header is not send within first request in Apache HttpComponentsProxy-Authorization 标头未在 Apache HttpComponents 的第一个请求中发送
【发布时间】:2023-04-02 10:05:02
【问题描述】:

我正在使用Apache HttpComponents (4.5.2),我正在尝试通过代理服务器(SSH 隧道)请求 HTTPS 页面。

问题是,根据日志客户端发送的第一个请求没有Proxy-Authorization 标头,但是在代理响应407 错误(需要代理身份验证)后,它通过发送Proxy-Authorization 标头退出身份验证。

我认为我的代码有问题,我需要启用原始身份验证之类的东西,但我找不到有关如何执行此操作的任何信息。

以下是确认我的话的日志。

第一个请求:

03:12:06,643 DEBUG headers:135 - http-outgoing-0 >> CONNECT t.myhost.com:443 HTTP/1.1
03:12:06,643 DEBUG headers:138 - http-outgoing-0 >> Host: t.myhost.com
03:12:06,643 DEBUG headers:138 - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_45)

03:12:06,793 DEBUG headers:124 - http-outgoing-0 << HTTP/1.1 407 Proxy Authentication Required
03:12:06,794 DEBUG headers:127 - http-outgoing-0 << Proxy-Authenticate: Basic realm="ProxyCompany"
03:12:06,794 DEBUG headers:127 - http-outgoing-0 << Proxy-Connection: close

// 然后它重试包含Proxy-Authorization 标头的请求

03:12:06,795 DEBUG HttpAuthenticator:77 - Authentication required
03:12:06,795 DEBUG HttpAuthenticator:107 - 162.243.116.56:71223 requested authentication
03:12:06,795 DEBUG ProxyAuthenticationStrategy:174 - Authentication schemes in the order of preference: [Negotiate, Kerberos, NTLM, Digest, Basic]
03:12:06,795 DEBUG ProxyAuthenticationStrategy:203 - Challenge for Negotiate authentication scheme not available
03:12:06,796 DEBUG ProxyAuthenticationStrategy:203 - Challenge for Kerberos authentication scheme not available
03:12:06,796 DEBUG ProxyAuthenticationStrategy:203 - Challenge for NTLM authentication scheme not available
03:12:06,796 DEBUG ProxyAuthenticationStrategy:203 - Challenge for Digest authentication scheme not available
03:12:06,800 DEBUG HttpAuthenticator:157 - Selected authentication options: [BASIC [complete=true]]
03:12:06,800 DEBUG DefaultManagedHttpClientConnection:81 - http-outgoing-0: Close connection
03:12:06,801 DEBUG DefaultHttpClientConnectionOperator:138 - Connecting to /162.243.116.56:71223
03:12:06,942 DEBUG DefaultHttpClientConnectionOperator:145 - Connection established 192.168.0.100:13391<->162.243.116.56:71223
03:12:06,942 DEBUG HttpAuthenticator:198 - Generating response to an authentication challenge using basic scheme
03:12:06,947 DEBUG headers:135 - http-outgoing-0 >> CONNECT t.myhost.com:443 HTTP/1.1
03:12:06,947 DEBUG headers:138 - http-outgoing-0 >> Host: t.myhost.com
03:12:06,947 DEBUG headers:138 - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_45)
03:12:06,947 DEBUG headers:138 - http-outgoing-0 >> Proxy-Authorization: Basic bHVtXXXXXXXXXXXXxOTE5NTUXXXXXXRmNmRkYmI1Mjk0MA==

03:12:07,304 DEBUG HttpAuthenticator:86 - Authentication succeeded
03:12:07,305 DEBUG ProxyAuthenticationStrategy:227 - Caching 'basic' auth scheme for http://162.243.116.56:71223 

这是我的代码(它是 Scala,但很容易阅读):

val credProvider = {
  val provider = new BasicCredentialsProvider()
  provider.setCredentials(AuthScope.ANY,
    new UsernamePasswordCredentials("myUser", "myPass"))
  provider
}

val connManager = {
  val mngr  = new PoolingHttpClientConnectionManager()
  mngr.setDefaultMaxPerRoute(Integer.MAX_VALUE)
  mngr.setMaxTotal(Integer.MAX_VALUE)
  mngr
}

val client = HttpClients.custom()
  .setConnectionManager(connManager)
  .disableRedirectHandling()
  .setDefaultCredentialsProvider(credProvider)
  .setProxy(new HttpHost(162.243.116.56, 71223 ))
  .build()

     val requestConfig = RequestConfig.custom()
       .setConnectTimeout(30000)
       .setConnectionRequestTimeout(30000)
       .build()


     val request = new HttpGet(url)
     request.setConfig(requestConfig)
     val response = client.execute(request)

我该如何解决这个问题(导致客户端总是发送 Proxy-Authorization )?

【问题讨论】:

标签: java scala apache-httpclient-4.x apache-httpcomponents


【解决方案1】:

我不确定这是否是同样的问题,但在 4.5.2 版本中,SPN (HTTP/something@somerealm) 和 https 引入了一个错误:HTTPCLIENT-1712(cmets 特别有趣,因为它们显示发生的历史)。

切换到 4.5.1 版本应该可以解决(当然如果这是同样的问题)

【讨论】:

    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2018-09-23
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多