【发布时间】:2018-12-01 12:30:06
【问题描述】:
在 AWS 作为联合身份生成的令牌过期后,我一直在寻找刷新令牌的正确方法。我的应用程序使用 cognito 登录和注册用户,然后获取 Access Token,然后使用 RetroFit 访问 api。
改造调用
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
//end
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.readTimeout(45, TimeUnit.SECONDS);
httpClient.connectTimeout(120, TimeUnit.SECONDS);
httpClient.interceptors().add(new AuthInterceptor());
httpClient.addInterceptor(interceptor); //debugging
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
// Customize the request
Request request = original.newBuilder()
.header("Content-type", "application/json")
.header("Authorization", "auth-token")
.header("Accept", "application/json")
.header("deviceType", "android") //experimental
.header("Version", "v1")
.method(original.method(), original.body())
.build();
Response response = chain.proceed(request);
return response;
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(httpClient.build())
.addConverterFactory(ToStringConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(RetroInterface.class);
AuthInterceptor
...
if (jsonObject.getInt("status") == 0 && jsonObject.getJSONObject("data").getInt
("code")
== 99) {
IS_TOKEN_EXIPRED = true;
// HashMap<String, String> logins = new HashMap<String, String>();
logins.put("cognito-idp.us-east-1.amazonaws.com/" + BuildConfig.USERPOOLID,
Cognito.getInstance().getCurrSession().getIdToken().getJWTToken());
CognitoLogin.credentialsProvider.setLogins(logins);
String newtoken = Cognito.getInstance().getCurrSession().getAccessToken().getJWTToken();
Log.e("refy", newtoken);
BaseApplication.getApp().doSaveSharedString(AppConstants.USER_ACCESS_TOKEN, newtoken);
BaseApplication.getApp().doWriteLog("AuthInterceptor Here: ");
}
} catch (JSONException e) {
BaseApplication.getApp().redirectToLoginActivity();
e.printStackTrace();
}
...
我被这个问题卡住了。,令牌在 1 小时后过期,然后我什么也做不了。 Android 中没有可用于刷新令牌的信息。我所能看到的是,只要刷新令牌为有效性,Android AWS SDK 就会自行刷新令牌。
【问题讨论】:
标签: android amazon-web-services amazon-cognito