java - 使用 java 的 Websphere MQ SSL 连接

标签 java ssl ibm-mq mq

我在下面的代码连接到没有 SSL 的 IBM Websphere MQ,没有问题并且能够放置/获取消息。但是,当我尝试使用 SSL 连接 MQ 时,出现错误。

请注意,patch() 方法是禁用 SSL,它会被执行但不会禁用 SSL。

除此之外,您能否告诉我如何连接到启用了 SSL channel 的 MQ。

注意 - 我有证书文件,例如 - key.kdb、key.crl、key.rdb、key.sth 和其他一些 CA3、CA4.cer 文件。如何安装它们并在我的代码中使用它们?

import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.*;

import java.io.IOException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Hashtable;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class mq1 {

public static String    host = "localhost";
public static int       port = 1414;
public static String    strchannel = "MQ.CHL";
public static String    strqueuemanager = "MQMGR";
public static String    strqueue = "REQUEST.QUEUE";

@SuppressWarnings("deprecation")
public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException {
//call patch() to skip SSL 
patch();

int openOptions = CMQC.MQOO_BROWSE | CMQC.MQOO_INQUIRE | CMQC.MQOO_OUTPUT | CMQC.MQOO_INPUT_AS_Q_DEF ;

MQEnvironment.hostname = host;
MQEnvironment.port = port;
MQEnvironment.channel = strchannel;
MQEnvironment.properties.put(CMQC.TRANSPORT_PROPERTY,CMQC.TRANSPORT_MQSERIES);

MQQueueManager qMgr;
try {
qMgr = new MQQueueManager (strqueuemanager);
System.out.println(qMgr);
MQQueue destQueue = qMgr.accessQueue(strqueue, openOptions);
System.out.println("Queue size:" + destQueue.getCurrentDepth()); 
MQMessage hello_world = new MQMessage(); 
System.out.println("MQMessage message created");
hello_world.writeUTF("Sending Sample message"); 
MQPutMessageOptions pmo = new MQPutMessageOptions();
try {
destQueue.put(hello_world,pmo);
destQueue.get(hello_world);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

int len = hello_world.getDataLength();
System.out.println("Length : " + len);
System.out.println("GET: "+ hello_world.readString(len-1));
destQueue.close();
qMgr.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}


public static void patch() throws KeyManagementException, NoSuchAlgorithmException {

System.out.println("Calling SSL patch");

TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] certs, String authType) {
}

public void checkServerTrusted(X509Certificate[] certs, String authType) {
}

}
};

System.out.println("trustAllCerts = "+trustAllCerts);

SSLContext sc = SSLContext.getInstance("SSL");

System.out.println("sc before init = "+sc);

sc.init(
null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
return true;
}
};

System.out.println("sc after init= "+sc);
System.out.println("allHostsValid= "+allHostsValid);
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}

}

最佳答案

最简单的方法是根本不将其放入您的代码中,而是使用客户端 channel 定义表 (CCDT)。然后您可以在没有 SSL 的情况下使用它,然后更改 CCDT 以使用 SSL,而根本不需要更改您的代码。这被认为是客户端连接到 IBM MQ 的最佳实践。

这将意味着您停止使用 MQEnvironment 类来传递 channel 名称、主机名等内容,而是使用略有不同的带有两个参数的 MQQueueManager 构造函数,第一个是队列管理器名称,第二个是您的 CCDT 文件的名称。

您可以在以下页面的官方 IBM MQ 知识中心阅读有关如何将 CCDT 与 Java 结合使用的更多信息,其中还包含指向如何创建 CCDT 文件的链接:-

关于java - 使用 java 的 Websphere MQ SSL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33106261/

相关文章:

linux - 查找 IBM MQ 队列管理器的端口号

java - Java 8 Stream 是安全的返回类型吗?

c# - Java InputStream 和 OutputStream 是否有 C# 等效方法?

java - 如何估计运行促进在线交易的网络应用程序所需的互联网带宽?

css - Flying Saucer 和 CSS

c - 如何枚举 IBM WebSphere MQ 的队列管理器的本地队列?

java - 使用 JFreechart 创建任务概述

apache - OpenSSL : error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

apache - 如何仅在指定的 XAMPP 目录上启用 SSL

binding - IBM MQ : Multiple QMs in JMS bindings file