Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何避免;“网站不安全”;在HTTPS配置的Express服务器上_Javascript_Node.js_Express_Ssl_Https - Fatal编程技术网

Javascript 如何避免;“网站不安全”;在HTTPS配置的Express服务器上

Javascript 如何避免;“网站不安全”;在HTTPS配置的Express服务器上,javascript,node.js,express,ssl,https,Javascript,Node.js,Express,Ssl,Https,我正在创建一个Express应用程序,我想在其中使用HTTPS。我在StartSSL.com上创建了一个证书,并将其导入到我的服务器中,现在可以正确地建立与服务器的连接 但有点不对劲: 为什么会发生这种情况?我是否必须以某种方式添加中间证书?这个证书应该已经签名了,尽管我可能错了。这是我在index.js中的配置部分: var bodyParser = require('body-parser'); app.use(bodyParser.json()); // support json enc

我正在创建一个Express应用程序,我想在其中使用HTTPS。我在StartSSL.com上创建了一个证书,并将其导入到我的服务器中,现在可以正确地建立与服务器的连接

但有点不对劲:

为什么会发生这种情况?我是否必须以某种方式添加中间证书?这个证书应该已经签名了,尽管我可能错了。这是我在index.js中的配置部分:

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(force_https);
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

app.use(helmet());
app.disable('x-powered-by');
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
  });

// Setup HTTPS
const httpsPort = 3443;
const options = {
  key: fs.readFileSync("./key.pem", "utf8"),
  cert: fs.readFileSync("./cert.pem", "utf8")
};

var secureServer = https.createServer(options, app).listen(httpsPort, () => {
    console.log(">> CentraliZr listening at port "+httpsPort);
});
我的证书包含此内容(共享它没有问题;这只是一个测试应用程序):

因此,有两个问题:

  • 为什么会发生这种错误
  • 我发送到那里的信息至少是加密的吗
  • 谢谢大家!

    编辑:这是我的导航器引发的错误:

    NET::ERR\u CERT\u AUTHORITY\u无效


    这就是我如何使用HTTP2和SSL设置我的服务器,希望能有所帮助

    const fs = require('fs');
    const path = require('path');
    const http = require('http');
    const https = require('https');
    const spdy = require('spdy')
    const express = require('express');
    const privateKey  = fs.readFileSync('certs/server.key', 'utf8');
    const certificate = fs.readFileSync('certs/server.crt', 'utf8');
    const credentials = {key: privateKey, cert: certificate};
    const app = express();
    
    app.use(express.static(path.join(__dirname, 'build')));
        app.get('*', (req, res) => {
           res.sendFile(path.join(__dirname + '/build/index.html'));
        });
    
    const httpServer = http.createServer(app);
    const httpsServer = spdy.createServer(credentials, app);
    
    httpServer.listen(80);
    httpsServer.listen(443);
    
    console.log('Server started over HTTP2 protocol.');
    

    这就是我如何使用HTTP2和SSL设置我的服务器,希望能有所帮助

    const fs = require('fs');
    const path = require('path');
    const http = require('http');
    const https = require('https');
    const spdy = require('spdy')
    const express = require('express');
    const privateKey  = fs.readFileSync('certs/server.key', 'utf8');
    const certificate = fs.readFileSync('certs/server.crt', 'utf8');
    const credentials = {key: privateKey, cert: certificate};
    const app = express();
    
    app.use(express.static(path.join(__dirname, 'build')));
        app.get('*', (req, res) => {
           res.sendFile(path.join(__dirname + '/build/index.html'));
        });
    
    const httpServer = http.createServer(app);
    const httpsServer = spdy.createServer(credentials, app);
    
    httpServer.listen(80);
    httpsServer.listen(443);
    
    console.log('Server started over HTTP2 protocol.');
    
  • 证书的主题必须与您访问的域匹配。该证书似乎对
    localhost
    无效。事实上,一个合适的CA几乎不可能为您颁发
    localhost
    的证书,因为他们不可能验证该域
  • Chrome和许多其他浏览器都是有效的
  • localhost
    获取有效证书的唯一方法是创建一个自签名根证书,将其安装在所有相关的本地信任存储中,并使用该证书对自创建的证书进行签名

  • 证书的主题必须与您访问的域匹配。该证书似乎对
    localhost
    无效。事实上,一个合适的CA几乎不可能为您颁发
    localhost
    的证书,因为他们不可能验证该域
  • Chrome和许多其他浏览器都是有效的

  • localhost
    获取有效证书的唯一方法是创建一个自签名根证书,将其安装到所有相关的本地信任存储中,并使用它来签名一个自创建的证书。

    连接到
    https://localhost
    与连接到
    https://centralizr.com
    。您没有本地主机的证书。请单击/!\浏览器会告诉你问题出在哪里。@James嗯,是的,我甚至可能没有为这个测试应用程序使用域,但是,数据包的内容会被加密吗?如果我想在prod中使用它,注册同一个域并在那里获得应用程序,会起作用吗?正如前面指出的
    localhost
    不等于
    centralizer.com
    ,现在如果您的windows上有一个快速而肮脏的解决方案,使其有效,就是将
    centralizer.com
    的主机文件改为指向
    127.0.0.1
    。Linux/Mac您也应该能够做到这一点。你甚至可能有一个路由器,你可以在上面做这件事。对于开发,虽然我们有一个*.domain证书,但我们随后创建了一个名为local的子域,然后将其指向我们的内部开发服务器,我们在开发过程中获得了很好的有效证书。连接到
    https://localhost
    与连接到
    https://centralizr.com
    。您没有本地主机的证书。请单击/!\浏览器会告诉你问题出在哪里。@James嗯,是的,我甚至可能没有为这个测试应用程序使用域,但是,数据包的内容会被加密吗?如果我想在prod中使用它,注册同一个域并在那里获得应用程序,会起作用吗?正如前面指出的
    localhost
    不等于
    centralizer.com
    ,现在如果您的windows上有一个快速而肮脏的解决方案,使其有效,就是将
    centralizer.com
    的主机文件改为指向
    127.0.0.1
    。Linux/Mac您也应该能够做到这一点。你甚至可能有一个路由器,你可以在上面做这件事。对于开发,虽然我们有一个*.domain证书,但我们随后创建了一个名为local的子域,我们将其指向我们的内部开发服务器,并在开发过程中获得了很好的有效证书。很棒的代码片段!问题不在于。。。让我编辑我的问题,我有更多的信息。伟大的片段!问题不在于。。。让我编辑我的问题,我有更多的信息。谢谢你的信息!无论如何,这个服务器和我自己的另一个应用程序之间的通信会被加密吗?如果HTTPS连接正在建立,会。它只是警告您服务器的身份无法验证,因此您正在通过安全通道与某人交谈,但某人可能不是您所认为的人。谢谢您提供的信息!无论如何,这个服务器和我自己的另一个应用程序之间的通信会被加密吗?如果HTTPS连接正在建立,会。它只是警告您服务器的身份无法验证,因此您正在通过安全通道与某人交谈,但某人可能不是您所认为的人。