1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| package com.holelin.sundry;
import java.io.FileInputStream; import java.io.IOException; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException;
import java.util.concurrent.TimeoutException; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory;
import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope;
public class VerifyClient {
private static String HOST_IP = "192.168.40.10"; private static int HOST_PORT = 5671;
private static String USERNAME = "test"; private static String PASSWORD = "test";
private static String QUEUE_NAME = "hello";
public VerifyClient() throws KeyManagementException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, KeyStoreException, CertificateException, TimeoutException {
char[] keyPassphrase = "rabbit".toCharArray(); KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(new FileInputStream("/path/to/clientcert.p12"), keyPassphrase);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, "rabbit".toCharArray());
char[] trustPassphrase = "rabbit".toCharArray(); KeyStore tks = KeyStore.getInstance("JKS"); tks.load(new FileInputStream("/path/to/keystore"), trustPassphrase);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); tmf.init(tks);
SSLContext c = SSLContext.getInstance("TLSv1.2"); c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST_IP); factory.setPort(HOST_PORT); factory.setUsername(USERNAME); factory.setPassword(PASSWORD);
factory.useSslProtocol(c);
Connection connection = factory.newConnection(); Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, true, true, null); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'");
Channel second = connection.createChannel(); Consumer consumer = new DefaultConsumer(second) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); } }; second.basicConsume(QUEUE_NAME, true, consumer);
channel.close(); second.close(); connection.close(); }
}
|