Android 使用改装调用添加自签名证书
我想使用改造添加自签名证书,因为我需要传递上下文,但在通过MVC模型传递上下文时遇到一些问题 改造客户端Android 使用改装调用添加自签名证书,android,ssl,kotlin,retrofit2,Android,Ssl,Kotlin,Retrofit2,我想使用改造添加自签名证书,因为我需要传递上下文,但在通过MVC模型传递上下文时遇到一些问题 改造客户端 object RetrofitClient { fun call(context: Context): Webservice { val webservice by lazy { Retrofit.Builder() // .baseUrl("http://68.183.183.255:8080") //.b
object RetrofitClient {
fun call(context: Context): Webservice {
val webservice by lazy {
Retrofit.Builder()
// .baseUrl("http://68.183.183.255:8080")
//.baseUrl("http://my-json-server.typicode.com")
.baseUrl("https://54roh005p5.execute-api.us-west-2.amazonaws.com/mdv/cloud/login/")
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.addConverterFactory(ScalarsConverterFactory.create())
.client(SelfSigningClientBuilder.createClient(context))
.build().create(Webservice::class.java)
}
return webservice
}
}
存储库
class TodoRepository(context:Context) {
private var client:Webservice = RetrofitClient.call(context)
//suspend fun getTodo(id: String) = client.getTodo(id)
suspend fun getTodo(id: User) = client.getTodo(id)
}
视图模型
class NetworkViewModel(context: Context) : ViewModel() {
var repository: TodoRepository = TodoRepository(context)
fun getTodo(arg : User):LiveData<Response<com.example.myapplication.entity.UserData>> {
val result = liveData(Dispatchers.IO) {
val retrivedTodo = repository.getTodo(arg)
emit(retrivedTodo)
}
return result
}
}
我做错了什么。如果有更好的方法,请建议它不要通过构造函数初始化ViewModel对象:-
NetworkViewModel(this)::class.java
是错误的。使用:-
networkViewModel = ViewModelProviders.of(this).get(NetworkViewModel::class.java)
您可以自定义OKHttpClient
sslSocketFactory
e、 g(改装
的客户端设置)
e、 g(OKHttpSCClient
的sslSocketFactory设置)
e、 g(SSLSocketFactory
带有自定义证书)
使用自定义证书获取密钥库
fun readKeyStore()= KeyStore.getInstance(KeyStore.getDefaultType()).also{
try (val inputStream = context.getResources().openRawResource(R.raw.custom_certificate)) {
it.load(inputStream, getPassword());
}
}
创建一个应用程序类并在需要时使用该应用程序上下文,而不是在类之间传递上下文。请检查以下内容:
class MainApplication : Application() {
companion object {
private lateinit var instance: MainApplication
fun applicationContext() : Context {
return instance.applicationContext
}
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
然后,在AndroidManifest
的应用程序标签中添加此android:name=“.MainApplication”
然后调用MainApplication.applicationContext()从任何地方获取上下文。为什么要将上下文传递给每个类?只需创建一个应用程序类并从中获取上下文,我尝试过它会给我错误MyApp。getContext()不能为Null添加MyApp代码。我将更新类MainApplication:Application(){init{instance=this}伴随对象{private var实例:MainApplication?=null fun applicationContext():Context{return instance!!.applicationContext}}覆盖fun onCreate(){super.onCreate()val context:context=MainApplication.applicationContext()}类MainApplication:Application(){companion object{private lateinit var实例:MainApplication fun applicationContext():context{return instance.applicationContext}}覆盖fun onCreate(){super.onCreate()实例=this}
val client =OkHttpClient.Builder()
.sslSocketFactory(sslContextWithSS.getSocketFactory())
.build()
val keyStore = readKeyStore() //your method to obtain KeyStore
val sslContext = SSLContext.getInstance("SSL")
val trustManagerFactory = TrustManagerFactory.getInstance(getDefaultAlgorithm())
trustManagerFactory.init(keyStore)
val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
keyManagerFactory.init(keyStore, "keystore_pass".toCharArray())
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), SecureRandom())
client.setSslSocketFactory(sslContext.getSocketFactory())
fun readKeyStore()= KeyStore.getInstance(KeyStore.getDefaultType()).also{
try (val inputStream = context.getResources().openRawResource(R.raw.custom_certificate)) {
it.load(inputStream, getPassword());
}
}
class MainApplication : Application() {
companion object {
private lateinit var instance: MainApplication
fun applicationContext() : Context {
return instance.applicationContext
}
}
override fun onCreate() {
super.onCreate()
instance = this
}
}