我有一个非常简单的 Java 应用程序,它使用名为“thesis.dimfccs.sensors”的自定义帐户向 openfire 服务器发送消息,该帐户正在向另一个名为“thesis.dimfccs”的自定义帐户发送消息。
我使用的是 openfire 3.9.1,我的 Java 应用程序能够连接到服务器,但无法登录。当我尝试登录时,出现以下错误:
Initializing connection to server localhost, port 5222, service gmail.com
Connected: true
Logging in: thesis.dimfccs.sensors, thesis.dimfccs.sensors.pwd
Exception in thread "main" No response from the server.:
at org.jivesoftware.smack.NonSASLAuthentication.authenticate(NonSASLAuthentication.java:73)
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:357)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:243)
at org.jivesoftware.smack.Connection.login(Connection.java:366)
at javacodegeeks.XmppManager.performLogin(XmppManager.java:76)
at javacodegeeks.XmppLocalTest.main(XmppLocalTest.java:44)
我知道测试帐户的密码和名称是正确的,因为我可以使用它们进入 Spark IM 客户端。
但是,真正让我大吃一惊的是,如果我使用“admin”帐户执行完全相同的操作,一切都会正常!
我已经仔细检查了 thesis.dimfccs.sensors 密码并为其授予了管理员访问权限,但这仍然不够。
代码如下:
Main.java:
public class XmppLocalTest {
//Total number of messages sent during one minute.
public static final int MESSAGES_NUMBER = 120;
//Each message will have a random number between 0 and MAX_RANDOM.
public static final int MAX_RANDOM = 100;
private static final String SENDER_USERNAME = "thesis.dimfccs.sensors";
private static final String SENDER_PASSWORD = "thesis.dimfccs.sensors.pwd";
public static void main(String[] args) throws Exception {
String username = SENDER_USERNAME;
String password = SENDER_PASSWORD;
XmppManager xmppManager = new XmppManager("localhost", 5222,
"gmail.com");
xmppManager.init();
xmppManager.performLogin(username, password);
System.out.println("Login performed with success, setting status.");
xmppManager.setStatus(true, "Let the pain begin!");
System.out.println("Setting up receivers");
xmppManager.createEntry("thesis.dimfccs", "thesis.dimfccs");
//go message!
xmppManager.sendMessage("Hello world!", "thesis.dimfccs@gmail.com");
xmppManager.destroy();
}
}
XmppManager.java:
package javacodegeeks;
/**
* Main class with all the logic of the connection. Followed from the tutorials:
* http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html
* */
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
public class XmppManager {
private static final int packetReplyTimeout = 500; // millis
private String server;
private int port;
private String service;
private ConnectionConfiguration config;
private XMPPConnection connection;
private ChatManager chatManager;
private MessageListener messageListener;
public XmppManager(String aServer, int aPort, String aService) {
server = aServer;
port = aPort;
service = aService;
}
public XmppManager(String server, int port) {
this(server, port, null);
}
public void init() throws XMPPException {
System.out.println(String.format("Initializing connection to server " + server + ", port " + port
+ ", service " + service));
SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);
if(service != null)
config = new ConnectionConfiguration(server, port, service);
else
config = new ConnectionConfiguration(server, port);
// config.setSASLAuthenticationEnabled(false);
// config.setSecurityMode(SecurityMode.disabled);
// SASLAuthentication.supportSASLMechanism("PLAIN", 0);
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
connection = new XMPPConnection(config);
connection.connect();
System.out.println("Connected: " + connection.isConnected());
chatManager = connection.getChatManager();
messageListener = new MyMessageListener();
}
public void performLogin(String username, String password) throws XMPPException {
if (connection!=null && connection.isConnected()) {
System.out.println("Logging in: " + username + ", " + password);
connection.login(username, password);
}
}
public void setStatus(boolean available, String status) {
Presence.Type type = available? Type.available: Type.unavailable;
Presence presence = new Presence(type);
presence.setStatus(status);
connection.sendPacket(presence);
}
public void destroy() {
if (connection!=null && connection.isConnected()) {
connection.disconnect();
}
}
public void sendMessage(String message, String buddyJID) throws XMPPException {
System.out.println(String.format("Sending message " + message + " to user " + buddyJID));
Chat chat = chatManager.createChat(buddyJID, messageListener);
chat.sendMessage(message);
}
public void createEntry(String user, String name) throws Exception {
System.out.println(String.format("Creating entry for buddy " + user + " with name " + name));
Roster roster = connection.getRoster();
roster.createEntry(user, name, null);
}
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 - 无法通过XMPP登录openfire帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23145243/