How can I provide a SSL certificate with create-react-app?
我正在尝试托管一个我使用Facebook样板在本地创建和测试的React应用。 Your connection is not private (NET:ERR_CERT_AUTHORITY_INVALID) 文档对我没有太大帮助: Note that the server will use a self-signed certificate, so your web browser will almost definitely display a warning upon accessing the page. 如何使用我自己的SSL证书(该证书已经在我域中的另一个应用程序上使用,并且像超级按钮一样工作),而不是这个自签名证书?我错过了什么吗? 更新:请参阅下面的安迪答复。 不建议 弹出
客户端应用程序与我使用node.js制作的API进行了交互,并且使用该API可以毫无问题地设置安全连接(通过node.js客户端发送我的SSL证书进行测试)。
但是,在使用react发送我的SSL证书而不是自签名证书时,我遇到了困难,这导致我使用chrome并尝试访问https://example.net:3000时遇到此错误:
https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#using-https-in-development
在最新版本中,您应该设置环境变量以配置证书
2
SSL_KEY_FILE=.cert/server.key
您将需要将证书复制到
这是我创建的脚本:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# With create-react-app, a self signed (therefore invalid) certificate is generated.
# 1. Create some folder in the root of your project
# 2. Copy your valid development certificate to this folder
# 3. Copy this file to the same folder
# 4. In you package.json, under `scripts`, add `postinstall` script that runs this file.
# Every time a user runs npm install this script will make sure to copy the certificate to the
# correct location
TARGET_LOCATION="./node_modules/webpack-dev-server/ssl/server.pem"
SOURCE_LOCATION=$(pwd)/$(dirname"./local-certificate/server.pem")/server.pem
echo Linking ${TARGET_LOCATION} TO ${SOURCE_LOCATION}
rm -f ${TARGET_LOCATION} || true
ln -s ${SOURCE_LOCATION} ${TARGET_LOCATION}
chmod 400 ${TARGET_LOCATION} # after 30 days create-react-app tries to generate a new certificate and overwrites the existing one.
echo"Created server.pem symlink"
您的
1 2 3 4 | "scripts": { ... "postinstall":"sh ./scripts/link-certificate.sh" } |
- 我的解决方案基于此线程
我能够在不使用
1 2 | SSL_CRT_FILE=.cert/server.crt SSL_KEY_FILE=.cert/server.key |
注意事项:
-
.cert 是我在项目根目录中创建的文件夹 - 我的生成SSL证书的脚本
- 关于这两个环境变量的官方文档
扩展Elad的答案:
- 按照https://github.com/webpack/webpack-dev-server/tree/master/examples/cli/https链接的说明创建自签名证书
-
将pem文件(包含证书和私钥)保存在项目中的某个位置(例如
/cert/server.pem ) -
修改您的package.json脚本:
1
2"start":"HTTPS=true react-scripts start",
"prestart":"rm ./node_modules/webpack-dev-server/ssl/server.pem && cp -f ./cert/server.pem ./node_modules/webpack-dev-server/ssl",
从该端口提供文件的服务器需要配置为使用SSL证书。我猜您正在该端口上使用webpack-dev-server(这是
您可以使用已配置的服务器来提供已编译的文件,也可以将webpack-dev-server配置为使用SSL证书。
为此,您可以使用webpack-dev-server \\的
NOTE: you need an extra
-- to pass arguments through npm run to the underlying command, e.g.npm start -- --cert ... .
如果要使用npm start(调用自定义启动脚本)来执行此操作,则必须编辑该启动脚本。您可能需要首先使用
这是开始脚本的源代码:https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/scripts/start.js#L230
我还应该注意,webpack-dev-server不适用于生产环境。
玩得开心!
这是在
这就是我所做的:
1 | bash <(wget -qO- https://gist.github.com/w35l3y/5bb7fe8508d576472136f35428019177/raw/local-cert-generator.sh) |
然后我双击并导入:
然后我修改了
1 2 | "start":"HTTPS=true react-scripts start", "prestart":"cp -f /path/to/server.pem ./node_modules/webpack-dev-server/ssl", |
非常重要的来源:
- https://serverfault.com/questions/226386/wget-a-script-and-run-it
- https://gist.github.com/w35l3y/5bb7fe8508d576472136f35428019177