ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ Java / JMS » Nice sample I did - Runtime JMS SSL client connection

Post new topic  Reply to topic
 Nice sample I did - Runtime JMS SSL client connection « View previous topic :: View next topic » 
Author Message
HenriqueS
PostPosted: Tue Aug 25, 2009 4:04 pm    Post subject: Nice sample I did - Runtime JMS SSL client connection Reply with quote

Master

Joined: 22 Sep 2006
Posts: 235

Folks, below there is a nice sample I did. It is a merge of some samples I found out on the net...

It shows a JMS connection assembled in runtime with customized SSL settings (pointing to an external keystore), etc.

It is fully working here and has commented entries for who plays with IBM´s JSSE provider.

Code:

import com.ibm.mq.jms.*;

import java.io.FileInputStream;
import java.security.*;

import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import com.ibm.mq.jms.MQQueueConnectionFactory;

public class SSLTest {
   public static void main(String[] args) {
      System.out.println(System.getProperty("java.home"));
      
      String HOSTNAME = "mq-h.bc";
      String QMGRNAME = "QM.MQ_H";
      String CHANNEL = "APICLIENT.MQ_H";
      // CipherSuite SSL_RSA_WITH_3DES_EDE_CBC_SHA is the same as WebSphere MQ 'CipherSpec' TRIPLE_DES_SHA_US
      // see book 'WebSphereMQ - Using Java' for equivalency table
      String SSLCIPHERSUITE = "SSL_RSA_WITH_3DES_EDE_CBC_SHA";
      
      try {
         Class.forName("com.sun.net.ssl.internal.ssl.Provider");
         //Class.forName("com.ibm.jsse2.IBMJSSEProvider2");
         
         System.out.println("JSSE is installed correctly!");

         char[] KSPW = "password".toCharArray();

         // instantiate a KeyStore with type JKS
         KeyStore ks = KeyStore.getInstance("JKS");
         // load the contents of the KeyStore
         ks.load(new FileInputStream("C:\\MQUtils\\SSL\\mqm.jks"), KSPW);
         System.out.println("Number of keys on JKS: "
               + Integer.toString(ks.size()));

         // Create a keystore object for the truststore
         KeyStore trustStore = KeyStore.getInstance("JKS");
         // Open our file and read the truststore (no password)
         trustStore.load(new FileInputStream("C:\\MQUtils\\SSL\\mqm.jks"), null);
         
         // Create a default trust and key manager
         TrustManagerFactory trustManagerFactory =
           TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
         KeyManagerFactory keyManagerFactory =
           KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

         // Initialise the managers
         trustManagerFactory.init(trustStore);
         keyManagerFactory.init(ks,KSPW);
         
         // Get an SSL context.
         // Note: not all providers support all CipherSuites. But the
         // "SSL_RSA_WITH_3DES_EDE_CBC_SHA" CipherSuite is supported on both SunJSSE
         // and IBMJSSE2 providers

         // Accessing available algorithm/protocol in the SunJSSE provider
         // see http://java.sun.com/javase/6/docs/technotes/guides/security/SunProviders.html
         SSLContext sslContext = SSLContext.getInstance("SSLv3");

         // Acessing available algorithm/protocol in the IBMJSSE2 provider
         // see http://www.ibm.com/developerworks/java/jdk/security/142/secguides/jsse2docs/JSSE2RefGuide.html
         // SSLContext sslContext = SSLContext.getInstance("SSL_TLS");
          System.out.println("SSLContext provider: " +
                            sslContext.getProvider().toString());

         // Initialise our SSL context from the key/trust managers
         sslContext.init(keyManagerFactory.getKeyManagers(),
                         trustManagerFactory.getTrustManagers(), null);

         // Get an SSLSocketFactory to pass to WMQ
         SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

         // Create default MQ connection factory
         MQQueueConnectionFactory factory = new MQQueueConnectionFactory();

         //Customize the factory
         factory.setSSLSocketFactory(sslSocketFactory);
         factory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
         factory.setQueueManager(QMGRNAME);
         factory.setHostName(HOSTNAME);
         factory.setChannel(CHANNEL);
         factory.setPort(1414);
         factory.setSSLFipsRequired(false);
         factory.setSSLCipherSuite(SSLCIPHERSUITE);
         
         QueueConnection connection = null;
         connection = factory.createQueueConnection("",""); //empty user, pass to avoid
         // MQJMS2013 messages
         connection.start();
         System.out.println("JMS SSL client connection started!");
         connection.close();
         
      } catch (JMSException ex) {
         ex.printStackTrace();
      } catch (Exception ex){
         ex.printStackTrace();
      }
   }
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ Java / JMS » Nice sample I did - Runtime JMS SSL client connection
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.