코딩하는 일용직 노동자

SSL 무시하기 처리. 본문

안드로이드

SSL 무시하기 처리.

bacass 2020. 5. 3. 16:45

SSL 적용된 서버 접속하기 위한 소스.
SSL 처리를 무시하는 소스이다.

// TI2
val socketFactory = SSLContext.getInstance("SSL").let { sslContext ->
    val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
        override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate>? = emptyArray()
        override fun checkClientTrusted(certs: Array<java.security.cert.X509Certificate>, authType: String) = Unit
        override fun checkServerTrusted(certs: Array<java.security.cert.X509Certificate>, authType: String) = Unit
    })

    sslContext.init(null, trustAllCerts, java.security.SecureRandom())
    sslContext.socketFactory
}

val loggingInterceptor =
    HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { Log.d("ApiService", it) }).apply {
        level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
    }

val headerInterceptor = Interceptor { chain ->
    chain.proceed(
        chain.request().newBuilder()
            .header("Authorization", "TOKEN_STR")
            .build()
    )
}

val okHttpClient = OkHttpClient.Builder()
    .socketFactory(socketFactory)
    .hostnameVerifier { _, _ -> true }
    .addInterceptor(headerInterceptor)
    .addInterceptor(loggingInterceptor)
    .build()

Retrofit.Builder()
    .baseUrl(App.testIpAddress)
    .addConverterFactory(GsonConverterFactory.create())
    .client(okHttpClient)
    .build()
    .create(ApiService::class.java)

'안드로이드' 카테고리의 다른 글

File Exploler 라이브러리  (0) 2020.05.03
Wifi ADB 디버깅 방법  (0) 2020.05.03
SMS Retriever  (0) 2020.05.03
안드로이드 Dokka 사용방법  (0) 2020.05.03
안드로이드 StatusBar 색상 변경하기. (Kotlin)  (0) 2020.05.03