React-Native axios https [英] React-Native axios https

查看:141
本文介绍了React-Native axios https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

axios({方法:'POST',网址:网址,标题:标题,数据:参数,超时:超时,//httpsAgent: new https.Agent({ rejectUnauthorized: false }),})

解决方案

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

  1. 在您的 podfile 中添加并安装 TrustKit.(pod 'TrustKit')
  2. 打开您的 Info.plist 作为源代码,向其中添加以下代码.

<块引用>

TSKConfiguration<字典><key>TSKSwizzleNetworkDelegates</key><真/><key>TSKPinnedDomains</key><字典><key>yourDomain.com</key><字典><key>TSKPublicKeyHashes</key><阵列><string>公钥1</string><string>公钥2</string></阵列><key>TSKPublicKeyAlgorithms</key><阵列><string>TSKAlgorithmRsa2048</string></阵列><key>TSKIncludeSubdomains</key><真/><key>TSKEnforcePinning</key><真/></dict></dict></dict>

需要注意的重要事项:

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

How does react-native use self-signed certificates for HTTPS requests?

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

解决方案

I was able to enable SSL pinning on the iOS part for my React Native Application using TrustKit. I am also using Axios to make server interactions. There are two ways to implement TrustKit, by code or by using Info.plist. I have done using the Info.plist and you can find the implementation for the same below:

  1. Add and install TrustKit in your podfile. (pod 'TrustKit')
  2. Open your Info.plist as Source Code add the below code to it.

<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>

Important things to note:

  1. TSKSwizzleNetworkDelegates needs to be set to true.
  2. yourDomain.com is the base URL for your API.
  3. public Key 1 and public Key 2 are the public keys for your API. You can get the pubic keys for any public domain here
  4. TSKEnforcePinning can be used to enable / disable SSL pinning by setting it to true / false respectively. (Incase you wish to enable/disable it temporarily)
  5. For more details, please check out TrustKit Documentation

这篇关于React-Native axios https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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