Java Can';t在Smack中连接到XMPP(Openfire)服务器。SASLAuthentication异常
将Smack库连接到Openfire服务器时出现以下错误。我在stack overflow中搜索,但无法解决我的问题 例外情况如下Java Can';t在Smack中连接到XMPP(Openfire)服务器。SASLAuthentication异常,java,xmpp,openfire,smack,sasl,Java,Xmpp,Openfire,Smack,Sasl,将Smack库连接到Openfire服务器时出现以下错误。我在stack overflow中搜索,但无法解决我的问题 例外情况如下 Exception in thread "main" org.jivesoftware.smack.SmackException$NoResponseException at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:352) at org.jives
Exception in thread "main" org.jivesoftware.smack.SmackException$NoResponseException
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:352)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.login(XMPPTCPConnection.java:244)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:442)
at JabberSmackAPI.login(JabberSmackAPI.java:64)
at JabberSmackAPI.main(JabberSmackAPI.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code 1
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.util.StringUtils;
import sun.rmi.runtime.Log;
import javax.net.ssl.*;
import javax.security.sasl.SaslException;
public class JabberSmackAPI implements MessageListener {
private XMPPConnection connection;
private final String mHost = "192.168.2.250";//if fails use "192.168.2.250" // server IP address or the
// host
public void login(String userName, String password) throws XMPPException, IOException, SmackException, NoSuchAlgorithmException, KeyManagementException {
String service = StringUtils.parseServer(userName);
final String user_name = StringUtils.parseName(userName);
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(
X509Certificate[] certs,
String authType) {
}
public void checkServerTrusted(
X509Certificate[] certs,
String authType) {
}
}
};
HostnameVerifier verifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
ConnectionConfiguration config = new ConnectionConfiguration(mHost, 5222);
SSLContext context= SSLContext.getInstance("TLS");
context.init(null, trustAllCerts, new java.security.SecureRandom());
config.setCustomSSLContext(context);
config.setHostnameVerifier(verifier);
config.setSendPresence(true);
config.setDebuggerEnabled(false);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
connection = new XMPPTCPConnection(config);
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.connect();
System.out.println("JabberSmackAPI.login");
connection.login(user_name, password);
}
public void sendMessage(String msg, String message) throws XMPPException, SmackException.NotConnectedException {
ChatManager chatManager = ChatManager.getInstanceFor(connection);
Chat chat = chatManager.createChat("ali", new MyMessageListener());
chat.sendMessage(message);
}
public void displayBuddyList() {
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for (RosterEntry r : entries) {
System.out.println(r.getUser());
}
}
public void disconnect() throws SmackException.NotConnectedException {
connection.disconnect();
}
public void processMessage(Chat chat, Message message) {
System.out.println("Received something: " + message.getBody());
if (message.getType() == Message.Type.chat)
System.out.println(chat.getParticipant() + " says: "
+ message.getBody());
}
public static void main(String args[]) throws XMPPException, IOException, SmackException, KeyManagementException, NoSuchAlgorithmException {
// declare variables
JabberSmackAPI c = new JabberSmackAPI();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
// turn on the enhanced debugger
// XMPPConnection.DEBUG_ENABLED = true;
// Enter your login information here
System.out.println("-----");
System.out.println("Login information:");
System.out.print("username: ");
String login_username = br.readLine();
System.out.print("password: ");
String login_pass = br.readLine();
c.login(login_username, login_pass);
c.displayBuddyList();
System.out.println("-----");
System.out
.println("Who do you want to talk to? - Type contacts full email address:");
String talkTo = br.readLine();
System.out.println("-----");
System.out.println("All messages will be sent to " + talkTo);
System.out.println("Enter your message in the console:");
System.out.println("-----\n");
while (!(msg = br.readLine()).equals("bye")) {
c.sendMessage(msg, talkTo);
}
c.disconnect();
System.exit(0);
}
private class MyMessageListener implements MessageListener {
@Override
public void processMessage(Chat chat, Message message) {
String from = message.getFrom();
String body = message.getBody();
System.out.println(String.format("Received message " + body + " from " + from));
}
}
}
我的代码如下所示
Exception in thread "main" org.jivesoftware.smack.SmackException$NoResponseException
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:352)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.login(XMPPTCPConnection.java:244)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:442)
at JabberSmackAPI.login(JabberSmackAPI.java:64)
at JabberSmackAPI.main(JabberSmackAPI.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code 1
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.util.StringUtils;
import sun.rmi.runtime.Log;
import javax.net.ssl.*;
import javax.security.sasl.SaslException;
public class JabberSmackAPI implements MessageListener {
private XMPPConnection connection;
private final String mHost = "192.168.2.250";//if fails use "192.168.2.250" // server IP address or the
// host
public void login(String userName, String password) throws XMPPException, IOException, SmackException, NoSuchAlgorithmException, KeyManagementException {
String service = StringUtils.parseServer(userName);
final String user_name = StringUtils.parseName(userName);
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(
X509Certificate[] certs,
String authType) {
}
public void checkServerTrusted(
X509Certificate[] certs,
String authType) {
}
}
};
HostnameVerifier verifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
ConnectionConfiguration config = new ConnectionConfiguration(mHost, 5222);
SSLContext context= SSLContext.getInstance("TLS");
context.init(null, trustAllCerts, new java.security.SecureRandom());
config.setCustomSSLContext(context);
config.setHostnameVerifier(verifier);
config.setSendPresence(true);
config.setDebuggerEnabled(false);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
connection = new XMPPTCPConnection(config);
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.connect();
System.out.println("JabberSmackAPI.login");
connection.login(user_name, password);
}
public void sendMessage(String msg, String message) throws XMPPException, SmackException.NotConnectedException {
ChatManager chatManager = ChatManager.getInstanceFor(connection);
Chat chat = chatManager.createChat("ali", new MyMessageListener());
chat.sendMessage(message);
}
public void displayBuddyList() {
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for (RosterEntry r : entries) {
System.out.println(r.getUser());
}
}
public void disconnect() throws SmackException.NotConnectedException {
connection.disconnect();
}
public void processMessage(Chat chat, Message message) {
System.out.println("Received something: " + message.getBody());
if (message.getType() == Message.Type.chat)
System.out.println(chat.getParticipant() + " says: "
+ message.getBody());
}
public static void main(String args[]) throws XMPPException, IOException, SmackException, KeyManagementException, NoSuchAlgorithmException {
// declare variables
JabberSmackAPI c = new JabberSmackAPI();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
// turn on the enhanced debugger
// XMPPConnection.DEBUG_ENABLED = true;
// Enter your login information here
System.out.println("-----");
System.out.println("Login information:");
System.out.print("username: ");
String login_username = br.readLine();
System.out.print("password: ");
String login_pass = br.readLine();
c.login(login_username, login_pass);
c.displayBuddyList();
System.out.println("-----");
System.out
.println("Who do you want to talk to? - Type contacts full email address:");
String talkTo = br.readLine();
System.out.println("-----");
System.out.println("All messages will be sent to " + talkTo);
System.out.println("Enter your message in the console:");
System.out.println("-----\n");
while (!(msg = br.readLine()).equals("bye")) {
c.sendMessage(msg, talkTo);
}
c.disconnect();
System.exit(0);
}
private class MyMessageListener implements MessageListener {
@Override
public void processMessage(Chat chat, Message message) {
String from = message.getFrom();
String body = message.getBody();
System.out.println(String.format("Received message " + body + " from " + from));
}
}
}
导入java.security.KeyManagementException;
导入java.security.NoSuchAlgorithmException;
导入java.security.cert.x509证书;
导入java.util.*;
导入java.io.*;
导入org.jivesoftware.smack.*;
导入org.jivesoftware.smack.packet.Message;
导入org.jivesoftware.smack.packet.packet;
导入org.jivesoftware.smack.tcp.XMPPTCPConnection;
导入org.jivesoftware.smack.util.StringUtils;
导入sun.rmi.runtime.Log;
导入javax.net.ssl.*;
导入javax.security.sasl.SaslException;
公共类JabberSmackAPI实现MessageListener{
专用XMPPConnection连接;
私有最终字符串mHost=“192.168.2.250”;//如果失败,请使用“192.168.2.250”//服务器IP地址或
//主人
public void登录(字符串用户名、字符串密码)抛出xmppeexception、IOException、SmackException、nosuchagorithmexception、KeyManagementException{
String service=StringUtils.parseServer(用户名);
最终字符串user\u name=StringUtils.parseName(用户名);
TrustManager[]trustAllCerts=新的TrustManager[]{
新X509TrustManager(){
public java.security.cert.X509Certificate[]getAcceptedIssuers(){
返回新的X509证书[0];
}
公共无效checkClientTrusted(
X509证书[]证书,
字符串(authType){
}
公共无效检查服务器受信任(
X509证书[]证书,
字符串(authType){
}
}
};
HostnameVerifier验证器=新的HostnameVerifier(){
@凌驾
公共布尔验证(字符串主机名、SSLSession会话){
返回true;
}
};
ConnectionConfiguration配置=新的ConnectionConfiguration(mHost,5222);
SSLContext context=SSLContext.getInstance(“TLS”);
init(null,trustAllCerts,new java.security.SecureRandom());
config.setCustomSSLContext(上下文);
config.setHostnameVerifier(验证器);
config.setSendPresence(true);
config.setDebuggerEnabled(false);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
连接=新的XMPPTCPConnection(配置);
SASLAuthentication.支持SaslMechanism(“平原”,0);
connection.connect();
System.out.println(“JabberSmackAPI.login”);
连接。登录(用户名、密码);
}
public void sendMessage(String msg,String message)抛出XMPPException、smakexception.NotConnectedException{
ChatManager ChatManager=ChatManager.getInstanceFor(连接);
Chat Chat=chatManager.createChat(“ali”,新的MyMessageListener());
发送消息(message);
}
public void displaybudylist(){
花名册=connection.get花名册();
集合条目=花名册.getEntries();
System.out.println(“\n\n”+entries.size()+“buddy:”);
对于(名册r:条目){
System.out.println(r.getUser());
}
}
public void disconnect()引发SmackException.NotConnectedException{
连接断开();
}
public void processMessage(聊天室、消息消息){
System.out.println(“收到某物:+message.getBody());
if(message.getType()==message.Type.chat)
System.out.println(chat.getParticipant()+“表示:
+message.getBody());
}
publicstaticvoidmain(字符串args[])抛出xmppeexception、IOException、SmackException、KeyManagementException、nosuchalgorithexception{
//声明变量
JabberSmackAPI c=新的JabberSmackAPI();
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
串味精;
//打开增强的调试器
//XMPPConnection.DEBUG_ENABLED=true;
//在此处输入您的登录信息
System.out.println(“----”);
System.out.println(“登录信息:”);
系统输出打印(“用户名:”);
字符串login_username=br.readLine();
系统输出打印(“密码:”);
字符串login_pass=br.readLine();
c、 登录(登录用户名、登录密码);
c、 displaybudylist();
System.out.println(“----”);
系统输出
.println(“您想和谁通话?-键入联系人完整电子邮件地址:”);
String talkTo=br.readLine();
System.out.println(“----”);
System.out.println(“所有消息将发送到”+talkTo);
System.out.println(“在控制台中输入消息:”);
System.out.println(“----\n”);
而(!(msg=br.readLine()).equals(“再见”)){
c、 发送消息(msg、talkTo);
}
c、 断开连接();
系统出口(0);
}
私有类MyMessageListener实现MessageListener{
@凌驾
public void processMessage(聊天室、消息消息){
String from=message.getFrom();
字符串body=message.getBody();
System.out.println(String.format(“接收到的消息”+body+“来自”+from));
}
}
}
欢迎提供任何帮助您有这一行:
connection.login(user_name, password);
它抛出了一个:
公共静态类SmackException.NoResponseException扩展
SmackException
当IQ请求没有响应时始终引发异常
在所用连接实例的数据包回复超时内
这本质上意味着您没有得到响应,因此您必须检查是否:
- 接收方存在并且能够获取和解析消息
- 接收器能够发送响应
- 发送者能够发送消息
- 连接设置正确且最新