Javascript 使用自签名 SSL 从 https 服务器获取到 localhost:port 的请求
Javascript get request from https server to localhost:port with self signed SSL
我配置了两台服务器,运行 在我的 Debian 服务器上。一台主服务器和一台 Elasticsearch(搜索引擎)服务器。
主服务器是 运行 在 https 节点服务器上,带有 NGINX 代理和购买的 SSL 证书。 Elasticsearch 服务器在 http 服务器上 运行。我添加了一个新的 NGINX 代理服务器以使用 self-signed SSL 证书重定向 https://localhost:9999 to http://localhost:9200。在 Elasticsearch 服务器上还有一个配置的身份验证,其中包含用户名和密码。
一切似乎都已正确配置,因为当我使用 -k[=48 从服务器终端向 https://localhost:9999 执行 curl 时,我可以从服务器获得成功的响应=] 选项绕过 self-signed 证书的验证,没有它,它不起作用。
我无法从我的 https 主服务器向我的 http 本地主机服务器发出 cross-domain 请求。因此我需要在我的本地主机服务器上配置 https。
没有 -k 选项:
curl: (60) SSL certificate problem: self signed certificate
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
使用 -k 选项:
{
"name" : "server-name",
"cluster_name" : "name",
"cluster_uuid" : "uuid",
"version" : {
"number" : "x.x.x",
"build_hash" : "abc123",
"build_date" : "Timestamp",
"build_snapshot" : false,
"lucene_version" : "x.x.x"
},
"tagline" : "You Know, for Search"
}
这是一个成功的 Elasticsearch 服务器响应。
所以完整的 curl 请求看起来像 curl -k https://localhost:9999/ --user username:password
。
So, the actual question:
我希望能够向该服务器发送一个简单的 jQuery AJAX 请求。我正在尝试以下请求 $.get('https://username:password@localhost:9999/')
但我得到 ERR_CONNECTION_REFUSED
.
我的猜测是 AJAX 请求没有绕过 self-signed 证书验证,因此它拒绝连接。
有什么简单的方法可以通过请求 headers 或类似的方法来解决这个问题吗?还是我需要购买 CA-certificate 才能与 AJAX 一起使用?
你是对的,问题是自签名 certificate.If 你尝试相同的请求,但作为 http 它会工作。
这是使 ElasticSearch 使用 https 的解决方法:
您需要实现自己的 Http 连接器:
var HttpConnector = require('elasticsearch/src/lib/connectors/http');
var inherits = require('util').inherits;
var qs = require('querystring');
var fs = require('fs');
function CustomHttpConnector(host, config) {
HttpConnector.call(this, host, config);
}
inherits(CustomHttpConnector, HttpConnector);
// This function is copied and modified from elasticsearch-js/src/lib/connectors/http.js
CustomHttpConnector.prototype.makeReqParams = function (params) {
params = params || {};
var host = this.host;
var reqParams = {
method: params.method || 'GET',
protocol: host.protocol + ':',
auth: host.auth,
hostname: host.host,
port: host.port,
path: (host.path || '') + (params.path || ''),
headers: host.getHeaders(params.headers),
agent: this.agent,
rejectUnauthorized: true,
ca: fs.readFileSync('publicCertificate.crt', 'utf8')
};
if (!reqParams.path) {
reqParams.path = '/';
}
var query = host.getQuery(params.query);
if (query) {
reqParams.path = reqParams.path + '?' + qs.stringify(query);
}
return reqParams;
};
module.exports = CustomHttpConnector;
然后像这样注册:
var elasticsearch = require('elasticsearch');
var CustomHttpConnector = require('./customHttpConnector');
var Elasticsearch = function() {
this.client = new elasticsearch.Client({
host: {
host: 'my.server.com',
port: '443',
protocol: 'https',
auth: 'user:passwd'
},
keepAlive: true,
apiVerison: "1.3",
connectionClass: CustomHttpConnector
});
}
https://gist.github.com/fractalf/d08de3b59c32197ccd65
如果您想不使用 ES 进行简单的 ajax 调用,您唯一可以做的就是提示用户访问该页面并在请求被拒绝时自行接受证书。
另见:
我配置了两台服务器,运行 在我的 Debian 服务器上。一台主服务器和一台 Elasticsearch(搜索引擎)服务器。
主服务器是 运行 在 https 节点服务器上,带有 NGINX 代理和购买的 SSL 证书。 Elasticsearch 服务器在 http 服务器上 运行。我添加了一个新的 NGINX 代理服务器以使用 self-signed SSL 证书重定向 https://localhost:9999 to http://localhost:9200。在 Elasticsearch 服务器上还有一个配置的身份验证,其中包含用户名和密码。
一切似乎都已正确配置,因为当我使用 -k[=48 从服务器终端向 https://localhost:9999 执行 curl 时,我可以从服务器获得成功的响应=] 选项绕过 self-signed 证书的验证,没有它,它不起作用。
我无法从我的 https 主服务器向我的 http 本地主机服务器发出 cross-domain 请求。因此我需要在我的本地主机服务器上配置 https。
没有 -k 选项:
curl: (60) SSL certificate problem: self signed certificate
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
使用 -k 选项:
{
"name" : "server-name",
"cluster_name" : "name",
"cluster_uuid" : "uuid",
"version" : {
"number" : "x.x.x",
"build_hash" : "abc123",
"build_date" : "Timestamp",
"build_snapshot" : false,
"lucene_version" : "x.x.x"
},
"tagline" : "You Know, for Search"
}
这是一个成功的 Elasticsearch 服务器响应。
所以完整的 curl 请求看起来像 curl -k https://localhost:9999/ --user username:password
。
So, the actual question:
我希望能够向该服务器发送一个简单的 jQuery AJAX 请求。我正在尝试以下请求 $.get('https://username:password@localhost:9999/')
但我得到 ERR_CONNECTION_REFUSED
.
我的猜测是 AJAX 请求没有绕过 self-signed 证书验证,因此它拒绝连接。
有什么简单的方法可以通过请求 headers 或类似的方法来解决这个问题吗?还是我需要购买 CA-certificate 才能与 AJAX 一起使用?
你是对的,问题是自签名 certificate.If 你尝试相同的请求,但作为 http 它会工作。
这是使 ElasticSearch 使用 https 的解决方法:
您需要实现自己的 Http 连接器:
var HttpConnector = require('elasticsearch/src/lib/connectors/http');
var inherits = require('util').inherits;
var qs = require('querystring');
var fs = require('fs');
function CustomHttpConnector(host, config) {
HttpConnector.call(this, host, config);
}
inherits(CustomHttpConnector, HttpConnector);
// This function is copied and modified from elasticsearch-js/src/lib/connectors/http.js
CustomHttpConnector.prototype.makeReqParams = function (params) {
params = params || {};
var host = this.host;
var reqParams = {
method: params.method || 'GET',
protocol: host.protocol + ':',
auth: host.auth,
hostname: host.host,
port: host.port,
path: (host.path || '') + (params.path || ''),
headers: host.getHeaders(params.headers),
agent: this.agent,
rejectUnauthorized: true,
ca: fs.readFileSync('publicCertificate.crt', 'utf8')
};
if (!reqParams.path) {
reqParams.path = '/';
}
var query = host.getQuery(params.query);
if (query) {
reqParams.path = reqParams.path + '?' + qs.stringify(query);
}
return reqParams;
};
module.exports = CustomHttpConnector;
然后像这样注册:
var elasticsearch = require('elasticsearch');
var CustomHttpConnector = require('./customHttpConnector');
var Elasticsearch = function() {
this.client = new elasticsearch.Client({
host: {
host: 'my.server.com',
port: '443',
protocol: 'https',
auth: 'user:passwd'
},
keepAlive: true,
apiVerison: "1.3",
connectionClass: CustomHttpConnector
});
}
https://gist.github.com/fractalf/d08de3b59c32197ccd65
如果您想不使用 ES 进行简单的 ajax 调用,您唯一可以做的就是提示用户访问该页面并在请求被拒绝时自行接受证书。
另见: