android - 我如何告诉 Android Volley 中的 TLS 版本

标签 android ssl https android-volley sslsocketfactory

我的项目长期使用Android Volley网络框架,但最近发现网上公布了一个SSL 3.0协议(protocol)的bug。

我想知道如何知道我的项目使用的 TLS 版本是什么,以及如何确认库是否更新。

这是我的源代码 fragment :

HttpStack stack = new HurlStack();
Network network = new BasicNetwork(stack);
mHttpRequestQueue = new RequestQueue(new NoCache(), network);
mHttpRequestQueue.start();

我认为重点在 HurlStack 类中,它取决于 org.apache.http 包,但我无法弄清楚 TLS/SSL 配置在哪里。

最佳答案

您可以通过创建自定义 HTTPStack 并在 Volley.java 中的 Volley.newRequestQueue(context, httpStack) 方法中设置堆栈来修改 Volley 中使用的 TLS 版本。 .虽然,您只需要为 Android 版本 16-19 执行此操作。在 v16 之前,不支持 TLS 1.2,在 v19 之后,默认启用 TLS 1.2。因此,对于 Android 版本 16-19,您应该专注于手动将 TLS 设置为 1.2。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
    && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
    try {
      ProviderInstaller.installIfNeeded(getContext());
    } catch (GooglePlayServicesRepairableException e) {
      // Indicates that Google Play services is out of date, disabled, etc.
      // Prompt the user to install/update/enable Google Play services.
      GooglePlayServicesUtil.showErrorNotification(e.getConnectionStatusCode(), getContext());
      // Notify the SyncManager that a soft error occurred.
      syncResult.stats.numIOExceptions++;
      return;
    } catch (GooglePlayServicesNotAvailableException e) {
      // Indicates a non-recoverable error; the ProviderInstaller is not able
      // to install an up-to-date Provider.
      // Notify the SyncManager that a hard error occurred.
      syncResult.stats.numAuthExceptions++;
      return;
    }

    HttpStack stack = null;
    try {
      stack = new HurlStack(null, new TLSSocketFactory());
    } catch (KeyManagementException e) {
      e.printStackTrace();
      Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
      stack = new HurlStack();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
      stack = new HurlStack();
    }
    requestQueue = Volley.newRequestQueue(context, stack);
} else {
  requestQueue = Volley.newRequestQueue(context);
}

然后使用扩展 SSLSocketFactory 的 TLSSocketFactory 类,就像 Florian Krauthan 在此处创建的那样,其中启用了 v1.2 TLS 协议(protocol):https://gist.github.com/fkrauthan/ac8624466a4dee4fd02f#file-tlssocketfactory-java

关于android - 我如何告诉 Android Volley 中的 TLS 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31269425/

相关文章:

java - 杀死应用程序时 SharedPreferences 消失

java - OpenCV 相机图像显示偏蓝

android - 未经授权的设备 - 不允许从此设备付款

ssl - 将中间证书加载到 Infoblox NIOS

ssl - 使用 golang mutual TLS auth 信任特定客户端

php - https 和来自另一个站点的图像。如何在不修改 .htaccess 文件的情况下使用 php 或 javascript 删除 http 图像的警告?

security - 如果我可以使用 https,为什么还要使用相关协议(protocol)?

java - Android - 创建篮球统计屏幕

Android 信任特定域证书

django - Django、Nginx 和 Gunicorn 上的 SSL