首页 > 解决方案 > React-Native axios https

问题描述

react-native 如何为 HTTPS 请求使用自签名证书?

axios({
        method: 'POST',
        url: url,
        headers: headers,
        data: params,
        timeout: timeout,
        // httpsAgent: new https.Agent({ rejectUnauthorized: false }),
    })

标签: react-nativeaxios

解决方案


我能够使用TrustKit在我的 React Native Application 的 iOS 部分启用 SSL 固定。我也在使用 Axios 进行服务器交互。有两种方法可以实现 TrustKit,通过代码或使用 Info.plist。我已经使用了 Info.plist,您可以在下面找到相同的实现:

  1. 在您的 podfile 中添加并安装 TrustKit。( pod 'TrustKit')
  2. 打开你的 Info.plist 作为源代码添加下面的代码。
<key>TSKConfiguration</key>
<dict>
    <key>TSKSwizzleNetworkDelegates</key>
    <true/>
    <key>TSKPinnedDomains</key>
    <dict>
        <key>yourDomain.com</key>
        <dict>
            <key>TSKPublicKeyHashes</key>
            <array>
                <string>public key 1</string>
                <string>public key 2</string>
            </array>
            <key>TSKPublicKeyAlgorithms</key>
            <array>
                <string>TSKAlgorithmRsa2048</string>
            </array>
            <key>TSKIncludeSubdomains</key>
            <true/>
            <key>TSKEnforcePinning</key>
            <true/>
        </dict>
    </dict>
</dict>

需要注意的重要事项:

  1. TSKSwizzleNetworkDelegates需要设置为真。
  2. yourDomain.com是您的 API 的基本 URL。
  3. public Key 1并且public Key 2是您的 API 的公钥。您可以在此处获取任何公共域的公钥
  4. TSKEnforcePinning可用于通过分别将其设置为true/来启用/禁用 SSL 固定false。(如果您希望暂时启用/禁用它)
  5. 有关更多详细信息,请查看TrustKit 文档

推荐阅读