Ale*_*ang 8 ssl https android android-volley sslsocketfactory
我的项目已经使用Android Volley网络框架很长一段时间了,但最近我发现了Internet上发布的SSL 3.0协议错误.
我想知道如何找出我的项目使用的TLS版本,以及如何确认库是否更新.
这是我的源代码片段:
HttpStack stack = new HurlStack();
Network network = new BasicNetwork(stack);
mHttpRequestQueue = new RequestQueue(new NoCache(), network);
mHttpRequestQueue.start();
Run Code Online (Sandbox Code Playgroud)
我认为重点在于HurlStack类,它取决于org.apache.http包,但我无法弄清楚TLS/SSL配置的位置.
w3b*_*ark 18
您可以通过创建自定义HTTPStack并Volley.newRequestQueue(context, httpStack)在Volley.java中的方法中设置堆栈来修改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);
}
Run Code Online (Sandbox Code Playgroud)
然后使用扩展SSLSocketFactory的TLSSocketFactory类,就像这里创建的Florian Krauthan一样,启用了v1.2 TLS协议:https://gist.github.com/fkrauthan/ac8624466a4dee4fd02f#file-tlssocketfactory-java
| 归档时间: |
|
| 查看次数: |
7496 次 |
| 最近记录: |