【问题标题】:Insecure Reponse on https access with ExpressJS/NodeJS使用 ExpressJS/NodeJS 对 https 访问的不安全响应
【发布时间】:2016-01-31 10:54:32
【问题描述】:

我在通过 wamp 构建的自定义网站上经常遇到这个问题。

我按照说明如何为 ssl 生成自签名证书和私钥。然后我使用 nodejs/expressjs 创建一个 https 服务器,为我的网站上的数据使用 web api。然后我使用 angularjs 来显示数据。

现在令人困惑的是,有时它可以在 Google Chrome 上运行。但是,它在 firefox、opera 和 Microsoft Edge 上肯定会失败。

在 angularjs 文件中我有这个:

testControllers.controller('summoner-by-name', ['$scope', '$http', '$resource', function($scope, $http, $resource) {
$scope.summonerName = {text: 'abc'};    
$scope.items = regions; 
$scope.submit = function ()
{
    setTimeout(function() {
            data = {"summonerName": $scope.summonerName.text, "region": $scope.items.selectedOption.name, "PID": "000"};
                $http({
                    method: 'POST',
                    url: 'https://localhost:3030/custom_Project',
                    dataType: "json",
                    headers: {
                        "Content-Type": "application/json",
                        "Content-Length": data.length
                    },
                    data: data 
                }).then(function(response) {
                    $scope.posts = response.data;
                });
    }, 1000);
}
}]);

...

在节点 js 文件上,我有这个:

app.use(function (req, res, next) {

res.setHeader('Access-Control-Allow-Origin', 'https://custom_site.com');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});

var options = {
key: fs.readFileSync('C:/wamp/www/custom_Project/ssl_certs/private.key'),
cert: fs.readFileSync('C:/wamp/www/custom_Project/ssl_certs/public.crt'),
ca: fs.readFileSync('C:/wamp/bin/php/php5.5.12/cacert.pem'),
};

app.post('/custom_Project', function (req, res) {

...

}

https.createServer(options, app).listen(3030);

以下是我在尝试从 nodejs/expressjs 服务器检索数据时在控制台上收到的消息:

微软边缘:

SCRIPT7002:XMLHttpRequest:网络错误 0x80070005,访问被拒绝。

火狐:

跨域请求被阻止:同源策略不允许读取位于https://localhost:3030/custom_Project 的远程资源。 (原因:CORS 请求失败)。

Opera(偶尔在 Chrome 上):

选项https://localhost:3030/custom_Project net::ERR_INSECURE_RESPONSE

我假设它是因为证书是自签名的,但我可能错了。有什么想法吗?

【问题讨论】:

  • 更新:我决定尝试一些建议,看看是否可以解决我的问题。到目前为止,这些是我尝试过的,但它们还没有为我解决问题: 1. 我通过 npm 添加了 cors 模块; 2. 我在我的根文件夹中添加了一个 .htaccess 文件,它允许使用 * 进行访问控制允许来源; 3. 我编辑了 httpd.conf 以包含 Access-control-allow-origin;
  • 我遇到了完全相同的问题。

标签: angularjs node.js express xmlhttprequest


【解决方案1】:

我找到了解决问题的方法。

对于 firefox 和 opera,我在开发人员工具的网络选项卡上跟踪了错误。由于证书问题,请求未通过节点 js 脚本。所以,使用这些浏览器,我导航到https://localhost:3030/custom_Project

提示通知我该站点不受信任,要导航到该站点,我必须将其添加到受信任站点列表中。之后,我运行了 ajax post call,它成功地向我展示了结果。

然而,Edge 并没有给我带来任何运气。它在左上角有一个错误,指出该站点不受信任,我无法像使用 firefox、opera 和 chrome 那样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-12
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多