【问题标题】:Why does Volley fall back to SSLV3?为什么 Volley 回退到 SSLV3?
【发布时间】:2015-10-06 21:26:44
【问题描述】:

我一直在监控我的应用程序错误,但我多次看到以下错误

 javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb8f0fc28: Failure in SSL library, usually a protocol error

错误:14077410:SSL 例程:SSL23_GET_SERVER_HELLO:sslv3 警报握手失败 (external/openssl/ssl/s23_clnt.c:741 0xaa48cd5c:0x00000000)-javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL握手中止:ssl=0xb8f0fc28:SSL 库失败,通常是协议错误 错误:14077410:SSL 例程:SSL23_GET_SERVER_HELLO:sslv3 警报握手失败(外部/openssl/ssl/s23_clnt.c:741 0xaa48cd5c:0x00000000)

您可以看到错误是关于 SSLV3 的,而我的服务器仅支持 TLSV1.2。

似乎在某些客户端上 Volley 回退到使用 SSLV3(出于某种原因)并且出现错误。

出现此错误的用户是 Android 4.4.2、4.4.4 和 4.1.1 及更高版本。

有趣的是,我也在同一个应用程序中使用 DefaultHttpClient,但它似乎没有报告同样的问题。

我在 Volley 中使用默认的 HurlStack

我看过以下... Disable SSL as a protocol in HttpsURLConnection

https://code.google.com/p/android/issues/detail?id=78187

那么我有什么选择呢?

  1. 我对 Volley 回退到 SSLV3 的假设是否正确?

  2. 为什么凌空回退到 SSLV3?换句话说,导致回退的原始故障是什么以及如何解决?

  3. 我最近下载了 Volley,但我不确定它是最新的。如何找到我拥有的版本?。

有什么想法吗?

【问题讨论】:

标签: android ssl android-volley sslhandshakeexception sslv3


【解决方案1】:

您的服务器不支持 SSLv3,因为它存在一些安全问题,不应使用。

使用 Kitkat 之前的 Android 版本时,您必须使用移除 SSLv3 以用作默认配置的套接字工厂:

public class VolleyToolboxExtension extends Volley {
    /** Default on-disk cache directory. */
    private static final String DEFAULT_CACHE_DIR = "volley";

    /**
     * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
     *
     * @param context A {@link Context} to use for creating the cache dir.
     * @param stack An {@link HttpStack} to use for the network, or null for default.
     * @return A started {@link RequestQueue} instance.
     */
    public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
        File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
        String userAgent = "volley/0";
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            userAgent = packageName + "/" + info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {

        }
        if (stack == null) {
            if (Build.VERSION.SDK_INT >= 9) {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                    // Use a socket factory that removes sslv3
                    stack = new HurlStack(null, new NoSSLv3Compat.NoSSLv3Factory());
                } else {
                    stack = new HurlStack();
                }
            } else {
                // Prior to Gingerbread, HttpUrlConnection was unreliable.
                // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }
        Network network = new BasicNetwork(stack);
        RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
        queue.start();
        return queue;
    }

    /**
     * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
     *
     * @param context A {@link Context} to use for creating the cache dir.
     * @return A started {@link RequestQueue} instance.
     */
    public static RequestQueue newRequestQueue(Context context) {
        return newRequestQueue(context, null);
    }

}

NoSSLv3Compat 类可以在这里找到: https://github.com/Floens/volley/blob/master/src/com/android/volley/compat/NoSSLv3Compat.java

使用这个扩展来创建你的请求队列:

    /**
     * @return The Volley Request queue, the queue will be created if it is null
     */
    public RequestQueue getRequestQueue() {
        // lazy initialize the request queue, the queue instance will be
        // created when it is accessed for the first time
        if (mRequestQueue == null) {
            // Create the request queue
            mRequestQueue = VolleyToolboxExtension.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

您也可以使用 Retrofit 代替 Volley,因为 Square 发布了支持 TLS 版本配置的该库的 2.1 版本:

http://square.github.io/retrofit/

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 2014-06-26
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多