【问题标题】:Node Http Module & IIS - Setting Secure Cookie节点 Http 模块和 IIS - 设置安全 Cookie
【发布时间】:2021-10-11 12:12:23
【问题描述】:

我的目标是在 IIS 的节点应用程序中使用安全标记的 cookie。

我正在开发 Next.js 应用程序,并尝试在 IIS 上进行部署以满足一些公司需求。我已经通过将 iisnode、URL 重写模块、nodejs 安装到服务器来配置 IIS,并且大部分都遵循this 视频中的脚步。

SSR 和静态结构运行良好。 IIS 已经拥有自己的 SSL 证书。根据我的研究;

从 IIS 到运行 express 的节点应用程序的 iisnode 请求。 ssl 连接在 IIS 处终止,并且您的节点应用程序收到一个 http 要求。当应用程序通过安全连接需要 cookie 时,它 失败。

因此,当涉及到带有安全选项的 cookie 时,就会产生问题。据我了解,我需要告诉节点服务器信任代理。我在 express 中找到了一种方法,但我想继续使用 node http 模块,我认为必须有一种方法,因为 Express 只是在 http 模块之上制作的。不幸的是,与this Express 示例不同,我无法找到任何有关它的资源。

【问题讨论】:

    标签: node.js iis next.js iisnode


    【解决方案1】:

    参考这个article在重写规则中设置cookie。

    <rewrite>
    <outboundRules> 
        <rule name="Ensure secure Cookies" preCondition="Missing secure cookie">
            <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
            <action type="Rewrite" value="{R:0}; secure" />
        </rule>
        <preConditions>
            <preCondition name="Missing secure cookie">
                <!-- Don't remove the first line here, it does do stuff! -->
                <add input="{RESPONSE_Set_Cookie}" pattern="." />
                <add input="{RESPONSE_Set_Cookie}" pattern="; secure" negate="true" />
            </preCondition>
        </preconditions>
    </outboundRules>
    

    我不是节点专家。但是IIS url rewrite 可以使重写的url 也是https。在这种情况下,我认为node应用收到http请求的可能性很小。

    【讨论】:

    • 抱歉延迟反馈。我以前试过这个,遗憾的是我没有得到任何结果。我将尝试使用 express 而不是 node http 模块来构建我的服务器。之后,我将根据结果编辑此评论或创建答案。
    【解决方案2】:

    我通过创建另一个使用 express 构建的自定义服务器文件找到了我的解决方案。实际上,正如我所提到的,从一开始我就知道有一种方法可以通过 express 服务器实现信任代理设置。但是由于我已经在使用 node http 模块,所以我不想更改此结构,并想了解如何在此模块中完成此代理设置。我无法找到方法,但因为我知道 express.js 是基于节点 http 模块构建的;必须有办法。

    不幸的是我找不到它或者它更难实现然后表达 js。我已经有一个带有 http 模块的开发服务器,它还读取 ssl 证书等。我没有完全删除它,而是添加了另一个具有 expressjs 和代理设置的文件。

    以下代码没什么大不了,但它可能对某人有所帮助,因为它们提供了具有 SSL 环境的自定义 Next.js 服务器,用于开发和代理服务器生产。

    带快递和代理的服务器

    // This server file intended to use behind IIS.
    const next = require("next");
    const express = require('express')
    const port = process.env.PORT || 3000;
    const app = next({ dev: false, dir: ".", quiet: false });
    const handle = app.getRequestHandler();
    
    app.prepare().then(() => {
      const server = express();
      server.set('trust proxy', 1)
    
      server.get("*", (req, res) => {
        return handle(req, res);
      });
    
      server.listen(port, (err) => {
        if (err) {
          throw err;
        } else {
          console.log(`Server started at port ${port}`);
        }
      });
    });
    

    server.dev

    console.log("Development mode.");
    const { createServer } = require("https");
    const { parse } = require("url");
    const next = require("next");
    const fs = require("fs");
    
    const port = process.env.PORT || 3000;
    const app = next({ dev: devMode, dir: ".", quiet: false });
    const handle = app.getRequestHandler();
    
    const httpsOptions = {
      key: fs.readFileSync("./certs/cert.key"),
      cert: fs.readFileSync("./certs/cert.pem"),
    };
    
    app.prepare().then(() => {
      createServer(httpsOptions, (req, res) => {
        // Be sure to pass `true` as the second argument to `url.parse`.
        // This tells it to parse the query portion of the URL.
        const parsedUrl = parse(req.url, true);
        const { pathname, query } = parsedUrl;
        handle(req, res, parsedUrl);
      }).listen(port, (err) => {
        if (err) throw err;
        console.log(`Port: ${port}`);
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      相关资源
      最近更新 更多