使用翻新2和日志记录拦截器的身份验证在1小时后令牌过期

时间:2018-12-13 06:15:51

标签: android

就我而言,我使用了Retrofit 2库进行联网。  1.令牌是在登录时生成的,令牌在1小时后过期,并通过标头传递。  2.我已经实现了Interceptor来添加标题。  3.每当401授权在到期后到来时,我都会调用提供刷新令牌的方法callRefreshToken()。但是我需要在后台执行此操作。 希望获得帮助:这是我的下面的代码,

 public static RetroInterfaceAPI getClient() {

    if (retroInterfaceAPI == null) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("Retrofit", message);
            }
        });

        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); // sap request ko kun thau log garne. Body garo bhane sap aauxa

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        OkHttpClient okClient = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(2, TimeUnit.MINUTES)
                .addInterceptor(new HeaderAuthoriztionInterceptor())
                .addInterceptor(loggingInterceptor)
                .build();


        Retrofit client = new Retrofit.Builder()
                .client(okClient)
                .baseUrl(Constant.URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        retroInterfaceAPI = client.create(RetroInterfaceAPI.class);
    }

    return retroInterfaceAPI;
}



  private static class HeaderAuthoriztionInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        request = request.newBuilder().header("Authorization", "Bearer " + new PrefsManager(MasterApplication.getmInstance()).getAccessToken())
                .build();
        return chain.proceed(request);
    }
}



public void callReFreshToken() {
    mUserListAdapter.clears();


    mProgressDialog.setVisibility(View.VISIBLE);


    Map<String,String> hashMap=new HashMap<>();
    hashMap.put("refresh_token",new PrefsManager(getContext()).getRefreshTokenFromLogin());
    hashMap.put("grant_type","refresh_token");

    RestClient.RetroInterfaceAPI mInterface = RestClient.getClient();
    Call<Login> call = mInterface.getRefreshToken(hashMap);
    call.enqueue(new Callback<Login>() {
        @Override
        public void onResponse(Call<Login> call, Response<Login> response) {


            if (response != null) {
                mProgressDialog.setVisibility(View.GONE);

                new PrefsManager(getContext()).setAccessToken(response.body().getAccesstoken());



            }else {
                mProgressDialog.setVisibility(View.GONE);

            }


        }

        @Override
        public void onFailure(Call<Login> call, Throwable t) {
            mProgressDialog.setVisibility(View.GONE);

        }

    });

}

1 个答案:

答案 0 :(得分:1)

在过期时自动使用身份验证器刷新令牌

您的客户应该是

    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(15, TimeUnit.SECONDS)
            .readTimeout(2, TimeUnit.MINUTES)
            .addInterceptor(new HeaderAuthoriztionInterceptor())
            .addInterceptor(loggingInterceptor)
            .authenticator(new TokenAuthenticator()) 
            .builder.build()

身份验证器

class TokenAuthenticator implements Authenticator {

    @Throws(IOException::class)
    override fun authenticate(route: Route, response: Response): Request? {

        // Refresh your token here


        //returned new request with updated header
        return response.request().newBuilder()
                .header(header_key, new_token)
                .build()
    }
}

authenticate()将在您的api调用获得401时触发。