在 ktor httpClient(js) JS 引擎中配置忽略自签名证书

configuration to Ignore self-signed certificate in ktor httpClient(js) JS engine

Kotlin 多平台库,我的设置如下所示。 显然,我的 JS 测试失败并显示错误“错误:获取失败”,因为 Ktor httpClient(JS) 未配置为 ignore/Trust 自签名证书。并根据 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()
            }
        }
    }
}

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

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