Retrofit and OkHttp basic authentication

后端 未结 3 1383
[愿得一人]
[愿得一人] 2020-12-12 17:24

I am trying to add basic authentication (username and password) to a Retrofit OkHttp client. This is the code I have so far:

private static Retrofit createMM         


        
3条回答
  •  情话喂你
    2020-12-12 17:43

    add header interceptor

    public class HeaderInterceptor implements Interceptor {
    
        private PreferencesRepository mPrefs;
        private String mAuth;
    
        public HeaderInterceptor(PreferencesRepository p) {
            mPrefs = p;
        }
    
        @Override
        public Response intercept(Chain chain) throws IOException {
            mAuth = (mPrefs.getAuthToken() != null)?mPrefs.getAuthToken():"";
            Request r = chain.request()
                    .newBuilder()
                    .addHeader("Accept", "application/json")
                    // authorization token here
                    .addHeader("Authorization", "Bearer" + mAuth)
                    .build();
    
            return chain.proceed(r);
        }
    }
    

    add cacheinterceptor (optional)

    public class CacheInterceptor implements Interceptor {
    
        Context mContext;
    
        public CacheInterceptor(Context context) {
            this.mContext = context;
        }
    
        @Override
        public Response intercept(Chain chain) throws IOException {
    
            Request request = chain.request();
    
            if (request.method().equals("GET")) {
                if (DeviceUtils.isConnected(mContext)) {
                    request = request.newBuilder()
                            .header(Constant.CACHE_CONTROL, "only-if-cached")
                            .build();
                } else {
                    request = request.newBuilder()
                            .header(Constant.CACHE_CONTROL, "public, max-stale=2419200")
                            .build();
                }
            }
    
            Response originalResponse = chain.proceed(request);
            return originalResponse.newBuilder()
                    .header(Constant.CACHE_CONTROL, "max-age=600")
                    .build();
        }
    }
    

    implement it

    HttpLoggingInterceptor logger = new HttpLoggingInterceptor();
    logger.setLevel(HttpLoggingInterceptor.Level.BODY);
    
    long SIZE_OF_CACHE = 10 * 1024 * 1024; // 10 MiB
    Cache cache = new Cache(new File(mContext.getCacheDir(), "http"), SIZE_OF_CACHE);
    
    new OkHttpClient.Builder()
                        .addInterceptor(logger)
                        .addInterceptor(new HeaderInterceptor(u))
                        .cache(cache)
                        .addNetworkInterceptor(new CacheInterceptor(mContext))
                        .connectTimeout(Constant.CONNECTTIMEOUT, TimeUnit.SECONDS)
                        .readTimeout(Constant.READTIMEOUT, TimeUnit.SECONDS)
                        .writeTimeout(Constant.WRITETIMEOUT, TimeUnit.SECONDS)
                        .build();
    

提交回复
热议问题