求具体MQ Server端/客户端的配置,和java代码实例 在windows server 2003 上 MQ7.0
------解决方案--------------------------------------------------------
- Java code
public class Connect2MQ { public static void main(String[] args) { Hashtable config = new Hashtable(); MQQueueManager qmgr = null; MQQueue queue = null; try { KeyStoreSpi ks; KeyStore keystore = KeyStore.getInstance("JKS"); keystore.load(Connect2MQ.class.getResourceAsStream("/key.jks"), "windows2000".toCharArray()); KeyStore truststore = KeyStore.getInstance("JKS"); truststore.load(Connect2MQ.class.getResourceAsStream("/trust.jks"), "windows2000".toCharArray()); ArrayList certs = new ArrayList(); for (Enumeration aliases = keystore.aliases(); aliases .hasMoreElements();) { String alias = (String) aliases.nextElement(); certs.add(keystore.getCertificate(alias)); if (alias.toLowerCase().startsWith("ibmwebspheremq")) { System.out.println("Alias :" + alias); Certificate cert = keystore.getCertificate(alias); // System.out.println(cert); } } Provider a; //Alg: SunX509, IBMX509, JSSE. KeyManagerFactory kmf = null; try { kmf = KeyManagerFactory.getInstance("IBMX509"); } catch (Exception e) { kmf = KeyManagerFactory.getInstance("SunX509"); } kmf.init(keystore, "windows2000".toCharArray()); KeyManager[] km = kmf.getKeyManagers(); TrustManagerFactory tmf = null; try { tmf = TrustManagerFactory.getInstance("IBMX509"); } catch (Exception e) { tmf = TrustManagerFactory.getInstance("SunX509"); } tmf.init(truststore); TrustManager[] tm = tmf.getTrustManagers(); final X509TrustManager[] tm2 = (X509TrustManager[]) Arrays.asList( tm).toArray(new X509TrustManager[tm.length]); X509TrustManager[] tm3 = new X509TrustManager[] { new X509TrustManager() { public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { for (int i = 0; i < arg0.length; i++) { System.out.println("Check Client Cert :" + arg0[i] + ", alg:" + arg1); } try { tm2[0].checkClientTrusted(arg0, arg1); } catch (CertificateException e) { System.err.println("Cert ex caught :" + e.toString()); } } public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { for (int i = 0; i < arg0.length; i++) { System.out.println("Check Server Cert :" + arg0[i] + ", alg:" + arg1); } try { tm2[0].checkServerTrusted(arg0, arg1); } catch (CertificateException e) { System.err.println("Cert ex caught :" + e.toString()); } } public X509Certificate[] getAcceptedIssuers() { return tm2[0].getAcceptedIssuers(); } } }; SSLContext ctx = SSLContext.getInstance("SSL"); ctx.init(km, tm3, null); String[] suites = ctx.getSocketFactory().getSupportedCipherSuites(); for (int i = 0; i < suites.length; i++) { System.out.println("Suite : " + suites[i]); } System.out.println("\n\n"); MQEnvironment.sslSocketFactory = ctx.getSocketFactory(); MQEnvironment.sslCipherSuite = "TLS_RSA_WITH_AES_128_CBC_SHA"; // MQEnvironment.sslCipherSuite = "TLS_RSA_WITH_AES_128_CBC_SHA"; MQEnvironment.sslPeerName = "CN=CAPA, O=WebSphere MQ, C=CN"; // MQEnvironment.sslPeerName = "CN=Daniel, O= Atreides, C=CN"; MQEnvironment.hostname = "localhost"; MQEnvironment.port = 1414; MQEnvironment.CCSID = 819; MQEnvironment.channel = "GREEN.SVRCONN"; MQEnvironment.userID = "Daniel"; for (Iterator iter = new TreeMap(System.getProperties()).entrySet() .iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); if (((String) entry.getKey()).startsWith("javax.")) { System.out.println("-D" + entry.getKey() + "=" + entry.getValue()); } } qmgr = new MQQueueManager("CAPA"); queue = qmgr.accessQueue("Q.REPLY", MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT); MQMessage msg = new MQMessage(); msg.writeString("This is a confidential message."); queue.put(msg); Thread.sleep(50000);