又是我,
有人帮我改了这个,
OkHttpClient client() {
if (client != null)
return client;
if (System.getenv("trustSelfSignedCert") != null
&& System.getenv("trustSelfSignedCert").equalsIgnoreCase("true"))
trustSelfSigned = true;
if (System.getProperty("trustSelfSignedCert") != null
&& System.getProperty("trustSelfSignedCert").equalsIgnoreCase("true"))
trustSelfSigned = true;
OkHttpClient client0 = new OkHttpClient();
// if (proxy)
// client0.setProxy(new Proxy(Type.HTTP, new InetSocketAddress("proxy.domain.com", 8080)));
if (trustSelfSigned) {
TrustManager[] certs = new TrustManager[] { new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
} };
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance(tls);
ctx.init(null, certs, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
client0.setSslSocketFactory(ctx.getSocketFactory());
} catch (java.security.GeneralSecurityException ex) {
}
}
client0.setWriteTimeout(timeout * 1000L, TimeUnit.MILLISECONDS);
client0.setReadTimeout(timeout * 1000L, TimeUnit.MILLISECONDS);
return client = client0;
}
为了这个,
OkHttpClient client() {
if (client != null)
return client;
if (System.getenv("trustSelfSignedCert") != null
&& System.getenv("trustSelfSignedCert").equalsIgnoreCase("true"))
trustSelfSigned = true;
if (System.getProperty("trustSelfSignedCert") != null
&& System.getProperty("trustSelfSignedCert").equalsIgnoreCase("true"))
trustSelfSigned = true;
SSLContext ctx = null;
if (trustSelfSigned) {
TrustManager[] certs = new TrustManager[] { new X509TrustManager() {
private X509TrustManager standardTrustManager = null;
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
@Override
public void checkServerTrusted(X509Certificate[] certificates, String authType)
throws CertificateException {
if ((certificates != null) && (certificates.length == 1)) {
certificates[0].checkValidity();
} else {
standardTrustManager.checkServerTrusted(certificates, authType);
}
}
@Override
public void checkClientTrusted(X509Certificate[] certificates, String authType)
throws CertificateException {
standardTrustManager.checkClientTrusted(certificates, authType);
}
} };
try {
ctx = SSLContext.getInstance(tls);
ctx.init(null, certs, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
} catch (java.security.GeneralSecurityException ex) {
}
}
if (proxy) {
Proxy proxY = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.domain.com", 8080));
OkHttpClient client0 = new OkHttpClient.Builder().proxy(proxY)
.sslSocketFactory(ctx.getSocketFactory())
.writeTimeout(timeout * 1000L, TimeUnit.MILLISECONDS)
.readTimeout(timeout * 1000L, TimeUnit.MILLISECONDS)
.build();
return client = client0;
} else {
OkHttpClient client0 = new OkHttpClient.Builder()
.sslSocketFactory(ctx.getSocketFactory())
.writeTimeout(timeout * 1000L, TimeUnit.MILLISECONDS)
.readTimeout(timeout * 1000L, TimeUnit.MILLISECONDS)
.build();
return client = client0;
}
}
不幸的是,当我知道这个程序在哪里运行时,这种转换是错误的,因为在我之前,我只想修复这个漏洞。代理实际上可以在jvm参数上完成,所以我只是将其注释掉,或者您也可以将它添加到新代码中。问题在于setSslSocketFactory、setWriteTimeout和setReadTimeout。
如果我运行新代码,它就会出错,
javax.net.ssl.SSLException: java.lang.NullPointerException
希望有人会看到这段代码已经修复了类似的问题。再次感谢。
发布于 2022-07-22 21:10:27
这些都需要注释掉,这是一个错误。
private X509TrustManager standardTrustManager = null;
standardTrustManager.checkServerTrusted(certificates, authType);
Then it works!
https://stackoverflow.com/questions/73054942
复制相似问题