/** * <code>HostnameVerifier</code> provides a callback mechanism so that * implementers of this interface can supply a policy for * handling the case where the host to connect to and * the server name from the certificate mismatch. * <p> * The default implementation will deny such connections. */privatestaticHostnameVerifierdefaultHostnameVerifier =
newDefaultHostnameVerifier();
/* * The initial default <code>HostnameVerifier</code>. Should be * updated for another other type of <code>HostnameVerifier</code> * that are created. */privatestaticclassDefaultHostnameVerifierimplementsHostnameVerifier {
@Overridepublicbooleanverify(Stringhostname, SSLSessionsession) {
returnfalse;
}
}
/** * The <code>hostnameVerifier</code> for this object. */protectedHostnameVerifierhostnameVerifier = defaultHostnameVerifier;
从 doc 看到 HostnameVerifier 是一种 fallback 机制,当要连接的 host 与服务端证书 server name 不匹配时才有作用。并且 javax.net.ssl.HttpsURLConnection 的默认机制是这时候拒绝此类请求。而 hutool 则默认放行所有这类请求。
版本情况
JDK版本: corretto_8_312
hutool版本: 5.7.18(请确保最新尝试是否还有问题)
问题描述(包括截图)
注意到 hutool HttpUtil 中 HttpConnection#setHttpsInfo 默认传入的 HostnameVerifier 是 DefaultSSLInfo.TRUST_ANY_HOSTNAME_VERIFIER,即:
查看 javax.net.ssl.HttpsURLConnection.DefaultHostnameVerifier 代码如下:
从 doc 看到 HostnameVerifier 是一种 fallback 机制,当要连接的 host 与服务端证书 server name 不匹配时才有作用。并且 javax.net.ssl.HttpsURLConnection 的默认机制是这时候拒绝此类请求。而 hutool 则默认放行所有这类请求。
如果调用 cn.hutool.http.HttpRequest 时手动传入上面的 javax.net.ssl.HttpsURLConnection.DefaultHostnameVerifier:
也会请求成功。按我理解这样才是合理的,如果服务端证书 server name 与 host 不匹配,应该中断请求才安全。请问我的理解是否正确?是不是其实 javax.net.ssl.HttpsURLConnection 的默认行为会有坑,hutool 才另外指定了 HostnameVerifier 呢?
谢谢。
The text was updated successfully, but these errors were encountered: