android - 当我在改造中重定向时如何包含标题

标签 android redirect retrofit2 oauth-2.0 okhttp

我尝试为 Android 应用程序制作 oauth2。它几乎没有错误。

我的错误是当我重定向时它没有像 Authorization 这样的 header

我的 Cookie 代码。它在我登录时发送授权。但是当我重定向时它不起作用

public static Retrofit getLoginRetrofitOnAuthz() {
    Retrofit.Builder builder = new Retrofit.Builder().baseUrl(ServerValue.AuthServerUrl).addConverterFactory(GsonConverterFactory.create());
    if (LoginRetrofitAuthz == null) {
        httpClientAuthz.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {

                String str = etUsername.getText().toString() + ":" + etPassword.getText().toString();
                String Base64Str = "Basic " + Base64.encodeToString(str.getBytes(), Base64.NO_WRAP);
                System.out.println(Base64Str);
                Request request = chain.request().newBuilder().addHeader("Authorization", Base64Str).build();

                return chain.proceed(request);
            }
        });   
        CookieManager cookieManager = new CookieManager();
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        httpClientAuthz.cookieJar(new JavaNetCookieJar(cookieManager));

        LoginRetrofitAuthz = builder.client(httpClientAuthz.build()).build();
    }
    return LoginRetrofitAuthz;
}

服务器结果(顶部登录、底部、重定向) enter image description here

你知道如何在重定向时保持标题吗?

最佳答案

其实罪魁祸首是OkHttp,而不是Retrofit。 OkHttp 故意删除所有身份验证 header :

https://github.com/square/okhttp/blob/7cf6363662c7793c7694c8da0641be0508e04241/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpEngine.java

// When redirecting across hosts, drop all authentication headers. This
// is potentially annoying to the application layer since they have no
// way to retain them.
if (!sameConnection(url)) {
  requestBuilder.removeHeader("Authorization");
}

这里是对这个问题的讨论:https://github.com/square/retrofit/issues/977

您可以使用 OkHttp 验证器。如果返回 401 错误,它将被调用。因此您可以使用它来重新验证请求。

httpClient.authenticator(new Authenticator() {
            @Override
            public Request authenticate(Route route, Response response) throws IOException {
                return response.request().newBuilder()
                        .header("Authorization", "Token " + DataManager.getInstance().getPreferencesManager().getAuthToken())
                        .build();
            }
        });

但是在我的情况下,服务器返回 403 Forbidden 而不是 401。我必须得到 response.headers().get("Location");

就地创建并触发另一个网络请求:

public Call<Response> getMoreBills(@Header("Authorization") String authorization, @Url String nextPage)

关于android - 当我在改造中重定向时如何包含标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40098623/

相关文章:

java - 方法总是返回空列表

适用于所有 Android 设备的 Android Badger

android - 将自定义过滤器应用于相机输出

禁止 Python HTTP 重定向请求

linux - 通过 tee 发送到文件的转换流

javascript - 如何在没有 javascript 或 headers 的情况下在 PHP 中重定向

json - 使用 gson 和 Retrofit 2 将所有 JSON 存储在 Map 中

java - Android 架构组件和 MVVM 的错误处理

android - 让 Android EditText 向上延伸而不是向下延伸

android - Retrofit2 - 缺少@GET URL 或@Url 参数 - Proguard