是否可以使用以下代码段的基本身份验证对本地运行的网站发出简单的帖子请求?
app.controller('post', function($scope, $http) {
$http.post("http://localhost:8888/angular//wp-json/posts", {'title':'Posting from Angular'})
.success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
document.getElementById("response").innerHTML = "It worked!";
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
document.getElementById("response").innerHTML= "It did not work!";
alert(status)
});
});
答案 0 :(得分:2)
当您说基本身份验证时,您的意思是HTTP基本身份验证吗?
如果这就是你的意思:那么不。 HTTP基本身份验证要求您的客户端(在本例中为您的Web浏览器)需要知道您的API密钥,并能够通过网络将它们发送到您的服务器。由于您在Web浏览器中运行Angular,这意味着访问您站点的任何用户理论上都可以通过检查浏览器的Javascript控制台以纯文本格式查看您的API凭据。
这是一个巨大的安全风险。
您要做的是使用OAuth2等协议和密码授予(此处有更多信息:https://stormpath.com/blog/token-auth-spa/)。
OAuth2允许你基本上这样做:
这是从浏览器访问经过身份验证的API服务的唯一安全方式。