我正在创建授权应用程序,我正在使用Retrofit 2.当我正在进行调用时,这将转到onFailure方法并获得异常
"javax.net.ssl.SSLException: Connection closed by peer"
但问题是,昨天这个很有效。今天它给了例外。我在互联网上找到了一些SSLException - Connection closed by peer on Android 4.x versions或How to add TLS v 1.0 and TLS v.1.1 with Retrofit,但这对我没有帮助。任何想法如何解决它。在后端TLS1.2启用。
public class RegistrationFragment extends BaseFragment {
View mainView;
ApiClient apiClient = ApiClient.getInstance();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainView = inflater.inflate
(R.layout.registration_fragment, container, false);
//Calling the authorization method
registerCall();
}
});
return mainView;
}
//User authorization method
public void registerCall() {
Call<ResponseBody> call = apiClient.registration(supportopObj);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
//Calling the clientCall method for getting the user clientID and clientSecret
Toast.makeText(getActivity(), "Registration Successful ",
Toast.LENGTH_SHORT).show();;
} else {
//if the response not successful
Toast.makeText(getActivity(), "Could not register the user maybe already registered ",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "An error occurred", Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:0)
这不是因为改造而是因为okhttp。如果您使用okhttp版本3.x,您将遇到此问题。立即解决方案是使用okhttp版本2.x.还有一件事是,此问题仅适用于Android版本16-20 Link for reference
答案 1 :(得分:0)
我在我的OkHttp初始化类中使用了类似的东西,但在这里使用是否安全?
我创建了一个新课程Tls12SocketFactory.class
就是这样。
public class Tls12SocketFactory extends SSLSocketFactory {
private static final String[] TLS_V12_ONLY = {"TLSv1.2"};
final SSLSocketFactory delegate;
public Tls12SocketFactory(SSLSocketFactory base) {
this.delegate = base;
}
@Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return patch(delegate.createSocket(s, host, port, autoClose));
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return patch(delegate.createSocket(host, port));
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return patch(delegate.createSocket(host, port, localHost, localPort));
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return patch(delegate.createSocket(host, port));
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return patch(delegate.createSocket(address, port, localAddress, localPort));
}
private Socket patch(Socket s) {
if (s instanceof SSLSocket) {
((SSLSocket) s).setEnabledProtocols(TLS_V12_ONLY);
}
return s;
}
}
我在OkHttp初始化类中做了类似的事情。
X509TrustManager trustManager = null;
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
trustManager = (X509TrustManager) trustManagers[0];
} catch (KeyStoreException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
OkHttpClient.Builder client = new OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS);
try {
SSLContext sc = SSLContext.getInstance("TLSv1.2");
sc.init(null, new TrustManager[] { trustManager }, null);
client.sslSocketFactory(new Tls12SocketFactory(sc.getSocketFactory()), trustManager);
ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.build();
client.connectionSpecs(Collections.singletonList(cs));
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
name = new Retrofit.Builder()
.baseUrl(endpoint)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
在Android studio中使用此代码是安全的。并且这个代码将来会出现任何问题吗?谢谢。我认为我的回答也可以帮助其他人。