Add support for custom tls server names. (#22372)

* Add support for custom tls server names.

* Update samples

* Fix missing declaration.
This commit is contained in:
Brendan Burns
2025-11-19 18:36:11 -08:00
committed by GitHub
parent a1b962d0b6
commit 8a4246cbaf
16 changed files with 576 additions and 32 deletions

View File

@@ -91,7 +91,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -376,6 +377,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1635,7 +1659,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@@ -91,7 +91,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -376,6 +377,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1635,7 +1659,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@@ -92,7 +92,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -380,6 +381,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1655,7 +1679,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@@ -97,7 +97,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -393,6 +394,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1640,7 +1664,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@@ -91,7 +91,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -376,6 +377,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1635,7 +1659,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@@ -91,7 +91,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -379,6 +380,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1638,7 +1662,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@@ -91,7 +91,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -382,6 +383,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1641,7 +1665,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@@ -91,7 +91,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -376,6 +377,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1635,7 +1659,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@@ -91,7 +91,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -376,6 +377,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1635,7 +1659,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@@ -134,7 +134,8 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;
protected OkHttpClient httpClient;
protected JSON json;
@@ -428,6 +429,29 @@ public class ApiClient {
return this;
}
/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}
/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}
/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
@@ -1709,7 +1733,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");