我的项目已经使用Android Volley网络框架很长一段时间了,但最近我发现了一个发布在互联网上的SSL3.0协议错误。
我想知道如何找到我的项目使用的TLS版本,以及如何确认库是否更新。
以下是我的源代码片段:
HttpStack stack = new HurlStack();
Network network = new BasicNetwork(stack);
mHttpRequestQueue = new RequestQueue(new NoCache(), network);
mHttpRequestQueue.start();
我认为重点在于HurlStack类,它依赖于org.apache.http
包,但是我不知道TLS/SSL配置在哪里。
发布于 2015-11-23 23:00:07
您可以通过创建自定义HTTPStack并在Volley.java的Volley.newRequestQueue(context, httpStack)
方法中设置堆栈来修改Volley中使用的TLS的版本。不过,您只需要为Android版本16-19执行此操作。在v16之前,不支持TLS1.2,在v19之后,默认启用TLS1.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.2TLS协议:https://gist.github.com/fkrauthan/ac8624466a4dee4fd02f#file-tlssocketfactory-java
发布于 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
发布于 2017-10-10 19:51:17
@w3bshark的答案对我很有效。但在使用该代码之前,请确保包含用于更新安全提供商的代码。在我的例子中,直到我更新了安全提供者,TLS才能工作。以下是更新它的代码。
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.");
}
}
https://stackoverflow.com/questions/31269425
复制相似问题