You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Expo CLI中禁用SSL验证?已尝试两周未找到有效方法

Expo 应用禁用 SSL 验证的可行方案
  • 使用自定义开发客户端 + react-native-fs
    Expo 托管环境下原生能力受限,先构建自定义开发客户端,再通过react-native-fs的配置跳过 SSL 验证:

    import RNFS from 'react-native-fs';
    
    const requestOptions = {
      url: 'https://your-insecure-domain.com/api',
      method: 'GET',
      sslPinning: {
        certs: [], // 空证书数组表示不验证SSL证书
      },
    };
    
    RNFS.fetch(requestOptions)
      .then(res => res.json())
      .catch(err => console.error('请求失败:', err));
    

    ⚠️ 仅用于开发阶段,生产环境禁用会引发严重安全问题。

  • 裸工作流下修改原生配置
    如果使用 bare workflow,可直接修改原生代码实现域名级别的 SSL 豁免:

    • iOS:在Info.plist中添加 ATS 配置,针对目标域名关闭 SSL 验证:
      <key>NSAppTransportSecurity</key>
      <dict>
        <key>NSAllowsArbitraryLoads</key>
        <false/>
        <key>NSExceptionDomains</key>
        <dict>
          <key>your-insecure-domain.com</key>
          <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
          </dict>
        </dict>
      </dict>
      
    • Android:创建res/xml/network_security_config.xml配置文件:
      <?xml version="1.0" encoding="utf-8"?>
      <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
          <domain includeSubdomains="true">your-insecure-domain.com</domain>
        </domain-config>
      </network-security-config>
      
      然后在AndroidManifest.xml的application标签中引用:
      <application
        ...
        android:networkSecurityConfig="@xml/network_security_config">
      
  • 替代 rn-fetch-blob:使用 expo-file-system
    Expo 官方的expo-file-system可配合原生配置实现请求豁免,无需额外代码开关:

    import * as FileSystem from 'expo-file-system';
    
    try {
      const response = await FileSystem.fetchAsync(
        'https://your-insecure-domain.com/api',
        {}
      );
      const data = await response.json();
      console.log(data);
    } catch (error) {
      console.error('请求出错:', error);
    }
    

内容的提问来源于stack exchange,提问作者Balaji N R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 01:23:29