【问题标题】:SSL Certificate Pinning w/ Picasso带毕加索的 SSL 证书固定
【发布时间】:2015-08-06 15:04:07
【问题描述】:

我正在使用毕加索来缓存图像。我们的后端最近使用自签名证书固定作为身份验证切换到 HTTPS。我使用 khandroid 库创建了一个 HTTP 客户端,将证书固定到每个请求;基本上按照这个例子。

http://ogrelab.ikratko.com/using-android-volley-with-self-signed-certificate/

我现在需要将同样的概念应用到 Picasso,但不确定如何修改 Picasso 的单例以使用固定 SSL 证书。

【问题讨论】:

  • OkHttp 有证书固定; here is a pin generator for it。而且,IIRC,如果您的应用程序中包含 OkHttp 和 Picasso,那么您有一种方法可以配置 Picasso 使用的 OkHttp。

标签: android ssl picasso


【解决方案1】:

原来我只是找错地方了。我试图修改 OkHttpDownloader,但我需要修改 OkHttpClient。这是一些示例代码。

public static Picasso getInstance(Context context) {
        if (sPicasso == null) {
            InputStream keyStore = context.getResources().openRawResource(R.raw.my_keystore);
            Picasso.Builder builder = new Picasso.Builder(context);
            OkHttpClient okHttpClient = new OkHttpClient();
            SSLContext sslContext;
            try {
                sslContext = SSLContext.getInstance("TLS");
                sslContext.init(null, new TrustManager[]{new SsX509TrustManager(keyStore, password)}, null);
                okHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
                OkHttpDownloader okHttpDownloader = new OkHttpDownloader(okHttpClient);
                builder.downloader(okHttpDownloader);
                sPicasso = builder.build();
            } catch (NoSuchAlgorithmException e) {
                throw new IllegalStateException("Failure initializing default SSL context", e);
            } catch (KeyManagementException e) {
                throw new IllegalStateException("Failure initializing default SSL context", e);
            } catch (GeneralSecurityException e) {
                e.printStackTrace();
            }
        }

        return sPicasso;
    }

【讨论】:

  • 无法解决 SsX509TrustManager 出现错误,是否有任何依赖关系。
  • @AmitVerma 它是 javax.net.ssl.X509TrustManager 的实现
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 2020-04-14
  • 2019-09-17
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 2013-10-28
相关资源
最近更新 更多