首页 > 解决方案 > 在 Sails 处于生产模式时,Recaptcha POST 请求返回 Forbidden

问题描述

我的 Sails 应用程序有一个受 Google Recaptcha v2 保护的表单。我有一个具有不同域的开发应用程序和一个生产应用程序。两个域都在同一台服务器上,并具有共享的 https 证书。这两个域都在 Recaptcha 域列表中。

这是表单控制器:

  var formdata = this.req.body;

  const https = require('https');

  const options = {
    host: 'www.google.com',
    path: '/recaptcha/api/siteverify',
    port: 443,
    method: 'POST',
    body: {
      secret: "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
      response: this.req.body['g-recaptcha-response'],
      remoteid: this.req.connection.remoteAddress,
    },
  };
  
  const req = await https.request(options, (res) => {
  
    res.on('data', async function(d) {
      var emailAddress = formdata.emailAddress.toLowerCase();

      await Eoi.create({
        name: formdata.name,
        company: formdata.company,
        emailAddress: emailAddress
      })
    });
  });
  
  req.on('error', (e) => {
    console.error(e);
  });
  req.end();

  throw {redirect: '/'};

当我在开发应用程序上提交表单时,它提交并验证正常,检查 csrf 并从 Google 返回 200 结果。但是生产应用程序(目前具有完全相同的代码)不起作用。相反,它显示:

https://example.com/eoi

在地址栏中和页面上的“禁止”。

我已将sails 日志级别更改为详细,它显示的唯一错误是:

Error: Could not parse session id from cookie of connecting socket, and then failed again when trying to use a generated cookie. Something has possibly gone wrong with your session store configuration.

我认为这与这个问题没有太大关系。在生产模式下,我可以看到这两个应用程序之间的唯一区别。

我在这里想念什么?

更新 2021-05-24: 我刚刚在生产模式下提升了我的开发应用程序并得到了同样的错误。因此,这肯定与 Sails 在开发与生产模式中的行为方式不同有关。

标签: sails.jsrecaptcha

解决方案


推荐阅读