我正在尝试从 API 发布数据,但无法通过基本身份验证。
我尝试:
$.ajax({
type: 'POST',
url: http://theappurl.com/api/v1/method/,
data: {},
crossDomain: true,
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Basic [REDACTED]');
}
});
我的服务器配置响应是:
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "POST"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "*"
我得到的标题是:
请求标头
OPTIONS /api/v1/token-auth/ HTTP/1.1
Host: theappurl.com
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://127.0.0.1:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31
Access-Control-Request-Headers: origin, authorization, content-type
Accept: */*
Referer: http://127.0.0.1:8080/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
响应头
HTTP/1.1 401 Unauthorized
Server: nginx/1.1.19
Date: Fri, 16 Aug 2013 01:29:21 GMT
Content-Type: text/html
Content-Length: 597
Connection: keep-alive
WWW-Authenticate: Basic realm="Restricted"
我想服务器配置很好,因为我可以从高级REST客户端(Chrome扩展)访问API
有什么建议吗?
PD: 我从高级 REST 客户端获得的标头是:
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Authorization: Basic [REDACTED]
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
和
Server: nginx/1.1.19
Date: Fri, 16 Aug 2013 01:07:18 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept, Cookie
Allow: POST, OPTIONS
X-Robots-Tag: noindex
发送OPTION方法
根据 https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding 和 http://en.wikipedia.org/wiki/Basic_access_authentication ,以下是如何执行基本操作使用标头进行身份验证,而不是将用户名和密码放在 URL 中。请注意,这仍然不会向任何有权访问网络或此 JS 代码的人隐藏用户名或密码(例如,用户在浏览器中执行它):
$.ajax({
type: 'POST',
url: http://theappurl.com/api/v1/method/,
data: {},
crossDomain: true,
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(unescape(encodeURIComponent(YOUR_USERNAME + ':' + YOUR_PASSWORD))))
}
});
如果您想使用 NodeJS 执行此操作:使用
Authorization
标头创建 GET 到 JSON 端点并获取 Promise
返回:
第一
npm install --save request request-promise
(参见 npm),然后在您的
.js
文件中:
var requestPromise = require('request-promise');
var user = 'user';
var password = 'password';
var base64encodedData = Buffer.from(user + ':' + password).toString('base64');
requestPromise.get({
uri: 'https://example.org/whatever',
headers: {
'Authorization': 'Basic ' + base64encodedData
},
json: true
})
.then(function ok(jsonData) {
console.dir(jsonData);
})
.catch(function fail(error) {
// handle error
});
您可以将用户和密码作为 URL 的一部分:
http://user:[email protected]/index.html
请参阅此网址,了解更多
当然,你需要用户名密码,这不是
'Basic hashstring
。
如果您在浏览器环境中,也可以使用btoa。
btoa
是一个函数,它将字符串作为参数并生成 Base64 编码的 ASCII 字符串。 97% 的浏览器支持它。
示例:
> "Basic " + btoa("billy"+":"+"secretpassword")
< "Basic YmlsbHk6c2VjcmV0cGFzc3dvcmQ="
然后您可以将
Basic YmlsbHk6c2VjcmV0cGFzc3dvcmQ=
添加到 authorization
标题。
请注意,有关 HTTP BASIC 身份验证的常见警告适用,最重要的是,如果您不通过 https 发送流量,窃听者可以简单地解码 Base64 编码字符串,从而获取您的密码。
这个 security.stackexchange.com 答案很好地概述了一些缺点。
无需使用用户名和密码作为 URL 的一部分
你可以试试这个
byte[] encodedBytes = Base64.encodeBase64("user:passwd".getBytes());
String USER_PASS = new String(encodedBytes);
HttpUriRequest request = RequestBuilder.get(url).addHeader("Authorization", USER_PASS).build();
PHP - 卷曲:
$username = 'myusername';
$password = 'mypassword';
...
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
...
PHP - 在 WordPress 中发布:
$username = 'myusername';
$password = 'mypassword';
...
wp_remote_post('https://...some...api...endpoint...', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode("$username:$password")
)
));
...