【问题标题】:configuration to Ignore self-signed certificate in ktor httpClient(js) JS engine在 ktor httpClient(js) JS 引擎中忽略自签名证书的配置
【发布时间】:2021-06-01 04:46:15
【问题描述】:

Kotlin 多平台库,我的设置如下所示。 显然,我的 JS 测试因错误“错误:无法获取”而失败,因为 Ktor httpClient(JS) 未配置为忽略/信任自签名证书。并根据documentation“Js 引擎没有自定义配置”。 所以我的问题是有没有办法让我的测试通过 JS 平台。 或者您知道任何解决方法吗?还是我在这里遗漏了什么?

expect object Provider {
    fun createClient(): HttpClient
}

//JVM
actual object Provider {
    actual fun createClient(): HttpClient {
        return HttpClient(Apache) {
            install(JsonFeature) {
                serializer = KotlinxSerializer()
            }
            engine {
                customizeClient {
                    setSSLContext(
                        SSLContextBuilder
                            .create()
                            .loadTrustMaterial(TrustSelfSignedStrategy())
                            .build()
                    )
                    setSSLHostnameVerifier(NoopHostnameVerifier())
                }
            }
        }
    }
}

//JS
actual object Provider {
    actual fun createClient(): HttpClient {
        return HttpClient(Js) {
            install(JsonFeature) {
                serializer = KotlinxSerializer()
            }
        }
    }
}

【问题讨论】:

    标签: javascript kotlin ktor kmm


    【解决方案1】:

    作为一种解决方法,在 Node.js 目标上,您可以设置环境变量 NODE_TLS_REJECT_UNAUTHORIZED 以禁用所有证书验证:

    js("process.env.NODE_TLS_REJECT_UNAUTHORIZED = \"0\";")
    val client = HttpClient(Js)
    client.get<String>("https://localhost:8443/")
    

    【讨论】:

      猜你喜欢
      • 2017-02-16
      • 1970-01-01
      • 2016-05-11
      • 2018-07-26
      • 2012-08-30
      • 1970-01-01
      • 2016-07-27
      • 2020-04-01
      • 2011-01-16
      相关资源
      最近更新 更多