简体   繁体   中英

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.

In the following links:
https://stackoverflow.com/a/21809393/322537
https://nodejs.org/dist/latest-v8.x/docs/api/https.html
I have found an example of how https servers are created. It is my understanding that the createServer function should run every time a client makes a request.

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.

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?

The plan is to then make cookies to identify clients and then to setup a login system. 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. However, upon moving to https, the request is never recieved by the server. 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. What you have is very close, however your setup for https.createServer({options}, handler), needs adjustment.

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":

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.


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.

//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:

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. Such certificates are called chained root certificates. Source

intermediate.crt will have the following structure:

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

Here is the response in the browser.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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