HttpUnit 中领域的挑战

发布于 2024-11-07 19:11:47 字数 279 浏览 0 评论 0 原文

Webclient 的 HttpUnit API 表示“仅在针对指定领域进行质询时才发送授权标头。”在这种情况下,“挑战”意味着什么? HttpUnit 如何识别挑战?

The HttpUnit API for webclient says that "Will only send the authorization header when challenged for the specified realm." What does challenged mean in this case? How does HttpUnit recognize a challenge?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾铮苏瑾 2024-11-14 19:11:47

这是指 HTTP 身份验证的工作方式:

当访问受保护的 URL 时(第一次,请求中不包含凭据),服务器将发回状态代码为 401 UnauthorizedWWW-Authenticate 标头设置为 Basicrealm="My Realm" 之类的内容。这表明给定 URL 需要基本身份验证,并且领域名为“My Realm”。这就是挑战 - 服务器通知用户代理它尝试访问的 URL 需要身份验证,并且它应该发回用户凭据。用户代理通常会提示用户输入凭据,然后重试请求,这次将 Authorization 标头设置为 Basic rXflcjMwYXxz 之类的内容,其中第二部分是 Base64 编码用户名和密码对。

对于您链接到的 HttpUnit 方法,您将看到它需要领域、用户名和密码。我想象当访问 URL 时,如果它从服务器返回 401(质询),它会将您传递给它的领域与响应中的领域进行比较;如果匹配,它将尝试使用提供的用户名和密码进行身份验证。

参考文献:

This refers to the way HTTP Authentication works:

When accessing a protected URL (for the first time, with no credentials included in the request), the server will send back a response that has a status code of 401 Unauthorized and a WWW-Authenticate header set to something like Basic realm="My Realm". This indicates that Basic authentication is needed for the given URL and the realm is named 'My Realm'. This is the challenge - the user agent is being informed by the server that the URL it tried to access requires authentication and it should send back the user credentials. The user agent will typically prompt the user for credentials and then retry the request, this time with a Authorization header set to something like Basic rXflcjMwYXxz where the second part is the Base64 encoded username and password pair.

In case of the HttpUnit method you've linked to, you'll see that it requires a realm, username and password. I imagine that when the a URL is accessed, if it gets back a 401 (the challenge) from the server, it'll compare the realm you passed it with the realm in the response; if it matches, it'll attempt to authenticate with the username and password supplied.

References:

莫多说 2024-11-14 19:11:47

当服务器响应 401 错误时,HttpUnit 会抛出 AuthorizationRequiredException。我们可以使用异常的 getParameter("realm") 来获取领域并使用该领域名称再次发送请求。

When the server responds with a 401 error, the HttpUnit throws an AuthorizationRequiredException. We can use getParameter("realm") of the exception to get the realm and send a request again with this realm name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文