【问题标题】:Android Retrofit2 Trust anchor for certification path not found未找到用于认证路径的 Android Retrofit2 信任锚
【发布时间】:2018-07-15 14:33:14
【问题描述】:

当我打开 api 链接时,我有一个使用 SLL 认证的 API 谷歌浏览器要求我使用用户名和密码登录以授权我连接网站 API 链接:https://45.55.43.15:9090/api/machine?page=0&size=10

我的问题是如何使用 OkHttp3 和 Retrofit2 实现这一点

这是我的试验

 public class RestAdapter {


    Context context;
    public static final String BASE_URL = "https://45.55.43.15:9090/api/";


    private OkHttpClient okHttpClient;
    private Authenticator authenticator = new Authenticator() {
        @Override
        public Request authenticate(Route route, Response response) {
            return null;
        }
    };


    private RestAdapter() {
    }

    public void setAuthenticator(Authenticator authenticator) {
        this.authenticator = authenticator;
    }


    public static class Builder {
        String email, password;
        RestAdapter apiManager = new RestAdapter();

        public Builder setAuthenticator(Authenticator authenticator) {
            apiManager.setAuthenticator(authenticator);
            return this;
        }

        public RestAdapter build(String param_email, String param_password) {
            this.email = param_email;
            this.password = param_password;
            return apiManager.newInstance(email, password);
        }

    }

    public class RequestTokenInterceptor implements Interceptor {
        String email, password;
        String credentials, basic;
        public RequestTokenInterceptor(String email, String password) {
            this.email = email;
            this.password = password;
            credentials = email + ":" + password;
            basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
        }



        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder builder = original.newBuilder()
                    .addHeader("Authorization", basic)
                    .method(original.method(), original.body());
            return chain.proceed(builder.build());
        }
    }

    private RestAdapter newInstance(String email, String password) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.e("https", message);
            }
        });
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);


        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .addInterceptor(new RequestTokenInterceptor(email, password))
                .authenticator(authenticator)
                .build();


        return this;
    }


    public <T> T createRest(Class<T> t) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

        return retrofit.create(t);
    }
 }

【问题讨论】:

    标签: android retrofit2 okhttp3


    【解决方案1】:

    该服务器正在使用自签名证书。例如,Web 浏览器会因此拒绝连接到它。

    使用network security configurationOkHttp's custom certificate support 允许您的代码连接到此服务器。或者,切换到具有域名和标准 SSL 证书的服务器。

    【讨论】:

    • 我知道服务器使用自签名证书,但我的问题是如何使用 Retrofit2 解决这个问题,我需要一些示例来编写正确的代码谢谢
    • @KhaledMohammad:“我如何使用 Retrofit2 解决这个问题”——正如我所写,要么使用网络安全配置,要么使用 OkHttp 的自定义证书支持,让您的代码连接到该服务器。您可以向 Retrofit 提供 OkHttpClient,您已在其中配置 OkHttpClient 以支持您的自签名证书。有关网络安全配置解决方案的更多信息,请参阅thisthis
    猜你喜欢
    • 2018-01-06
    • 2015-11-16
    • 2019-01-02
    • 2022-01-06
    • 2017-01-08
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多