2828import javax .naming .Reference ;
2929import javax .naming .Referenceable ;
3030import javax .naming .StringRefAddr ;
31+ import javax .net .ssl .HostnameVerifier ;
3132import javax .net .ssl .SSLContext ;
3233import javax .net .ssl .SSLException ;
3334import java .io .IOException ;
@@ -116,6 +117,13 @@ public class RMQConnectionFactory implements ConnectionFactory, Referenceable, S
116117 */
117118 private boolean hostnameVerification = false ;
118119
120+ /**
121+ * {@link HostnameVerifier} to use when TLS is on.
122+ *
123+ * @since 1.10.0
124+ */
125+ private HostnameVerifier hostnameVerifier ;
126+
119127
120128 /** The maximum number of messages to read on a queue browser, which must be non-negative;
121129 * 0 means unlimited and is the default; negative values are interpreted as 0. */
@@ -162,8 +170,8 @@ public Connection createConnection(String username, String password) throws JMSE
162170 com .rabbitmq .client .ConnectionFactory factory = new com .rabbitmq .client .ConnectionFactory ();
163171 setRabbitUri (logger , this , factory , this .getUri ());
164172 maybeEnableTLS (factory );
165- factory .setMetricsCollector (this .metricsCollector );
166173 maybeEnableHostnameVerification (factory );
174+ factory .setMetricsCollector (this .metricsCollector );
167175 com .rabbitmq .client .Connection rabbitConnection = instantiateNodeConnection (factory );
168176
169177 RMQConnection conn = new RMQConnection (new ConnectionParams ()
@@ -189,6 +197,7 @@ public Connection createConnection(String username, String password, List<Addres
189197 this .password = password ;
190198 com .rabbitmq .client .ConnectionFactory cf = new com .rabbitmq .client .ConnectionFactory ();
191199 maybeEnableTLS (cf );
200+ maybeEnableHostnameVerification (cf );
192201 cf .setMetricsCollector (this .metricsCollector );
193202 com .rabbitmq .client .Connection rabbitConnection = instantiateNodeConnection (cf , endpoints );
194203
@@ -350,9 +359,13 @@ private void maybeEnableTLS(com.rabbitmq.client.ConnectionFactory factory) {
350359 }
351360
352361 private void maybeEnableHostnameVerification (com .rabbitmq .client .ConnectionFactory factory ) {
353- if (hostnameVerification ) {
362+ if (hostnameVerification || hostnameVerifier != null ) {
354363 if (this .ssl ) {
355- factory .enableHostnameVerification ();
364+ if (hostnameVerifier == null ) {
365+ factory .enableHostnameVerification ();
366+ } else {
367+ factory .enableHostnameVerification (this .hostnameVerifier );
368+ }
356369 } else {
357370 logger .warn ("Hostname verification enabled, but not TLS, please enable TLS too." );
358371 }
@@ -764,14 +777,38 @@ public void setMetricsCollector(MetricsCollector metricsCollector) {
764777
765778 /**
766779 * Enable or disable hostname verification when TLS is used.
780+ * <p>
781+ * If using Java 7 and more, the hostname verification will be handled
782+ * by the JVM as part of the TLS handshake. If using Java 6,
783+ * the hostname verification is performed by the {@link HostnameVerifier}
784+ * from the Commons HttpClient project. This implies that Commons HttpClient
785+ * and its dependencies are added to the classpath. To use another {@link HostnameVerifier},
786+ * use {@link RMQConnectionFactory#setHostnameVerifier(HostnameVerifier)}.
787+ *
767788 *
768789 * @param hostnameVerification
769790 * @see com.rabbitmq.client.ConnectionFactory#enableHostnameVerification()
791+ * @see com.rabbitmq.client.ConnectionFactory#enableHostnameVerification(HostnameVerifier)
792+ * @see #setHostnameVerifier(HostnameVerifier)
770793 * @since 1.10.0
771794 */
772795 public void setHostnameVerification (boolean hostnameVerification ) {
773796 this .hostnameVerification = hostnameVerification ;
774797 }
775798
799+ /**
800+ * Set the {@link HostnameVerifier} to use for the hostname verification.
801+ * <p>
802+ * Setting an {@link HostnameVerifier} is relevant for Java 6, as the JVM
803+ * can perform the hostname verification as of Java 7.
804+ *
805+ * @param hostnameVerifier
806+ * @see #setHostnameVerification(boolean)
807+ * @see com.rabbitmq.client.ConnectionFactory#enableHostnameVerification(HostnameVerifier)
808+ * @since 1.10.0
809+ */
810+ public void setHostnameVerifier (HostnameVerifier hostnameVerifier ) {
811+ this .hostnameVerifier = hostnameVerifier ;
812+ }
776813}
777814
0 commit comments