Commit fd9ae6fb by 段启岩

添加请求拦截器

parent fb6a1389
package cn.yunliyunwai.beyondclouds.data;
import android.util.Log;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
import cn.yunliyunwai.beyondclouds.viewmodel.AuthenticationViewModel;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
@Singleton
public class TokenInterceptor implements Interceptor {
private AuthenticationViewModel authenticationViewModel;
@Inject
public TokenInterceptor(AuthenticationViewModel authenticationViewModel) {
this.authenticationViewModel = authenticationViewModel;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder requestBuilder = chain.request().newBuilder();
if (authenticationViewModel.getAuthenticationState().getValue() == AuthenticationViewModel.AuthenticationState.AUTHENTICATED) {
requestBuilder
.addHeader("Authorization","Bearer " + authenticationViewModel.getToken());
Log.d(getClass().getName(), "检测到用户已认证,本次请求将携带token:" + authenticationViewModel.getToken());
} else {
Log.d(getClass().getName(), "检测到用未认证,本次请求不携带token!");
}
return chain.proceed(requestBuilder.build());
}
}
......@@ -18,6 +18,7 @@ import java.util.concurrent.TimeUnit;
import javax.inject.Singleton;
import cn.yunliyunwai.beyondclouds.adapter.common.LiveDataCallAdapterFactory;
import cn.yunliyunwai.beyondclouds.data.TokenInterceptor;
import cn.yunliyunwai.beyondclouds.data.source.local.database.BlogDatabase;
import cn.yunliyunwai.beyondclouds.data.source.local.database.ProjectDatabase;
import cn.yunliyunwai.beyondclouds.data.source.local.database.QuestionDatabase;
......@@ -89,9 +90,12 @@ public class AppModule {
@Singleton
@Provides
public OkHttpClient provideOkHttpClient() {
public OkHttpClient provideOkHttpClient(TokenInterceptor tokenInterceptor) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS);
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.addInterceptor(tokenInterceptor);
return clientBuilder.build();
}
......
package cn.yunliyunwai.beyondclouds.viewmodel;
import android.content.Context;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class AuthenticationViewModel {
private static final String SHARED_PREFERENCES_NAME_AUTHENTICATION = "SHARED_PREFERENCES_AUTHENTICATION";
private static final String SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN = "SHARED_PREFERENCES_AUTHENTICATION_TOKEN";
private String token;
private MutableLiveData<AuthenticationState> authenticationState = new MutableLiveData<>();
public static enum AuthenticationState {
/**
* 未认证
*/
UN_AUTHENTICATION,
/**
* 已认证
*/
AUTHENTICATED
}
@Inject
public AuthenticationViewModel(Context context) {
String token = context.getSharedPreferences(SHARED_PREFERENCES_NAME_AUTHENTICATION, Context.MODE_PRIVATE).getString(SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN, null);
if (null != token) {
this.token = token;
this.authenticationState.setValue(AuthenticationState.AUTHENTICATED);
} else {
this.authenticationState.setValue(AuthenticationState.UN_AUTHENTICATION);
}
}
public LiveData<AuthenticationState> getAuthenticationState() {
return authenticationState;
}
public String getToken() {
return token;
}
/**
* 用户登录
* @param account
* @param password
*/
public void login(String account, String password) {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment