使用 Apache HttpClient 4 进行抢占式基本身份验证
有没有比描述的更简单的方法来设置http客户端以进行抢占式基本身份验证 这里?
在以前的版本 (3.x) 中,它曾经是一个简单的方法调用(例如,httpClient.getParams().setAuthenticationPreemptive(true)
)。
我想避免的主要事情是将 BasicHttpContext 添加到我执行的每个方法中。
Is there an easier way to setup the http client for preemptive basic authentication than what described here?
In previous version (3.x) it used to be a simple method call (eg, httpClient.getParams().setAuthenticationPreemptive(true)
).
The main thing I want to avoid is adding the BasicHttpContext to each method I execute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您希望强制 HttpClient 4 通过单个请求进行身份验证,则可以使用以下方法:
If you are looking to force HttpClient 4 to authenticate with a single request, the following will work:
如果不每次都传递上下文,很难做到这一点,但您可以通过使用请求拦截器来做到这一点。这是我们使用的一些代码(从他们的 JIRA、iirc 中找到):
It's difficult to do this without passing a context through every time, but you can probably do it by using a request interceptor. Here is some code that we use (found from their JIRA, iirc):
这与 Mat 的 Mannion 的解决方案相同,但您不必将 localContext 放入每个请求中。它更简单,但它为所有请求添加了身份验证。如果您无法控制单个请求,那么很有用,就像我使用 Apache Solr 时的情况一样,它在内部使用 HttpClient。
当然,您必须设置凭据提供程序:
AuthScope
不得包含领域,因为它事先是未知的。This is the same solution as Mat's Mannion's, but you don't have to put localContext to each request. It's simpler, but it adds authentication to ALL requests. Useful, if you don't have control over individual requests, as in my case when using Apache Solr, which uses HttpClient internally.
Of course, you have to set the credentials provider:
The
AuthScope
must not contain realm, as it is not known in advance.上面的很多答案都使用了已弃用的代码。我正在使用 Apache SOLRJ 版本 5.0.0。
我的代码由
PreemptiveAuthInterceptor 组成,现在如下:-
A lot of the answers above use deprecated code. I am using Apache SOLRJ version 5.0.0.
My code consists of
The PreemptiveAuthInterceptor is now as follows:-
聚会有点晚了,但我遇到了试图解决此问题的帖子请求的代理预授权的线程。为了补充亚当的回应,我发现以下内容对我有用:
认为这可能对遇到此问题的其他人有帮助。
A little late to the party but I came accross the thread trying to solve this for proxy pre-authorization of a post request. To add to Adam's response, I found the following worked for me:
Thought that might be helpful for anyone else who runs into this.
我认为最好的方法可能是手动完成。我添加了以下函数
Classic Java:
...
的实例
HTTPRequestBase
可以是HttpGet
或HttpPost
Android: ...
I think the best way may be to just do it manually. I added the following function
Classic Java:
...
HTTPRequestBase
can be an instance ofHttpGet
orHttpPost
Android:
...
我正在使用此代码,基于我对 HTTPClient 4.5 文档:
...并确保始终将该上下文传递给
HTTPClient.execute()
。I'm using this code, based on my reading of the HTTPClient 4.5 docs:
...and make sure you always pass that context to
HTTPClient.execute()
.我不太明白你的结束语。 HttpClient 拥有用于执行抢占式身份验证的所有机制,并且您只需执行一次(当您构造和配置 HttpClient 时)。完成此操作后,您可以像往常一样构建方法实例。您不“将 BasicHttpContext 添加到该方法中”。
我认为,您最好的选择是拥有自己的对象来设置抢占式身份验证所需的所有垃圾,并具有一个或多个简单方法来在给定的 HTTPMethod 对象上执行请求。
I don't quite get your closing comment. It's the HttpClient that has all of that machinery for doing preemptive auth, and you only have to do that once (when you construct and configure your HttpClient). Once you've done that, you construct your method instances the same way as always. You don't "add the BasicHttpContext" to the method.
Your best bet, I'd think, is to have your own object that sets up all of the junk required for preemptive auth, and has a simple method or methods for executing requests on given HTTPMethod objects.
SolrConfig:
PreemptiveAuthInterceptor:
SolrConfig:
PreemptiveAuthInterceptor:
在android中,Mat Mannion的答案无法解析https,仍然发送两个请求,你可以像下面这样做,技巧是附加authHeader与user-agent:
in android,Mat Mannion's answer can't resolve https,still send two requests,you can do like below,the trick is append authHeader with user-agent: