首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android Volley中辨别TLS版本

如何在Android Volley中辨别TLS版本
EN

Stack Overflow用户
提问于 2015-07-07 21:07:52
回答 3查看 9.6K关注 0票数 10

我的项目已经使用Android Volley网络框架很长一段时间了,但最近我发现了一个发布在互联网上的SSL3.0协议错误。

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

以下是我的源代码片段:

代码语言:javascript
运行
复制
HttpStack stack = new HurlStack();
Network network = new BasicNetwork(stack);
mHttpRequestQueue = new RequestQueue(new NoCache(), network);
mHttpRequestQueue.start();

我认为重点在于HurlStack类,它依赖于org.apache.http包,但是我不知道TLS/SSL配置在哪里。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-11-23 23:00:07

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

代码语言:javascript
运行
复制
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.2TLS协议:https://gist.github.com/fkrauthan/ac8624466a4dee4fd02f#file-tlssocketfactory-java

票数 20
EN

Stack Overflow用户

发布于 2015-07-08 16:05:06

在Android上,使用的TLS版本主要取决于使用的Android版本。Apache Volley基于基于HttpsUrlConnection的Apache Http客户端,因此使用标准的SSL/TLS SSLSocketFactory。

在安卓4.3以下版本中,通常只支持SSLv3和TLS1.0。在更高版本中,TLS 1.1和1.2通常受支持,但被禁用。

从Android5开始,TLS1.1和TLS1.2都是supported and enabled by default

票数 1
EN

Stack Overflow用户

发布于 2017-10-10 19:51:17

@w3bshark的答案对我很有效。但在使用该代码之前,请确保包含用于更新安全提供商的代码。在我的例子中,直到我更新了安全提供者,TLS才能工作。以下是更新它的代码。

代码语言:javascript
运行
复制
private void updateAndroidSecurityProvider() {
    try {
        ProviderInstaller.installIfNeeded(this);
    } catch (GooglePlayServicesRepairableException e) {
        Log.e("Test321", "PlayServices not installed");
    } catch (GooglePlayServicesNotAvailableException e) {
        Log.e("Test321", "Google Play Services not available.");
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31269425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档