Skip to content

Commit f5f5808

Browse files
committed
Document hostname verification for Java client
References rabbitmq/rabbitmq-java-client#394
1 parent d0195b1 commit f5f5808

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

site/api-guide.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ factory.useSslProtocol();
16981698
To learn more about TLS support in RabbitMQ, see
16991699
the <a href="ssl.html">TLS guide</a>. If you only want to configure
17001700
the Java client (especially the peer verification and trust manager parts),
1701-
read <a href="ssl.html#trust-levels">the appropriate section</a> of the TLS guide.
1701+
read <a href="ssl.html#java-client">the appropriate section</a> of the TLS guide.
17021702
</p>
17031703
</doc:section>
17041704
</body>

site/ssl.xml

+37-19
Original file line numberDiff line numberDiff line change
@@ -722,14 +722,11 @@ ssl_options.fail_if_no_peer_cert = false
722722
import java.io.*;
723723
import java.security.*;
724724

725-
726725
import com.rabbitmq.client.*;
727726

728-
public class Example1
729-
{
730-
public static void main(String[] args) throws Exception
731-
{
727+
public class Example1 {
732728

729+
public static void main(String[] args) throws Exception {
733730
ConnectionFactory factory = new ConnectionFactory();
734731
factory.setHost(&quot;localhost&quot;);
735732
factory.setPort(5671);
@@ -745,16 +742,14 @@ public class Example1
745742
channel.queueDeclare(&quot;rabbitmq-java-test&quot;, false, true, true, null);
746743
channel.basicPublish(&quot;&quot;, &quot;rabbitmq-java-test&quot;, null, &quot;Hello, World&quot;.getBytes());
747744

748-
749745
GetResponse chResponse = channel.basicGet(&quot;rabbitmq-java-test&quot;, false);
750-
if(chResponse == null) {
746+
if (chResponse == null) {
751747
System.out.println(&quot;No message retrieved&quot;);
752748
} else {
753749
byte[] body = chResponse.getBody();
754-
System.out.println(&quot;Recieved: &quot; + new String(body));
750+
System.out.println(&quot;Received: &quot; + new String(body));
755751
}
756752

757-
758753
channel.close();
759754
conn.close();
760755
}
@@ -806,12 +801,9 @@ import javax.net.ssl.*;
806801

807802
import com.rabbitmq.client.*;
808803

804+
public class Example2 {
809805

810-
public class Example2
811-
{
812-
public static void main(String[] args) throws Exception
813-
{
814-
806+
public static void main(String[] args) throws Exception {
815807
char[] keyPassphrase = &quot;MySecretPassword&quot;.toCharArray();
816808
KeyStore ks = KeyStore.getInstance(&quot;PKCS12&quot;);
817809
ks.load(new FileInputStream(&quot;/path/to/client_key.p12&quot;), keyPassphrase);
@@ -826,30 +818,29 @@ public class Example2
826818
TrustManagerFactory tmf = TrustManagerFactory.getInstance(&quot;SunX509&quot;);
827819
tmf.init(tks);
828820

829-
SSLContext c = SSLContext.getInstance(&quot;TLSv1.1&quot;);
821+
SSLContext c = SSLContext.getInstance(&quot;TLSv1.2&quot;);
830822
c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
831823

832824
ConnectionFactory factory = new ConnectionFactory();
833825
factory.setHost(&quot;localhost&quot;);
834826
factory.setPort(5671);
835827
factory.useSslProtocol(c);
828+
factory.enableHostnameVerification();
836829

837830
Connection conn = factory.newConnection();
838831
Channel channel = conn.createChannel();
839832

840833
channel.queueDeclare(&quot;rabbitmq-java-test&quot;, false, true, true, null);
841834
channel.basicpublish(&quot;&quot;, &quot;rabbitmq-java-test&quot;, null, &quot;Hello, World&quot;.getBytes());
842835

843-
844836
GetResponse chResponse = channel.basicGet(&quot;rabbitmq-java-test&quot;, false);
845-
if(chResponse == null) {
837+
if (chResponse == null) {
846838
System.out.println(&quot;No message retrieved&quot;);
847839
} else {
848840
byte[] body = chResponse.getBody();
849-
System.out.println(&quot;Recieved: &quot; + new String(body));
841+
System.out.println(&quot;Received: &quot; + new String(body));
850842
}
851843

852-
853844
channel.close();
854845
conn.close();
855846
}
@@ -861,6 +852,33 @@ public class Example2
861852
a RabbitMQ node with a certificate that has not been imported
862853
into the key store and watch the connection fail.
863854
</p>
855+
856+
<p>
857+
Note hostname verification must be explicitly enabled with
858+
<code>ConnectionFactory#enableHostnameVerification()</code>. This checks
859+
that the server certificate has been issued for the hostname the
860+
client is requested. If you're using Java 6, you need to add
861+
the Commons HttpClient dependency to your project, e.g. for Maven
862+
and Gradle:
863+
</p>
864+
<pre class="sourcecode xml">
865+
&lt;!-- Maven dependency to add for hostname verification on Java 6 --&gt;
866+
&lt;dependency&gt;
867+
&lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
868+
&lt;artifactId&gt;httpclient&lt;/artifactId&gt;
869+
&lt;version&gt;4.5.6&lt;/version&gt;
870+
&lt;/dependency&gt;
871+
</pre>
872+
<pre class="sourcecode groovy">
873+
// Gradle dependency to add for hostname verification on Java 6
874+
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
875+
</pre>
876+
<p>If you don't want to use Commons HttpClient, use
877+
<code>ConnectionFactory#enableHostnameVerification(HostnameVerifier)</code>
878+
with the <code>HostnameVerifier</code> instance of your choice. Again, this is
879+
needed only for Java 6, hostname verification is built-in in Java 7 and more.
880+
</p>
881+
864882
</doc:subsection>
865883

866884
<doc:subsection name="tls-versions-java-client">

0 commit comments

Comments
 (0)