从本地NextJS调用本地dotnet https后端会导致FetchError:自签名证书

s4chpxco  于 2023-05-06  发布在  其他
关注(0)|答案(2)|浏览(245)

我最近不得不将我的本地dotnet core 6后端开发切换到现在在HTTPS上运行,因为后端和前端之间传递了一些安全cookie。不幸的是,现在我在从我的API调用它时遇到了一些问题,如下所示
错误- FetchError:请求https://localhost:5001/api/user/login失败,原因:自签名证书
我猜这是因为我没有在next.js本地服务器上使用https,但我不完全理解为什么或如何去做。任何帮助无论是解决方案或理解的问题将不胜感激。请在下面找到我的NextJS API中有问题的代码块。

import type { NextApiRequest, NextApiResponse } from 'next';
import { User } from '../../models/User';

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<User>
) {
  console.log(req.body);
  await fetch('https://localhost:5001/api/user/login', {
    body: req.body,
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    }
  })
    .then((res) => res.json())
    .then((response: User) => {
      res.status(200)
        .json(response);
      console.log('res', response);
    });
}
zpqajqem

zpqajqem1#

您需要使用根CA签署证书。然后将其添加到受信任的根证书。

但您可以按照以下步骤生成新的
1.创建RootCA.pem和RootCA.key
1.创建.csr文件
1.使用RootCA发布
1.将其添加到信任根证书
还需要配置请求文件和domain.ext

sudo openssl req -new -nodes -days 720 -newkey rsa:2048 -keyout /etc/ssl/private/$Domain.key -out /etc/ssl/certs/$Domain.csr -config /certs/openssl.cnf
       
sudo openssl x509 -req -sha256 -days 720 -in /etc/ssl/certs/$Domain.csr -CA /certs/RootCA.pem -CAkey /certs/RootCA.key -CAcreateserial -extfile /certs/domains.ext -out /etc/ssl/certs/$Domain.crt
mu0hgdu0

mu0hgdu02#

您可以在env文件中使用NODE_TLS_REJECT_UNAUTHORIZED = 0。但这只能是为了当地的发展。谢谢!推荐的解决方案:'Error: self signed certificate' with node+express application

相关问题