node.js - Webpack DevServer -> 代理 HTTPS 资源 -> UNABLE_TO_VERIFY_LEAF_SIGNATURE

标签 node.js webpack https proxy webpack-dev-server

我正在使用具有以下设置的 Webpack DevServer:

devServer: {
    proxy: {
        '*': {
            target: 'https://localhost:44369',
            secure: true
        }
    },
    port: 8080,
    host: '0.0.0.0',
    hot: true,
    https: false
}

https://webpack.js.org/configuration/dev-server/#devserver-https

它在 HTTP 上运行良好,但是当我现在将我的代理切换到 HTTPS 时,我开始遇到错误。

在命令提示符中收到以下消息:

[HPM] Error occurred while trying to proxy request / from localhost:8080 to https://localhost:44369 (UNABLE_TO_VERIFY_LEAF_SIGNATURE) (https://nodejs.org/api/errors.html#errors_common_system_errors)

我尝试将 Node NODE_TLS_REJECT_UNAUTHORIZED 设置为 0 但这没有帮助。

new webpack.DefinePlugin({
    'process.env.NODE_TLS_REJECT_UNAUTHORIZED': 0
})

有什么方法可以访问生成的证书 CA 并将其添加到受信任的根证书颁发机构?这会有帮助还是我还需要更改其他内容?

如果我将 https 切换为 true,我会得到同样的错误。

Generating SSL Certificate

...

[HPM] Error occurred while trying to proxy request / from localhost:8080 to https://localhost:44369 (UNABLE_TO_VERIFY_LEAF_SIGNATURE) (https://nodejs.org/api/errors.html#errors_common_system_errors)

当我在 Chrome 中访问所需的资源时,我也收到以下错误:

enter image description here

更新:

我现在已将 webpack-dev-server 设置为使用与我的原始 Web 服务器 (https://localhost:44369) 相同的证书。我已验证 ThumbprintSerial number 两者相同。

https://webpack.js.org/configuration/dev-server/#devserver-pfx

https: true,
pfx: fs.readFileSync(path.resolve(__dirname, "../Client.WebProxy/App_Data/Certificate/") + '\\localhost.pfx'),
pfxPassphrase: 'securePassword'

enter image description here

我也尝试过使用 ssl-root-cas 注入(inject)证书,但我仍然遇到同样的错误。

var sslRootCAs = require('ssl-root-cas/latest')
sslRootCAs.inject();

也试过这个:

target: 'https://[::1]:44369',

https://github.com/saikat/react-apollo-starter-kit/issues/20#issuecomment-316651403

var rootCas = require('ssl-root-cas/latest').create();
rootCas.addFile(path.resolve(__dirname, "../Client.WebProxy/App_Data/Certificate/") + '\\localhost.pem');
require('https').globalAgent.options.ca = rootCas;

https://www.npmjs.com/package/ssl-root-cas

https: {
    key: fs.readFileSync(path.resolve(__dirname, "../Client.WebProxy/App_Data/Certificate/") + '\\localhost.key'),
    cert: fs.readFileSync(path.resolve(__dirname, "../Client.WebProxy/App_Data/Certificate/") + '\\localhost.crt'),
    ca: fs.readFileSync(path.resolve(__dirname, "../Client.WebProxy/App_Data/Certificate/") + '\\localhost.pem'),
}

https://webpack.js.org/configuration/dev-server/#devserver-https

最佳答案

为此花费了很多时间,但最终却如此简单。它的工作原理是将代理的 secure 设置为 false,然后通过 http 访问 webpack-dev-server。

https://webpack.js.org/configuration/dev-server/#devserverproxy

devServer: {
    proxy: {
        '*': {
            target: 'https://localhost:44369',
            secure: false
        }
    },
    port: 8080,
    host: '0.0.0.0',
    hot: true,
}

关于node.js - Webpack DevServer -> 代理 HTTPS 资源 -> UNABLE_TO_VERIFY_LEAF_SIGNATURE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621156/

相关文章:

node.js - AWS lambda 二进制文件 EACCES 错误

webpack - 使用带有 gulp 插件的 UglifyJS2 时保留许可证注释

node.js - 在 NextJS monorepo 中构建失败 — HookWebpackError : processor is not a function

node.js - 在路由器中设置 'views' 和 'view engine' (Express.js)

node.js - 为什么我无法使用 React 运行 Webpack?

ssl - Spinnaker 服务在 HTTPS 配置后未启动

ruby-on-rails - NET::ERR_CERT_AUTHORITY_INVALID https 为红色

c# - 为 c# Post 使用不受信任的 ssl 证书

javascript - Mongoose 保存时可以避免多层嵌套回调吗?

node.js - NPM 在 Windows 10 上启动时 NODE_PATH 抛出错误