Last active
November 15, 2017 04:56
-
-
Save tienthanh2509/7006a6ec8d404bccd887a90bbd14dcc4 to your computer and use it in GitHub Desktop.
Test RabbitMQ with validating server certificates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.JsonObject; | |
import com.rabbitmq.client.Channel; | |
import com.rabbitmq.client.Connection; | |
import com.rabbitmq.client.ConnectionFactory; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.TrustManagerFactory; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
import java.net.URI; | |
import java.security.KeyStore; | |
import java.security.cert.CertificateFactory; | |
import java.security.cert.X509Certificate; | |
/** | |
* Snnd AMQP | |
*/ | |
public class RequestDeviceCert { | |
private final static String QUEUE_NAME = "-----------------------"; | |
public static void main(String[] argv) throws Exception { | |
ConnectionFactory factory = new ConnectionFactory(); | |
factory.setUri(new URI("amqps://admin:xxxxxx@localhost/%2F")); | |
factory.useSslProtocol(getSSLContextFromKeyStore()); | |
Connection connection = factory.newConnection(); | |
Channel channel = connection.createChannel(); | |
JsonObject message = new JsonObject(); | |
message.addProperty("name", "Hello World!"); | |
channel.basicPublish("", QUEUE_NAME, null, message.toString().getBytes("UTF-8")); | |
System.out.println(" [o] Sent '" + message + "'"); | |
channel.close(); | |
connection.close(); | |
} | |
/** | |
* Get SSLContext from CA bundle | |
* | |
* @return SSLContext | |
* @throws Exception FileNotFoundException... | |
*/ | |
private static SSLContext getSSLContext() throws Exception { | |
InputStream is = new FileInputStream("ca-bundle.crt"); | |
CertificateFactory cf = CertificateFactory.getInstance("X.509"); | |
X509Certificate caCert = (X509Certificate) cf.generateCertificate(is); | |
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | |
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); | |
ks.load(null); | |
ks.setCertificateEntry("caCert", caCert); | |
tmf.init(ks); | |
SSLContext c = SSLContext.getInstance("TLSv1.2"); | |
c.init(null, tmf.getTrustManagers(), null); | |
return c; | |
} | |
/** | |
* Get SSLContext from CA bundle | |
* | |
* @return SSLContext | |
* @throws KeyStoreException | |
* @throws IOException | |
* @throws NoSuchAlgorithmException | |
* @throws CertificateException | |
* @throws KeyManagementException | |
*/ | |
private static SSLContext getSSLContextFromKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException { | |
char[] trustPassphrase = "".toCharArray(); | |
KeyStore tks = KeyStore.getInstance("JKS"); | |
tks.load(new FileInputStream("ale-bundle.jks"), trustPassphrase); | |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); | |
tmf.init(tks); | |
SSLContext c = SSLContext.getInstance("TLSv1.1"); | |
c.init(null, tmf.getTrustManagers(), null); | |
return c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exception if validating falied