mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-07 18:06:08 +00:00
okhttp-gson: fix SSL settings with okhttp3 (#4226)
The old code used to work with older okhttp (< 3), but will throw NullPointerExceptions with okhttp3.
This commit is contained in:
committed by
William Cheng
parent
652b0f5bf6
commit
c3666e9350
@@ -15,6 +15,7 @@ package org.openapitools.client;
|
||||
|
||||
import okhttp3.*;
|
||||
import okhttp3.internal.http.HttpMethod;
|
||||
import okhttp3.internal.tls.OkHostnameVerifier;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import okhttp3.logging.HttpLoggingInterceptor.Level;
|
||||
import okio.BufferedSink;
|
||||
@@ -1305,8 +1306,8 @@ public class ApiClient {
|
||||
*/
|
||||
private void applySslSettings() {
|
||||
try {
|
||||
TrustManager[] trustManagers = null;
|
||||
HostnameVerifier hostnameVerifier = null;
|
||||
TrustManager[] trustManagers;
|
||||
HostnameVerifier hostnameVerifier;
|
||||
if (!verifyingSsl) {
|
||||
trustManagers = new TrustManager[]{
|
||||
new X509TrustManager() {
|
||||
@@ -1324,40 +1325,42 @@ public class ApiClient {
|
||||
}
|
||||
}
|
||||
};
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
hostnameVerifier = new HostnameVerifier() {
|
||||
@Override
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
} else if (sslCaCert != null) {
|
||||
char[] password = null; // Any password will work.
|
||||
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
|
||||
Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert);
|
||||
if (certificates.isEmpty()) {
|
||||
throw new IllegalArgumentException("expected non-empty set of trusted certificates");
|
||||
}
|
||||
KeyStore caKeyStore = newEmptyKeyStore(password);
|
||||
int index = 0;
|
||||
for (Certificate certificate : certificates) {
|
||||
String certificateAlias = "ca" + Integer.toString(index++);
|
||||
caKeyStore.setCertificateEntry(certificateAlias, certificate);
|
||||
}
|
||||
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||
trustManagerFactory.init(caKeyStore);
|
||||
trustManagers = trustManagerFactory.getTrustManagers();
|
||||
}
|
||||
|
||||
if (keyManagers != null || trustManagers != null) {
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(keyManagers, trustManagers, new SecureRandom());
|
||||
httpClient = httpClient.newBuilder().sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagers[0]).build();
|
||||
} else {
|
||||
httpClient = httpClient.newBuilder().sslSocketFactory(null, (X509TrustManager) trustManagers[0]).build();
|
||||
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||
|
||||
if (sslCaCert == null) {
|
||||
trustManagerFactory.init((KeyStore) null);
|
||||
} else {
|
||||
char[] password = null; // Any password will work.
|
||||
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
|
||||
Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert);
|
||||
if (certificates.isEmpty()) {
|
||||
throw new IllegalArgumentException("expected non-empty set of trusted certificates");
|
||||
}
|
||||
KeyStore caKeyStore = newEmptyKeyStore(password);
|
||||
int index = 0;
|
||||
for (Certificate certificate : certificates) {
|
||||
String certificateAlias = "ca" + Integer.toString(index++);
|
||||
caKeyStore.setCertificateEntry(certificateAlias, certificate);
|
||||
}
|
||||
trustManagerFactory.init(caKeyStore);
|
||||
}
|
||||
trustManagers = trustManagerFactory.getTrustManagers();
|
||||
hostnameVerifier = OkHostnameVerifier.INSTANCE;
|
||||
}
|
||||
|
||||
httpClient = httpClient.newBuilder().hostnameVerifier(hostnameVerifier).build();
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(keyManagers, trustManagers, new SecureRandom());
|
||||
httpClient = httpClient.newBuilder()
|
||||
.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagers[0])
|
||||
.hostnameVerifier(hostnameVerifier)
|
||||
.build();
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user