SSL固定在同一请求中

时间:2017-02-21 07:40:47

标签: ssl okhttp3

我的问题是,在每次SSL Pinning请求

之前,我是否必须另行请求检查Get/Post
OkHttpClient client = new OkHttpClient.Builder().certificatePinner(
                new CertificatePinner.Builder()
                        .add(pinningUrl, "sha256/invalidPIN")
                        .build()).build();
Request request = new Request.Builder()
                .url(pinningUrl)
                .build();
Response response = client.newCall(request).execute();

或者我可以像{/ p>这样的每个Get/Post检查一下

CertificatePinner certificatePinner = new CertificatePinner.Builder()
                        .add(pinningUrl, "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")                    .build();
                OkHttpClient client = new OkHttpClient().newBuilder().certificatePinner(certificatePinner).build();
Request request = new Request.Builder()         .url(getResources().getString(R.string.server_url_user_mgmt_services))
                        .addHeader("Content-Type", "application).post(body)
                        .build();

                client.newCall(request)
                        .enqueue(new Callback() {

                            @Override
                            public void onFailure(Call call, IOException e) {
                                pd.dismiss();
                                Toast.makeText(LoginActivity.this, "Some error occured!\nTry Again", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                String str = response.body().toString();
                            }
                        });

如果我在每次请求时都检查了请求是否已执行,但它没有检查certificate是否帮助我。

1 个答案:

答案 0 :(得分:2)

根据您的第一个代码示例,您似乎尝试使用网址而不是主机名或通配符。

您应该在OkHttpClient 每台主机上配置一次,然后发出正常请求。您定义的引脚应该将主机作为密钥,而不是URL。

https://square.github.io/okhttp/3.x/okhttp/okhttp3/CertificatePinner.html

 String hostname = "publicobject.com";
 CertificatePinner certificatePinner = new CertificatePinner.Builder()
     .add(hostname, "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
     .build();
 OkHttpClient client = OkHttpClient.Builder()
     .certificatePinner(certificatePinner)
     .build();

 Request request = new Request.Builder()
     .url("https://" + hostname)
     .build();
 client.newCall(request).execute();