如何使用改造刷新 lambda API 生成的令牌?
How to refresh the token generated by lambda API using retrofit?
目前我的登录 API 是使用 lambda 完成的,响应还包含一个令牌及其到期时间。对于 lambda 生成的令牌,有没有类似 O'Auth 令牌刷新的方法?
提前致谢。任何帮助表示赞赏。
Retrofit
中没有某些API来实现这个行为,因为Retrofit使用OkHttp
来处理网络操作。但是您可以通过实现传递给 Retrofit 构建器的 OkHttp
客户端的 Authenticator
接口来实现这一点。 OkHttp
在响应代码为 401
未授权错误时为 credentials
调用 Authenticator
,然后重试调用失败的请求。
您可以像这样实现 Authenticator
:
public class TokenAuthenticator implements Authenticator {
ApiService apiService;
TokenAuthenticator(ApiService apiService){
this.apiService = apiService;
}
@Override
public Request authenticate(Route route, Response response) throws IOException {
// Refresh your token using a synchronous request
String newToken = apiService.refreshToken().execute().body();
// Add new token to the failed request header and retry it
return response.request().newBuilder()
.header("Authorization", newToken)
.build();
}
}
然后像这样将 TokenAuthenticator
传递给 OkHttpClient
:
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.authenticator(new TokenAuthenticator(apiService))
.build();
最后,将 OkHttpClient 传递给 Retrofit 构建器:
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(BuildConfig.API_BASE_URL)
.build();
您可以创建一个拦截器,当任何请求 returns 401 刷新令牌时 returns 登录 activity
public class ErrorInterceptor implements Interceptor {
Context context;
public ErrorInterceptor(Context context) {
this.context = context;
}
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
// before request
Request request = chain.request();
// execute request
Response response = chain.proceed(request);
// after request
// inspect status codes of unsuccessful responses
switch (response.code()) {
case 401:
Intent intent = new Intent(context, Login.class);
context.startActivity(intent);
// do something else
Log.e("TEST", "Unauthorized error for: " + request.url());
// perhaps throw a custom exception ?
throw new IOException("Unauthorized !!");
}
return response;
}
}
然后您可以将 ErrorInterceptor 添加到您的 OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor( new ErrorInterceptor(context))
.build();
目前我的登录 API 是使用 lambda 完成的,响应还包含一个令牌及其到期时间。对于 lambda 生成的令牌,有没有类似 O'Auth 令牌刷新的方法? 提前致谢。任何帮助表示赞赏。
Retrofit
中没有某些API来实现这个行为,因为Retrofit使用OkHttp
来处理网络操作。但是您可以通过实现传递给 Retrofit 构建器的 OkHttp
客户端的 Authenticator
接口来实现这一点。 OkHttp
在响应代码为 401
未授权错误时为 credentials
调用 Authenticator
,然后重试调用失败的请求。
您可以像这样实现 Authenticator
:
public class TokenAuthenticator implements Authenticator {
ApiService apiService;
TokenAuthenticator(ApiService apiService){
this.apiService = apiService;
}
@Override
public Request authenticate(Route route, Response response) throws IOException {
// Refresh your token using a synchronous request
String newToken = apiService.refreshToken().execute().body();
// Add new token to the failed request header and retry it
return response.request().newBuilder()
.header("Authorization", newToken)
.build();
}
}
然后像这样将 TokenAuthenticator
传递给 OkHttpClient
:
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.authenticator(new TokenAuthenticator(apiService))
.build();
最后,将 OkHttpClient 传递给 Retrofit 构建器:
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(BuildConfig.API_BASE_URL)
.build();
您可以创建一个拦截器,当任何请求 returns 401 刷新令牌时 returns 登录 activity
public class ErrorInterceptor implements Interceptor {
Context context;
public ErrorInterceptor(Context context) {
this.context = context;
}
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
// before request
Request request = chain.request();
// execute request
Response response = chain.proceed(request);
// after request
// inspect status codes of unsuccessful responses
switch (response.code()) {
case 401:
Intent intent = new Intent(context, Login.class);
context.startActivity(intent);
// do something else
Log.e("TEST", "Unauthorized error for: " + request.url());
// perhaps throw a custom exception ?
throw new IOException("Unauthorized !!");
}
return response;
}
}
然后您可以将 ErrorInterceptor 添加到您的 OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor( new ErrorInterceptor(context))
.build();