简体   繁体   English

在nodejs中设置https cookie

[英]setting up https cookie in nodejs

I am trying too setup a login system on my website.我也在尝试在我的网站上设置登录系统。

Heard that nodejs cookies are a good way to do that.听说 nodejs cookies 是一个很好的方法。

In the following links:在以下链接中:
https://stackoverflow.com/a/21809393/322537 https://stackoverflow.com/a/21809393/322537
https://nodejs.org/dist/latest-v8.x/docs/api/https.html https://nodejs.org/dist/latest-v8.x/docs/api/https.html
I have found an example of how https servers are created.我找到了如何创建 https 服务器的示例。 It is my understanding that the createServer function should run every time a client makes a request.据我了解,每次客户端发出请求时,createServer function 都应该运行。

So I have the following in my code:所以我的代码中有以下内容:

var server_https=modules.https.createServer({
    key: this.ssl_key,
    cert:this.ssl_cert
    },this.respond_to_client).listen(this.port);


mconnection.prototype.respond_to_client=function(request,response){
    console.log('responded to client');
    }

The server appear to run fine as the website is up and running.随着网站的启动和运行,服务器似乎运行良好。 But the respond_to_client function appears to never run as nodejs's log file never indicates the 'responded to client' string.但是respond_to_client function 似乎永远不会运行,因为 nodejs 的日志文件从未指示“响应客户端”字符串。

How could that be?怎么可能? Could it have something to do with that I'm upgrading the https server to a websocket shortly later in the code?这可能与我稍后在代码中将 https 服务器升级到 websocket 有关吗?

The plan is to then make cookies to identify clients and then to setup a login system.计划是让 cookies 识别客户端,然后设置登录系统。 But I'm stuck at this.但我坚持这一点。 /: /:

I have replicated your node scripts for local testing.我已经复制了您的节点脚本以进行本地测试。 I first got things running on http (vs https), and was able to get response just fine.我首先在 http(vs https)上运行,并且能够得到很好的响应。 However, upon moving to https, the request is never recieved by the server.但是,在移动到 https 后,服务器永远不会收到请求。 The browser MUST first establish a secure connection before the actual request is sent.在发送实际请求之前,浏览器必须首先建立安全连接。

I ran into a similar issue when trying to run multiple servers (https and ws) on the same port.尝试在同一端口上运行多个服务器(https 和 ws)时,我遇到了类似的问题。 What you have is very close, however your setup for https.createServer({options}, handler), needs adjustment.您所拥有的非常接近,但是您对 https.createServer({options}, handler) 的设置需要调整。

Where you have:你在哪里:

var server_https= https.createServer({
        key: this.ssl_key,
        cert:this.ssl_cert
},respond_to_client).listen(this.port);

You need to also add an option for "ca":您还需要为“ca”添加一个选项:

var server_https= https.createServer({
        key: this.ssl_key,
        cert: this.ssl_cert,
        ca: this.ssl_ca,  // also add this
},respond_to_client).listen(this.port);

The value I have used for "ca" has been the contents of the file: intermediate.crt received from the certificate signing authority.我用于“ca”的值是文件的内容:intermediate.crt 从证书签名机构收到。


IMPORTANT重要的

While it may be possible to get this working using a self signed certificate, I have never been able to do so as there is no signing authority.虽然可以使用自签名证书来完成这项工作,但我从来没有这样做过,因为没有签名授权。


So just like you have done for your other certificate files, you should also do this for the intermediate.crt file.因此,就像您对其他证书文件所做的那样,您也应该对 middle.crt 文件执行此操作。

//Where you read your other cert files: add another.
this.ssl_ca = modules.fs.readFileSync(this.ssl_ca_pathfile);

I found this to be difficult and poorly documented.我发现这很困难,而且记录不充分。 I am not an expert on SSL/TSL, however a quick search on intermediate certificate turn up:我不是 SSL/TSL 方面的专家,但是可以快速搜索中间证书:

An intermediate certificate is a subordinate certificate issued by the trusted root specifically to issue end-entity server certificates.中间证书是受信任的根颁发的专门用于颁发终端实体服务器证书的从属证书。 The result is a certificate chain that begins at the trusted root CA, through the intermediate and ending with the SSL certificate issued to you.结果是一个证书链,从受信任的根 CA 开始,通过中间证书并以颁发给您的 SSL 证书结束。 Such certificates are called chained root certificates.此类证书称为链式根证书。 Source 资源

intermediate.crt will have the following structure: middle.crt 将具有以下结构:

-----BEGIN CERTIFICATE-----
****
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
****
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
****
-----END CERTIFICATE-----

Here is the response in the browser.这是浏览器中的响应。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM