我正在try 与具有自签名证书的https服务器通信.

我可以在家里做这件事.NET应用程序(使用ServicePointManager.ServerCertificateValidationCallback事件)、本机iOs应用程序(使用allowsAnyHTTPSCertificateForHost)或web浏览器(只需声明证书受信任).

但我无法让它在react-native 应用程序中工作(无论是在Android还是在iOS模拟器中).

我try 过不同的事情,但仍然没有成功.

I know there are some similar topics there:
Ignore errors for self-signed SSL certs using the fetch API in a ReactNative App?
React Native XMLHttpRequest request fails if ssl (https) certificate is not valid
Fetch in react native wont work with ssl on android
Problems fetching data from a SSL based Server
Unable to make API calls using react-native

但它们要么不包含答案,要么不起作用(而且它们根本不涉及android编程).搜索其他资源也没有什么成效.

我相信应该有一个简单的方法来使用自签名证书.我错了吗?有人知道吗(iOS和Android都知道)?

推荐答案

免责声明:此解决方案应该是临时的,并且有文档记录,这样它就不会停留在软件的生产阶段,这只是为了开发.

对于iOS,你所要做的就是打开你的xcodeproject(在RN的iOS文件夹中),一旦打开了,就转到RCTNetwork.在该项目中,导航到RCTHTTPRequestHandler.M

在该文件中,您将看到这样一行:

#pragma mark - NSURLSession delegate

就在那一行之后,添加这个函数

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
  completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}

瞧,您现在可以在没有有效证书的情况下对API进行不安全的调用.

这应该足够了,但如果你仍然有问题,你可能需要go 你的项目的信息.plist,左键点击它并 Select open as...源代码.

最后再加上

<key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>
  <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
        <key>subdomain.example.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>

所以你的文件看起来像这样

    ...
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string></string>
  <key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>
  <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
        <key>subdomain.example.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
</plist>

对于一个真正的生产就绪解决方案,https://stackoverflow.com/a/36368360/5943130该解决方案更好

React-native相关问答推荐

类似Snapchat的Reaction Native筛选器

到达安装依赖项时 EAS 构建错误

根据搜索字符串中的每个字母过滤数组

用图像 react native 动画进度条

如何将 react-native 图像从 expo-image-picker 上传到使用 multer 的 Express.js 后端

React Native 上传到 S3 存储空间

使用react native获取设备令牌

缺少请求的请求令牌

textAlignVertical不是有效的样式属性

如何在 array.map 中的每个元素之后添加逗号,除了 React JSX 中的最后一个元素

在特定屏幕上使用react-navigation修改后退按钮

Android 依赖 'com.google.android.gms:play-services-stats' 的编译(16.0.1)和运行时(17.0.0)类路径有不同的版本

是否可以在 react-native 的构造函数中调用异步函数?

无法识别的 WebSocket 连接选项 `agent`、`perMessageDeflate`、`pfx`、`key`、`passphrase`... 你的意思是把这些放在 `headers` 下吗?

Ipad 上的 React Native - 错误 - could not connect to development server

如何使用特定目标运行 react-native run-ios

在 webview 中检测touch 是 Apple Pencil 还是手指(react-native)

KeyboardAvoidingView - 隐藏键盘时重置高度

在 React Native ScrollView 中检测滚动结束,对齐页面

无法运行 jetifier React Native