Connecting to a Websphere MQ in Java with SSL/Keystore

前端 未结 4 1973
眼角桃花
眼角桃花 2020-11-30 08:14

I\'d like to connect to a Websphere 6.0 MQ via Java. I have already working code for a \"normal\" queue, but now I need to access a new queue which is SSL encrypted (keysto

4条回答
  •  失恋的感觉
    2020-11-30 08:47

    Using SSL from the Oracle JVM (JSSE)

    See also "What TLS cipherspecs/ciphersuites are supported when connecting from Oracle Java (non-IBM JRE) to MQ queue manager?"

    In MQ Client version 8.0.0.2 there is a patch is included to use the TLS with Oracle JVM, this works with lanes answer above

    The get this to work you will need the latest MQ Client that contains IV66840: WMQ V7 JAVA/JMS: ADD SUPPORT FOR SELECTED TLS CIPHERSPECS WHEN RUNNING IN NON-IBM JAVA RUNTIME ENVIRONMENT
    http://www-01.ibm.com/support/docview.wss?uid=swg1IV66840
    (download)

    Depending on your location you may also need to install Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 8 (download)

    To use this you have to configured by using the JVM argument:

      -Dcom.ibm.mq.cfg.useIBMCipherMappings=false
    

    Note that the default security implementation behaviour differs between Oracle and IBM JVMs :

    The Oracle JSSE Reference guide says:

    If the KeyManager[] parameter is null, then an empty KeyManager will be defined for this context.

    The IBM JSSE Reference guide says:

    If the KeyManager[] paramater is null, the installed security providers will be searched for the highest-priority implementation of the KeyManagerFactory, from which an appropriate KeyManager will be obtained.

    Which means that you have to setup your own ssl context

    SSLContext  sslcontext = SSLContext.getInstance("TLS");
    String  keyStore = System.getProperty("javax.net.ssl.keyStore");
    String  keyStoreType = System.getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType());
    String  keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword","");
    KeyManager[]    kms = null;
    if (keyStore != null)
    {
        KeyManagerFactory   kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        KeyStore    ks = KeyStore.getInstance(keyStoreType);
        if (keyStore != null && !keyStore.equals("NONE")) {
            fs = new FileInputStream(keyStore);
        ks.load(fs, keyStorePassword.toCharArray());
        if (fs != null)
            fs.close();
        char[]  password = null;
        if (keyStorePassword.length() > 0)
            password = keyStorePassword.toCharArray();
        kmf.init(ks,password);
        kms = kmf.getKeyManagers();
    }
    sslcontext.init(kms,null,null);
    

    And then supply that to the MQ JMS client:

        JmsConnectionFactory cf = ...                                                                     
    
        MQConnectionFactory mqcf = (MQConnectionFactory) cf;              
        mqcf.setSSLSocketFactory(sslcontext.getSocketFactory());  
    

    If using a application server this might be handled by your application server.

提交回复
热议问题