gpt4 book ai didi

基于请求体的angularjs资源身份验证 header

转载 作者:行者123 更新时间:2023-12-02 22:49:31 25 4
gpt4 key购买 nike

我正在尝试将资源与受 HMAC 身份验证方法保护的 API 一起使用。所以我需要将“Authentication” header 附加到请求中。

在此示例代码中,我使用 GET 从 API 获取文章,并使用“更新”自定义方法对其进行更新。对于更新我需要身份验证 header 。问题是,当我定义 header 时,$scope.article未定义

getAuth 函数计算符号。

建议?

function EditCtrl($scope,$resource,articleId) {
var Article = $resource('/blog/articles/:articleId',
{articleId:articleId}, {
update: {
method:'PUT',
headers: {Authentication: getAuth('key','PUT','/blog/articles/'+articleId,$scope.article)}
}
});

var article = Article.get();
$scope.article = article;

$scope.save = function(){
article.$update();
}
}

function getAuth(key,verb,resource,data) {
//data is undefined there

content_md5 = CryptoJS.MD5(JSON.stringify(data));
message = verb+'\n'+resource+'\n'+content_md5;
hash = CryptoJS.HmacSHA512(message, key);

var sign = "AuthHMAC 0123456789:"+hash.toString(CryptoJS.enc.Base64);

return sign;
}

最佳答案

我使用三个元素改进了艾伦的解决方案:

  • 共享 key 的工厂服务
  • 发送 httpRequest 的 Controller
  • 设置 header 的httpRequestInterceptor

Controller 调用工厂服务提供的 setKey() 来计算 header 。最后,httpResponseInterceptor调用工厂服务提供的getKey()设置请求头。

解决了!

关于基于请求体的angularjs资源身份验证 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17569834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com