我正在尝试使用 HttpsUrlConnection 类向服务器发送请求。服务器存在证书问题,因此我设置了一个信任所有内容的 TrustManager,以及一个同样宽松的主机名验证器。当我直接发出请求时,这个管理器工作得很好,但当我通过代理发送请求时,它似乎根本没有被使用。
我这样设置代理设置:
Properties systemProperties = System.getProperties();
systemProperties.setProperty( "http.proxyHost", "proxyserver" );
systemProperties.setProperty( "http.proxyPort", "8080" );
systemProperties.setProperty( "https.proxyHost", "proxyserver" );
systemProperties.setProperty( "https.proxyPort", "8080" );
默认 SSLSocketFactory 的 TrustManager 设置如下:
SSLContext sslContext = SSLContext.getInstance( "SSL" );
// set up a TrustManager that trusts everything
sslContext.init( null, new TrustManager[]
{
new X509TrustManager()
{
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted( X509Certificate[] certs, String authType )
{
// everything is trusted
}
public void checkServerTrusted( X509Certificate[] certs, String authType )
{
// everything is trusted
}
}
}, new SecureRandom() );
// this doesn't seem to apply to connections through a proxy
HttpsURLConnection.setDefaultSSLSocketFactory( sslContext.getSocketFactory() );
// setup a hostname verifier that verifies everything
HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier()
{
public boolean verify( String arg0, SSLSession arg1 )
{
return true;
}
} );
如果我运行以下代码,最终会出现 SSLHandshakException(“握手期间远程主机关闭连接”):
URL url = new URL( "https://someurl" );
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setDoOutput( true );
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
connection.setRequestProperty( "Content-Length", "0" );
connection.connect();
我假设我在处理 SSL 时缺少某种与使用代理有关的设置。如果我不使用代理,我的 checkServerTrusted 方法将被调用;当我通过代理时,这也是我需要发生的事情。
我通常不与 Java 打交道,并且对 HTTP/web 内容也没有太多经验。我相信我已经提供了理解我正在尝试做的事情所需的所有细节。如果情况并非如此,请告诉我。
Update:
阅读了ZZ Coder建议的文章后,我对连接代码做了如下修改:
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setSSLSocketFactory( new SSLTunnelSocketFactory( proxyHost, proxyPort ) );
connection.setDoOutput( true );
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
connection.setRequestProperty( "Content-Length", "0" );
connection.connect();
结果(SSLHandshakeException)是相同的。当我将这里的 SLLSocketFactory 设置为 SSLTunnelSocketFactory(文章中解释的类)时,我使用 TrustManager 和 SSLContext 所做的事情被覆盖。我还不需要那个吗?
另一个更新:
我修改了 SSLTunnelSocketFactory 类以使用 SSLSocketFactory,该 SSLSocketFactory 使用信任所有内容的 TrustManager。这似乎没有产生任何影响。这是 SSLTunnelSocketFactory 的 createSocket 方法:
public Socket createSocket( Socket s, String host, int port, boolean autoClose )
throws IOException, UnknownHostException
{
Socket tunnel = new Socket( tunnelHost, tunnelPort );
doTunnelHandshake( tunnel, host, port );
SSLSocket result = (SSLSocket)dfactory.createSocket(
tunnel, host, port, autoClose );
result.addHandshakeCompletedListener(
new HandshakeCompletedListener()
{
public void handshakeCompleted( HandshakeCompletedEvent event )
{
System.out.println( "Handshake finished!" );
System.out.println(
"\t CipherSuite:" + event.getCipherSuite() );
System.out.println(
"\t SessionId " + event.getSession() );
System.out.println(
"\t PeerHost " + event.getSession().getPeerHost() );
}
} );
result.startHandshake();
return result;
}
当我的代码调用connection.connect时,就会调用这个方法,并且调用doTunnelHandshake成功。下一行代码使用我的 SSLSocketFactory 创建 SSLSocket;此调用后结果的 toString 值为:
“1d49247[SSL_NULL_WITH_NULL_NULL:套接字[addr=/proxyHost,端口=proxyPort,本地端口=24372]]”.
这对我来说毫无意义,但这可能是此后事情崩溃的原因。
当调用 result.startHandshake() 时,根据调用堆栈 HttpsClient.afterConnect,使用相同的参数再次调用相同的 createSocket 方法,但 Socket s 为 null,并且当它转到 result.startHandshake() 时再次,结果是相同的 SSLHandshakeException。
对于这个日益复杂的谜题,我是否仍然缺少重要的一块?
这是堆栈跟踪:
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:808)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1112)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1139)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1123)
at gsauthentication.SSLTunnelSocketFactory.createSocket(SSLTunnelSocketFactory.java:106)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:391)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:133)
at gsauthentication.GSAuthentication.main(GSAuthentication.java:52)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:333)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:789)
... 8 more