增加Token拦截适配器

This commit is contained in:
董宏宇
2021-01-28 17:43:20 +08:00
parent 352220d78b
commit de1767f8fd
5 changed files with 44 additions and 5 deletions

View File

@@ -40,7 +40,7 @@ public class MoGoApplication extends MultiDexApplication {
// 设置从蘑菇AI开放平台获取的APPKey
clientConfig.setThirdPartyAppKey("bydauto");
// 设置车机设备的唯一标识(这些表识必须是通过后台录入的设备)
clientConfig.setThirdPartyDeviceId("bydauto");
clientConfig.setThirdPartyDeviceId("F803EB2046PZD00149");
// 设置循环检测间隔时间
clientConfig.setLoopCheckDelay(15 * 1000);

View File

@@ -8,6 +8,7 @@ import com.mogo.cloud.network.SSLSocketFactoryUtils.createTrustAllManager
import com.mogo.cloud.network.interceptor.HttpHeaderInterceptor
import com.mogo.cloud.network.interceptor.HttpDnsInterceptor
import com.mogo.cloud.network.interceptor.HttpLoggingInterceptor
import com.mogo.cloud.network.interceptor.HttpPassportInterceptor
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit
@@ -25,6 +26,7 @@ class OkHttpFactory private constructor() {
.addNetworkInterceptor(HttpHeaderInterceptor())
.addNetworkInterceptor(HttpLoggingInterceptor())
.addInterceptor(HttpDnsInterceptor())
.addInterceptor(HttpPassportInterceptor())
.sslSocketFactory(createSSLSocketFactory(), createTrustAllManager())
.hostnameVerifier(SSLSocketFactoryUtils.TrustAllHostnameVerifier())
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)

View File

@@ -10,10 +10,10 @@ import javax.net.ssl.*
* describe:
*/
object SSLSocketFactoryUtils {
/*
* 默认信任所有的证书
* 最好加上证书认证主流App都有自己的证书
* */
/**
* 默认信任所有的证书
* 最好加上证书认证主流App都有自己的证书
* */
private const val str = "TLS"
fun createSSLSocketFactory(): SSLSocketFactory? {
var sslSocketFactory: SSLSocketFactory? = null

View File

@@ -0,0 +1,35 @@
package com.mogo.cloud.network.interceptor
import com.mogo.utils.logger.Logger
import okhttp3.Interceptor
import okhttp3.MediaType
import okhttp3.Response
import okhttp3.ResponseBody
/**
* created by donghongyu on 2021/1/28 14:33
* describe: 令牌拦截,如果发现令牌过期需要重新获取令牌
*/
class HttpPassportInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
Logger.d("HttpPassportInterceptor", "Passport error ")
val response = chain.proceed(chain.request())
val responseBody = response.body()
var responseContent: String? = null
var bodySize: String? = null
var contentType: MediaType? = null
var consumedResponse = false
if (responseBody != null) {
val contentLength = responseBody.contentLength()
bodySize = if (contentLength != -1L) "$contentLength-byte" else "unknown-length"
contentType = responseBody.contentType()
responseContent = responseBody.string()
Logger.d("响应结果", "responseContent$responseContent")
consumedResponse = true
}
return if (consumedResponse) response.newBuilder().body(ResponseBody.create(contentType, responseContent)).build() else response
}
}

View File

@@ -2,6 +2,7 @@ package com.mogo.cloud.passport;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Keep;
@@ -115,6 +116,7 @@ public class MoGoAiCloudClient {
@Override
public void onFailure(int code, String msg) {
Toast.makeText(mContext, "MoGo鉴权失败", Toast.LENGTH_SHORT).show();
Log.e(TAG, "═════════════════════════════════════");
Log.e(TAG, "║ MoGo鉴权失败 ");
Log.e(TAG, "║ ErrorCode" + code);