目前正在做一个基于 react native 的app,主要工作也是维护。就遇到一个关于SSL证书过期及更新证书的问题。由于之前没有处理过类似证书的问题,也是先入为主以为证书只会在后端或native(ios/android)端代码中才需要配置的原因,导致没有深入查找react native 里关于SSL 证书(证书文件)相关的内容。导致差点出现事故。哈哈
其实在 react native 里两个platform(ios/android)相关代码里有配SSL 相关的东西,但是不是文件啥的,而是 SSL Public Key Pinning(它是一个base64的字符串)。这个字符串是基于证书文件通过特殊命令生成的。
生成key 的条件及方式:
1,首先得有证书文件。
2,在终端运行如下命令:
openssl x509 -in my-certificate.crt -pubkey -noout|openssl pkey -pubin -outform der|openssl dgst -sha256 -binary|openssl enc -base64
openssl s_client -servername <domain_name> -connect <domain_name>:443 | openssl x509 -pubkey -noout | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64
其中 my-certificate.crt 就是第1步 证书文件在你电脑上的位置路径。在终端执行第2部命令,回车会生成一个base64 的字符串,这个字符串就是我们需要的SSL Public Key Pinning。(最好连外网)
到时候替换掉之前的 SSL Public Key Pinning 值就行。
这个 key 在react native 里的配置文件分别是:
iOS:AppDelegate.m 文件里查找 TrustKit 这个就会找到对应配置所在。
Android:SSLPinnerFactory.java (也可能不是这个名字,但肯定是跟SSL有关的文件)。
文章内容源引: