可以使用 jQuery 进行摘要式身份验证吗?
我正在尝试发送需要 HTTP 摘要身份验证的请求。
jQuery 中可以进行摘要吗?
如果是这样,这是否接近正确的方法?目前还没有工作。
<script type="text/javascript">
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
success: function() { alert('hello!'); },
error: function() { alert('error')},
beforeSend: setHeader
});
function setHeader(xhr){
xhr.setRequestHeader("Authorization", "Digest username:password");
xhr.setRequestHeader("Accept", "application/json");
}
</script>
I'm trying to send a request that requires HTTP Digest authentication.
Is Digest possible in jQuery?
If so, is this close to the correct way to do it? It's not currently working.
<script type="text/javascript">
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
success: function() { alert('hello!'); },
error: function() { alert('error')},
beforeSend: setHeader
});
function setHeader(xhr){
xhr.setRequestHeader("Authorization", "Digest username:password");
xhr.setRequestHeader("Accept", "application/json");
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,摘要式访问身份验证方案稍微复杂一些,因为它实现挑战-响应身份验证机制,需要以下步骤:
这意味着至少有两个请求/响应对。
每个 WWW-Authenticate 响应标头字段 的语法为:
因此,您需要解析digest-challenge以获取参数,以便能够为授权请求标头字段,语法如下:
该部分还描述了如何计算摘要响应参数。特别是,您可能需要 MD5 实现,因为这是此身份验证方案最常用的算法。
您可以从以下简单的标记化开始:
这会将 WWW-Authenticate 标头字段(如:) 转换
为:
然后您需要解析参数(检查存在性和有效性)并提取值。请注意,引用字符串值可以折叠,因此您需要展开它们(另请参阅 RFC 中取消引用函数
unq
的使用):有了这个,您应该能够自己实现。
No, the Digest Access Authentication Scheme is a little more complex as it implements a challenge-response authentication mechanism that requires the following steps:
This means there are at least two request/response pairs.
Each WWW-Authenticate response header field has the syntax:
So you need to parse the digest-challenge to get the parameters to be able to generate a digest-reponse for the Authorization request header field with the following syntax:
That section does also describe how the digest-response parameters are calculated. In particular, you will probably need an MD5 implementation as that’s the most commonly used algorithm for this authentication scheme.
Here is a simple tokenization that you can start with:
This will turn a WWW-Authenticate header field like:
into:
Then you need to parse the parameters (check existence and validity) and extract the values. Note that quoted-string values can be folded, so you need to unfold them (see also the use of the unquote function
unq
in the RFC):With this you should be able to implement that on your own.
使用普通的 javascript 是可能的。尝试digestAuthRequest.js:
https://github.com/inorganik/digest-auth-request
It is possible with vanilla javascript. Try digestAuthRequest.js:
https://github.com/inorganik/digest-auth-request
您应该尝试digestj jquery 插件。
http://code.google.com/p/digestj/
这是部分实现,但是足以帮助你度过难关。
You should try the digestj jquery plugin.
http://code.google.com/p/digestj/
It is a partial implementation but could be sufficient to help you get through.