1

I am creating android application which uses retrofit. I have used spring as rest api. I have used authentication with JWT. I have use two interceptor here RequestInterceptor and ResponseInterceptor. The scenario of calling BASE_URL/hello api with expire JWT is as below

  1. client call /hello with expired accesstoken in header using RequestInterceptor

  2. server check token and response with code 401/403

  3. client check response code and call /refresh using ResponseInterceptor with refreshtoken in header

  4. Server check refreshtoken and response with new accesstoken

now the problem is how to call again /hello. I want this for each request. How can i predict which request has been made last.

Here is the code:

part of the code where /hello is called

btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Call<HelloResponse> call= RetrofitFactoryWithJwt.getRetrofitInstance(getApplicationContext()).helloUser();

                call.enqueue(new Callback<HelloResponse>() {
                    @Override
                    public void onResponse(Call<HelloResponse> call, Response<HelloResponse> response) {
                        Log.d(TAG,"after call in enque");
                        if(response.code()==200)
                        {
                            Log.d(TAG,response.body().getSuccess());
                        }
                        else
                        {
                            Log.d(TAG,"problem in response:"+response.code());

                        }
                    }

                    @Override
                    public void onFailure(Call<HelloResponse> call, Throwable t) {
                        Log.d(TAG,"onfailure"+t.getMessage());
                    }
                });

                Intent intent = new Intent( getApplicationContext(), MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }
        });

RequestInterceptor.java

public class RequestInterceptor implements Interceptor {


   Context context;
  String TAG="heyrequest";

    public RequestInterceptor(Context context)
    {
        this.context=context;
    }
    @Override
    public Response intercept(Chain chain) throws IOException {

        Request originalRequest = chain.request();

        //if url is /refresh add refresh token in header instead of accesstoken
        if(originalRequest.url().encodedPath().equalsIgnoreCase("/refresh"))
        {

            SharedPreferences preferences = context.getSharedPreferences("tokens", MODE_PRIVATE);
            String refreshvalue=preferences.getString("refreshtoken","");


            // rewrite the request
            Request newRequest=originalRequest.newBuilder()
                    .addHeader("Authorization","Bearer "+refreshvalue)
                    .build();

            return chain.proceed(newRequest);
        }

      //for context we have use requestinterceptor context construction

        SharedPreferences preferences = context.getSharedPreferences("tokens", MODE_PRIVATE);
        String tokenvalue=preferences.getString("accesstoken","");

        // rewrite the request
        Request newRequest=originalRequest.newBuilder()
                            .addHeader("Authorization","Bearer "+tokenvalue)
                            .build();

        return chain.proceed(newRequest);
    }
}

ResponseInterceptor.java

public class ResponseInterceptor implements Interceptor {

    Context context;
    String TAG="heyresponse";
    String accesstoken=null;
    Response response=null;
    public ResponseInterceptor(Context context)
    {
       this.context=context;
    }

    @Override
    public Response intercept(final Chain chain) throws IOException {

         final Request request=chain.request();
         response = chain.proceed(request);
        if(response.code()==401 || response.code()==403)
        {
           accesstoken=getNewToken();
        }
        return chain.proceed(request);
    }

    public String getNewToken()
    {

        Call<RefreshResponse> call= RetrofitFactoryWithJwt.getRetrofitInstance(context).refreshToken();

        call.enqueue(new Callback<RefreshResponse>() {
            @Override
            public void onResponse(Call<RefreshResponse> call, retrofit2.Response<RefreshResponse> response1) {
                Log.d(TAG,"in refreshtoken call");
                if(response1.code()==200)
                {

                    accesstoken=response1.body().getAccesstoken();
                    Log.d(TAG,accesstoken);

                    SharedPreferences preferences = context.getSharedPreferences("tokens", MODE_PRIVATE);
                    preferences.edit().putString("accesstoken", accesstoken).apply();

                }
                else
                {
                    Log.d(TAG,"problem in response:"+response1.code());
                }
            }

            @Override
            public void onFailure(Call<RefreshResponse> call, Throwable t) {
                Log.d(TAG,"onfailure:"+t.getMessage());
            }
        });

return  accesstoken;
    }

}

Bhoomi Vaghasiya
  • 311
  • 1
  • 5
  • 12

1 Answers1

1

I solved this problem by using authenticator for handle the response and interceptor for adding header in request

Bhoomi Vaghasiya
  • 311
  • 1
  • 5
  • 12