当前位置: 首页 > 知识库问答 >
问题:

在Apache HttpComponents中,代理授权标头未在第一个请求内发送

黄信厚
2023-03-14

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

问题是根据日志Client发送第一个请求时没有Proxy-Authoration标头,但在Proxy响应407错误(需要代理身份验证)后,它会使用发送Proxy-Authoration标头退出身份验证。

我认为我的代码有问题,我需要像启用原始身份验证这样的东西,但是我找不到任何关于如何做到这一点的信息。

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

第一个请求:

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

//然后它使用包含的代理授权标头重试请求

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)

我如何解决这个问题(使客户端始终发送代理授权)?

共有1个答案

公冶伟
2023-03-14

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

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

 类似资料:
  • 我正在尝试从这个 API 调用中创建“UUID”,我正在使用 axios 和 vue.js。这是我的源代码。 但不幸的是,我得到了 在'访问XMLHttpRequesthttps://www.uuidtools.com/api/generate/v1'从原点'http://localhost:8080'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“access c

  • HTTP规范说; HTTP访问身份验证在“HTTP身份验证:基本和摘要访问身份验证”[43]中进行了描述。如果请求经过身份验证并指定了一个领域,则相同的凭据应该对该领域内的所有其他请求有效(假设身份验证方案本身不需要其他凭据,例如根据质询值变化的凭据或使用同步时钟)。 我真的不明白这意味着什么,但这是我的情况,这里有反对 HTTP 规范的东西吗?我使用 Java Rest 服务 客户端使用HTTP

  • 我使用springfox 2.9.2我有这样的api: 女巫来自身份验证服务器。我尝试在swagger中首次调用此服务器,并将其传递给像上面这样的控制器请求。所以我做 在Swagger ui上,授权调用成功返回了令牌,但它没有将令牌添加到请求头中。它会产生 如果我像这样设置令牌:

  • 我是一个使用angular的新手,我正在努力学习一点,但我不能使用令牌进行简单的获取。我做了很多研究,最后总是得到这段代码。 对于我的后端,我使用Kotlin/Springboot并配置了Cors。 即使如此,当尝试这样做请求我得到这个错误。 访问位于“”的XMLHttpRequesthttp://localhost:5000/api-v1/来源的帐户/列表 -- 果心js:15713错误Http

  • 向带有某些头的endpoint发出邮递员请求 当我运行下面的代码时 它返回了我所期望的一切,如下所示 问题只出在Axios请求上,而不是其他任何事情。这里面出了什么问题?

  • 我正在努力构建两个asp.netmvc web应用程序之间的安全集成;和调用网络扫描应用程序的ERP系统。所以目前在接收器应用程序(网络扫描应用程序)上我有以下操作方法,应该只由ERP系统执行:- 而在调用者系统(ERP系统)上,我有以下调用上述动作方法的动作方法。 如上面代码所示,我正在发送一个安全令牌,它存储在两个应用程序的web中。配置文件。通过这种方式,我可以确保(或者这是我的意图)网络扫