gpt4 book ai didi

android - 限制 checkServerTrusted 仅用于初始化与我的服务器的连接

转载 作者:行者123 更新时间:2023-12-05 00:02:24 27 4
gpt4 key购买 nike

CheckServerTrusted 在使用我的服务器进行初始化时被调用。
但是,在初始化 Google map 时也会调用 checkServerTrusted。
是否可以以某种方式限制 checkServerTrusted 仅用于初始化与我的服务器的连接?
所以我想 checkServerTrusted 只是在初始化与我的服务器的连接时被调用。
//自定义OkHttpClient

  public static OkHttpClient.Builder getCustomOkHttpClient() {
try {

TrustManagerFactory tmf = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
tmf.init((KeyStore)null);

TrustManager[] trustManagers = tmf.getTrustManagers();
final X509TrustManager origTrustmanager = (X509TrustManager)trustManagers[0];

TrustManager[] wrappedTrustManagers = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return origTrustmanager.getAcceptedIssuers();
}

public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
origTrustmanager.checkClientTrusted(certs, authType);
}

public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
origTrustmanager.checkServerTrusted(certs, authType);

}
};
//ApiClient
 val builder = CustomOkHttpClient.getCustomOkHttpClient();
client = builder.build()

val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()

return retrofit.create(ApiInterface::class.java)

最佳答案

您可以使用无效证书将特定服务器列入白名单

  val clientCertificates = HandshakeCertificates.Builder()
.addPlatformTrustedCertificates()
.addInsecureHost(server.hostName)
.build()

val client = OkHttpClient.Builder()
.sslSocketFactory(clientCertificates.sslSocketFactory(), clientCertificates.trustManager)
.build()
https://github.com/square/okhttp/blob/f8fd4d08decf697013008b05ad7d2be10a648358/okhttp-tls/src/main/kotlin/okhttp3/tls/internal/InsecureAndroidTrustManager.kt中的代码向您展示如何在 Android 上实现
对于 JDK https://github.com/square/okhttp/blob/f8fd4d08decf697013008b05ad7d2be10a648358/okhttp-tls/src/main/kotlin/okhttp3/tls/internal/InsecureExtendedTrustManager.kt

关于android - 限制 checkServerTrusted 仅用于初始化与我的服务器的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70020603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com