Dagger + Retrofit。在运行时添加auth头

时间:2017-03-27 16:18:42

标签: java android retrofit2 dagger-2 dagger

我想知道Dagger是否有办法知道在新数据可用时它应该重新创建一个对象。

我所说的实例是我用于改造的请求标头。在某些时候(当用户登录时),我得到一个令牌,我需要将其添加到改造的标题中以进行经过身份验证的请求。问题是,我留下了相同的未经验证的改造版本。这是我的注射码:

@Provides
    @Singleton
    OkHttpClient provideOkHttpClient(Cache cache) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .cache(cache).build();
         client
                .newBuilder()
                .addInterceptor(
                    chain -> {
                        Request original = chain.request();
                        Request.Builder requestBuilder = original.newBuilder()
                                .addHeader("Accept", "Application/JSON");
                        Request request = requestBuilder.build();
                        return chain.proceed(request);
                    }).build();
        return client;
    }

  @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) { 
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
        return retrofit;
}

@Provides
    @Singleton
    public NetworkService providesNetworkService(Retrofit retrofit) {
        return retrofit.create(NetworkService.class);
    }

关于如何使这项工作的任何想法?

5 个答案:

答案 0 :(得分:28)

我个人创建了一个okhttp3.Interceptor,它为我做了这个,一旦我有了所需的令牌,我就会更新。它看起来像:

@Singleton
public class MyServiceInterceptor implements Interceptor {
  private String sessionToken;

  @Inject public MyServiceInterceptor() {
  }

  public void setSessionToken(String sessionToken) {
    this.sessionToken = sessionToken;
  }

  @Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    Request.Builder requestBuilder = request.newBuilder();

    if (request.header(NO_AUTH_HEADER_KEY) == null) {
      // needs credentials
      if (sessionToken == null) {
        throw new RuntimeException("Session token should be defined for auth apis");
      } else {
        requestBuilder.addHeader("Cookie", sessionToken);
      }
    }

    return chain.proceed(requestBuilder.build());
  }
}

在相应的dagger组件中,我公开了这个拦截器,这样我就可以在需要时设置sessionToken

这是杰克谈论他的一些话题Making Retrofit Work For You

答案 1 :(得分:6)

请考虑使用 @oldergod 提及的the approach,因为它是"官方" 并且更好的方式,而提到的方法以下是建议,它们可能被视为解决方法。

你有几个选择。

  1. 一旦获得令牌,就必须将提供Retrofit实例的组件清零,创建一个新组件并要求新的Retrofit实例,该实例将被实例化必要的okhttp实例。
  2. 快速和糟糕的一个 - 在SharedPreferences中保存令牌,创建okHttp标头,这将从SharedPreferences应用令牌读取。如果没有 - 不发送令牌标题。
  3. 即使是更丑陋的解决方案 - 声明一个static volatile String字段,并执行与步骤2相同的操作。
  4. 为什么第二种选择不好?因为在每个请求中,您将轮询SD卡并从那里获取数据。

答案 2 :(得分:1)

基于@oldergod解决方案kotlin版本,具有不同的类和结构

使Retrofit实例像这样

object RetrofitClientInstance {
   private var retrofit: Retrofit? = null
   private val BASE_URL = "http://yoururl"


    val retrofitInstance: Retrofit?
        get() {
            if (retrofit == null) {
                var client = OkHttpClient.Builder()
                      .addInterceptor(ServiceInterceptor())
                      //.readTimeout(45,TimeUnit.SECONDS)
                      //.writeTimeout(45,TimeUnit.SECONDS)
                        .build()

                retrofit = Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .client(client)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build()

            }
            return retrofit
      }

}

添加如下的ServiceInterceptor

class ServiceInterceptor : Interceptor{

  var token : String = "";

  fun Token(token: String ) {
     this.token = token;
  }

  override fun intercept(chain: Interceptor.Chain): Response {
    var request = chain.request()

    if(request.header("No-Authentication")==null){
        //val token = getTokenFromSharedPreference();
        //or use Token Function
        if(!token.isNullOrEmpty())
        {
            val finalToken =  "Bearer "+token
            request = request.newBuilder()
                    .addHeader("Authorization",finalToken)
                    .build()
        }

    }

    return chain.proceed(request)
  }

}

登录界面和数据类的实现

interface Login {
  @POST("Login")
  @Headers("No-Authentication: true")
  fun login(@Body value: LoginModel): Call<LoginResponseModel>



  @POST("refreshToken")
  fun refreshToken(refreshToken: String): 
      Call<APIResponse<LoginResponseModel>>
}

data class LoginModel(val Email:String,val Password:String)
data class LoginResponseModel (val token:String,val 
         refreshToken:String)

在任何这样的活动中称呼它

val service = RetrofitClientInstance.retrofitInstance?.create(Login::class.java)
val refreshToken = "yourRefreshToken"
val call = service?.refreshToken(refreshToken)
        call?.enqueue(object: Callback<LoginResponseModel>{
            override fun onFailure(call: Call<LoginResponseModel>, t: Throwable) {
                print("throw Message"+t.message)
                Toast.makeText(applicationContext,"Error reading JSON",Toast.LENGTH_LONG).show()
            }

            override fun onResponse(call: Call<LoginResponseModel>, response: Response<LoginResponseModel>) {
                val body = response?.body()
                if(body!=null){
                    //do your work
                }
            }

        })

答案 3 :(得分:0)

使用@Inject构造函数创建自定义RequestInterceptor

RequestInterceptor

<button type="submit">Go</button>

ApplicationModule

@Singleton
class
RequestInterceptor @Inject constructor(
    private val preferencesHelper: PreferencesHelper,
) : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        var newRequest: Request = chain.request()

        newRequest = newRequest.newBuilder()
            .addHeader(
                "AccessToken",
                preferencesHelper.getAccessTokenFromPreference()
            )
            .build()


        Log.d(
            "OkHttp", String.format(
                "--> Sending request %s on %s%n%s",
                newRequest.url(),
                chain.connection(),
                newRequest.headers()
            )
        );
        return chain.proceed(newRequest)

  }

PreferencesHelper

@Module(includes = [AppUtilityModule::class])
class ApplicationModule(private val application: AppController) {

    @Provides
    @Singleton
    fun provideApplicationContext(): Context = application

    @Singleton
    @Provides
    fun provideSharedPreferences(): SharedPreferences =
        PreferenceManager.getDefaultSharedPreferences(application.applicationContext)

}

答案 4 :(得分:0)

经过良好测试,可以正常工作

public OkHttpClient getHttpClient(Context context) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    return  new OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .callTimeout(60,TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(logging)
            .addInterceptor(chain -> {
                Request newRequest = chain.request().newBuilder()
                        .addHeader("Authorization", "Bearer " + Utility.getSharedPreferencesString(context, API.AUTHORIZATION))
                        .build();
                return chain.proceed(newRequest);
            })

            .build();

}

我之前很想知道,如果会话到期并且用户再次登录,此拦截器将替换现有的auth,但是幸运的是,它工作正常。