@@ -722,14 +722,11 @@ ssl_options.fail_if_no_peer_cert = false
722
722
import java.io.*;
723
723
import java.security.*;
724
724
725
-
726
725
import com.rabbitmq.client.*;
727
726
728
- public class Example1
729
- {
730
- public static void main(String[] args) throws Exception
731
- {
727
+ public class Example1 {
732
728
729
+ public static void main(String[] args) throws Exception {
733
730
ConnectionFactory factory = new ConnectionFactory();
734
731
factory.setHost(" localhost" );
735
732
factory.setPort(5671);
@@ -745,16 +742,14 @@ public class Example1
745
742
channel.queueDeclare(" rabbitmq-java-test" , false, true, true, null);
746
743
channel.basicPublish("" , " rabbitmq-java-test" , null, " Hello, World" .getBytes());
747
744
748
-
749
745
GetResponse chResponse = channel.basicGet(" rabbitmq-java-test" , false);
750
- if(chResponse == null) {
746
+ if (chResponse == null) {
751
747
System.out.println(" No message retrieved" );
752
748
} else {
753
749
byte[] body = chResponse.getBody();
754
- System.out.println(" Recieved : " + new String(body));
750
+ System.out.println(" Received : " + new String(body));
755
751
}
756
752
757
-
758
753
channel.close();
759
754
conn.close();
760
755
}
@@ -806,12 +801,9 @@ import javax.net.ssl.*;
806
801
807
802
import com.rabbitmq.client.*;
808
803
804
+ public class Example2 {
809
805
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 {
815
807
char[] keyPassphrase = " MySecretPassword" .toCharArray();
816
808
KeyStore ks = KeyStore.getInstance(" PKCS12" );
817
809
ks.load(new FileInputStream(" /path/to/client_key.p12" ), keyPassphrase);
@@ -826,30 +818,29 @@ public class Example2
826
818
TrustManagerFactory tmf = TrustManagerFactory.getInstance(" SunX509" );
827
819
tmf.init(tks);
828
820
829
- SSLContext c = SSLContext.getInstance(" TLSv1.1 " );
821
+ SSLContext c = SSLContext.getInstance(" TLSv1.2 " );
830
822
c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
831
823
832
824
ConnectionFactory factory = new ConnectionFactory();
833
825
factory.setHost(" localhost" );
834
826
factory.setPort(5671);
835
827
factory.useSslProtocol(c);
828
+ factory.enableHostnameVerification();
836
829
837
830
Connection conn = factory.newConnection();
838
831
Channel channel = conn.createChannel();
839
832
840
833
channel.queueDeclare(" rabbitmq-java-test" , false, true, true, null);
841
834
channel.basicpublish("" , " rabbitmq-java-test" , null, " Hello, World" .getBytes());
842
835
843
-
844
836
GetResponse chResponse = channel.basicGet(" rabbitmq-java-test" , false);
845
- if(chResponse == null) {
837
+ if (chResponse == null) {
846
838
System.out.println(" No message retrieved" );
847
839
} else {
848
840
byte[] body = chResponse.getBody();
849
- System.out.println(" Recieved : " + new String(body));
841
+ System.out.println(" Received : " + new String(body));
850
842
}
851
843
852
-
853
844
channel.close();
854
845
conn.close();
855
846
}
@@ -861,6 +852,33 @@ public class Example2
861
852
a RabbitMQ node with a certificate that has not been imported
862
853
into the key store and watch the connection fail.
863
854
</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
+ < !-- Maven dependency to add for hostname verification on Java 6 -->
866
+ < dependency>
867
+ < groupId> org.apache.httpcomponents< /groupId>
868
+ < artifactId> httpclient< /artifactId>
869
+ < version> 4.5.6< /version>
870
+ < /dependency>
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
+
864
882
</doc : subsection >
865
883
866
884
<doc : subsection name =" tls-versions-java-client" >
0 commit comments