是否可以在下面的代码段中使用basic auth向本地运行的站点发出简单的post请求?
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)
});
}); 发布于 2015-07-06 16:18:06
当您说基本身份验证时,您是指HTTP基本身份验证吗?
如果这就是你的意思:那就不。HTTP基本身份验证要求您的客户端(在本例中是web浏览器)需要知道您的API密钥并能够通过网络将它们发送到您的服务器。由于您在web浏览器内部运行角,这意味着任何访问您的站点的用户理论上都可以通过检查浏览器的Javascript控制台以纯文本形式查看您的API凭据。
这是一个巨大的安全风险。
您想要做的是使用像OAuth2这样的带有密码授予的协议(这里有更多信息:https://stormpath.com/blog/token-auth-spa/)。
OAuth2允许您基本上执行以下操作:
这是从浏览器访问经过身份验证的API服务的唯一安全方法。
https://stackoverflow.com/questions/31225352
复制相似问题