在 ReactNative 应用程序中使用 fetch API 忽略自签名 SSL 证书的错误? [英] Ignore errors for self-signed SSL certs using the fetch API in a ReactNative App?

查看:88
本文介绍了在 ReactNative 应用程序中使用 fetch API 忽略自签名 SSL 证书的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个小型 ReactNative iOS 应用程序.在其中,我使用 fetch API 向我控制的服务器发出简单的 get 请求,该服务器具有有效但自签名的 SSL 证书.可以理解,这会导致错误.

I'm building a small ReactNative iOS application. In it, I use the fetch API to make a simple get request to a server I control that has a valid - but self-signed - SSL cert. Understandably, this causes an error.

在 iOS/JSC 之间,我不确定如何(或什么!)我可以配置我的应用程序以忽略此错误 - 到目前为止我找到的所有答案都与 Objective-C 解决方案有关,我正在寻找我可以在 JSC 环境中使用的东西.

Between iOS/JSC, I'm unsure how (or what!) I can configure for my app to ignore this error - all the answers I've found so far are related to Objective-C solutions, I'm looking for something I can use with a JSC environment.

推荐答案

我遇到了同样的问题.正如您所指出的,让原生 iOS 应用程序使用自签名证书的唯一方法似乎是编写/修改 Objective-C 代码,这对于使用 React Native 的 JavaScript 开发人员来说不是一个好方法.出于这个原因,我认为你的问题是一个X/Y 问题,我建议用不同的方法解决你的整体问题从一开始就使用自签名证书.

I encountered this same issue. As you noted, it seems the only way to get a native iOS app to work with a self-signed certificate is by writing/modifying Objective-C code, which is not a good approach for a JavaScript developer using React Native. For this reason, I think your question is an X/Y problem, and I propose solving your overall problem with a different approach from using a self-signed certificate in the first place.

我使用真正的证书进行开发.这是我为使本地开发 API 与 SSL 和 React Native 一起工作所做的工作.它免费且简单.

I use a real certificate for development instead. Here is what I did to get my local development API working with SSL and React Native. It's free and simple.

  • ssh 进入与您的域相关联的公共服务器
  • 安装letsencrypt
  • 为您的开发子域生成证书
    • dev.my-domain.com 用于在本地开发我的网站/网络应用
    • api.dev.my-domain.com 用于 api
    • ./letsencrypt-auto certonly --standalone -d dev.my-domain.com -d api.dev.my-domain.com
    • 可能在 /etc/letsencrypt/live/dev.my-domain.com
    • 下找到
    • 从远程机器获取文件的一种方法:scp -r your_user@123.123.(...):/etc/letsencrypt/live/dev.my-domain.com ./
    • 您可以在要使用域的每台机器上修改您的 hosts 文件
    • 如果你的路由器有 dnsmasq,你可以为整个网络做类似 address=/dev.my-domain.com/192.168.(...) 的事情(我推荐这个)莉>
    • you can modify your hosts file on each machine you want to use the domain
    • if your router has dnsmasq, you can do something like address=/dev.my-domain.com/192.168.(...) for the whole network (I recommend this)

    此时,由于您正在为正在访问的域使用真实可信的证书,因此您的 api 现在将在浏览器和正在开发的设备上受到信任!

    At this point, because you are using a real, trusted certificate for the domain you're accessing, your api will now be trusted in browsers and on devices in development!

    这篇关于在 ReactNative 应用程序中使用 fetch API 忽略自签名 SSL 证书的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆