Hilt Singleton 如何在运行时更新 Retrofit certificatePinner 值

问题描述 投票:0回答:0
@Singleton
@Provides
fun providesHttpClient(
    logging: HttpLoggingInterceptor,
    @ApplicationContext context: Context,
    authenticator: TokenAuthenticator
): OkHttpClient {
    return OkHttpClient.Builder()
        .addInterceptor(logging)
        .retryOnConnectionFailure(false)
        .readTimeout(30, TimeUnit.SECONDS)
        .connectTimeout(30, TimeUnit.SECONDS)
        .certificatePinner(CertificatePinner.Builder().add("com.example.domain",CONSTANT.KEY).build())
        .authenticator(authenticator)
        .build()
}

@单例 @提供 有趣的 providesConvertorFactory(): GsonConverterFactory { 返回 GsonConverterFactory.create() }

@Singleton
@Provides
fun provideRetrofit(
    okHttpClient: OkHttpClient,
    gsonConverterFactory: GsonConverterFactory
): Retrofit {
    return Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(gsonConverterFactory)
        .build()
}

密钥将从服务器获取,最初是空的。

如何更新 CertificatePinner 值

singleton runtime retrofit2 dagger-hilt public-key-pinning
© www.soinside.com 2019 - 2024. All rights reserved.