【问题标题】:React-Native axios httpsReact-Native axios https
【发布时间】:2020-05-02 21:01:00
【问题描述】:

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

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

【问题讨论】:

  • 您的问题和标题能否更具体一些?

标签: react-native axios


【解决方案1】:

你可以试试下面的:

NPM : react-native-ssl-pinning

更多信息:npmjs.com/package/react-native-ssl-pinning

【讨论】:

  • 这对我没用。
  • Awwwww,我虽然你需要这个,或者你能解释一下!
【解决方案2】:

我能够使用 TrustKit 在 iOS 部分为我的 React Native 应用程序启用 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 需要设置为 true。
  2. yourDomain.com 是您的 API 的基本 URL。
  3. public Key 1 和 public Key 2 是您的 API 的公钥。您可以获得任何公共域here 的公钥
  4. TSKEnforcePinning 可用于启用/禁用 SSL 固定,方法是将其分别设置为 true / false。 (如果您希望暂时启用/禁用它)
  5. 更多详情请关注TrustKit Documentation

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 2019-08-09
    • 1970-01-01
    • 2021-06-11
    • 2019-07-24
    • 2018-12-06
    • 2018-06-06
    • 2023-04-10
    • 2020-03-13
    相关资源
    最近更新 更多