import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.jid.Jid;
public static final String HOST = "192.168.1.114";
public static final int PORT = 5222;
public static final String SERVICE = "192.168.1.114"; // i dont know if its right..
public static final String USERNAME = "test";
public static final String PASSWORD = "test";
这是代码:
public void connect()
{
final ProgressDialog dialog = ProgressDialog.show(this, "Connecting...", "Please wait...", false);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Create a connection
// ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
Log.v("checking", "011");
XMPPTCPConnectionConfiguration.Builder connConfig = XMPPTCPConnectionConfiguration.builder();
connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); // enabled gives me SSL certificate authorization errors...
connConfig.setUsernameAndPassword(USERNAME, PASSWORD);
try {
connConfig.setServiceName(JidCreate.domainBareFrom(SERVICE));
} catch (XmppStringprepException e) {
e.printStackTrace();
Log.v("checking", e.toString() + "1");
}
connConfig.setHost(HOST);
connConfig.setPort(PORT);
connConfig.setDebuggerEnabled(true);
AbstractXMPPConnection connection = new XMPPTCPConnection(connConfig.build());
try {
connection.connect();
Log.v("XMPPChatDemoActivity", "[SettingsDialog] Connected to "+connection.getHost());
} catch (XMPPException ex) {
Log.v("XMPPChatDemoActivity", "[SettingsDialog] Failed to connect to "+ connection.getHost());
Log.v("XMPPChatDemoActivity", ex.toString());
setConnection(null);
} catch (SmackException e) {
e.printStackTrace();
Log.v("XMPPChatDemoActivity", e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.v("XMPPChatDemoActivity", e.toString());
} catch (InterruptedException e) {
e.printStackTrace();
Log.v("XMPPChatDemoActivity", e.toString());
}
try {
connection.login(USERNAME, PASSWORD);
Log.v("XMPPChatDemoActivity", "Logged in as" + connection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendStanza(presence);
setConnection((XMPPTCPConnection) connection);
Roster roster = Roster.getInstanceFor(connection);
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
Log.v("XMPPChatDemoActivity", "--------------------------------------");
Log.v("XMPPChatDemoActivity", "RosterEntry " + entry);
Log.v("XMPPChatDemoActivity", "User: " + entry.getUser());
Log.v("XMPPChatDemoActivity", "Name: " + entry.getName());
Log.v("XMPPChatDemoActivity", "Status: " + entry.getStatus());
Log.v("XMPPChatDemoActivity", "Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry.getUser());
Log.v("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
Log.v("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());
Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available)
Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
Log.v("XMPPChatDemoActivity", "Presence : " + entryPresence);
}
} catch (XMPPException ex) {
Log.v("XMPPChatDemoActivity", "Failed to log in as "+ USERNAME + " or " + connection.getHost());
Log.v("XMPPChatDemoActivity", ex.toString());
setConnection(null);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.dismiss();
}
});
t.start();
dialog.show();
}
这是logcat的一个片段:
08-03 06:56:26.812 8824-8824/com.example.sumit.testapp V/ActivityThread﹕ updateVisibility : ActivityRecord{15e86073 token=android.os.BinderProxy@26aa149d {com.example.sumit.testapp/com.example.sumit.testapp.MainActivity}} show : true
08-03 06:56:26.902 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='192.168.1.114' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='test@192.168.1.114' xml:lang='en'>
08-03 06:56:26.922 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="bahadurxxx" id="e61566be" xml:lang="en" version="1.0">
08-03 06:56:26.922 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression><auth xmlns="http://jabber.org/features/iq-auth"/><register xmlns="http://jabber.org/features/iq-register"/></stream:features>
08-03 06:56:26.932 8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ [SettingsDialog] Connected to 192.168.1.114
08-03 06:56:26.932 8824-8824/com.example.sumit.testapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@26aa149d time:22662680
08-03 06:56:26.932 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'>=</auth>
08-03 06:56:26.932 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cmVhbG09ImJhaGFkdXJ4eHgiLG5vbmNlPSJIVEVaUU5kRWpJM0cxRUtoWHFVYVM4akhyZkpNdTMyaTFPYVBETkJyIixxb3A9ImF1dGgiLGNoYXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNz</challenge>
08-03 06:56:26.932 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dXNlcm5hbWU9InRlc3QiLHJlYWxtPSIxOTIuMTY4LjEuMTE0Iixub25jZT0iSFRFWlFOZEVqSTNHMUVLaFhxVWFTOGpIcmZKTXUzMmkxT2FQRE5CciIsY25vbmNlPSJVUDR5c3dpOVlMMDY3RTNvMTZrNTVwT1FKdDEzMWVSWSIsbmM9MDAwMDAwMDEscW9wPWF1dGgsZGlnZXN0LXVyaT0ieG1wcC8xOTIuMTY4LjEuMTE0IixyZXNwb25zZT04NzllZmVjZGJhMGUwMDc1N2FmZWYxZmQxOWVhYTI0MyxjaGFyc2V0PXV0Zi04</response>
08-03 06:56:26.942 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
08-03 06:56:26.942 8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ Failed to log in as test or 192.168.1.114
08-03 06:56:26.942 8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ org.jivesoftware.smack.sasl.SASLErrorException: SASLError using DIGEST-MD5: not-authorized
08-03 06:56:30.582 8824-8824/com.example.sumit.testapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@26aa149d time:22666335
08-03 06:56:30.622 8824-8824/com.example.sumit.testapp E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-03 06:56:30.622 8824-8824/com.example.sumit.testapp E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-03 06:59:27.082 8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <iq type="get" id="553-31" from="bahadurxxx" to="bahadurxxx/e61566be"><ping xmlns="urn:xmpp:ping"/></iq>
08-03 06:59:27.092 8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <iq to='bahadurxxx' id='553-31' type='result'></iq>
您的客户端正在尝试使用领域“192.168.1.114”进行身份验证,而服务器只提供领域“bahadurxxx”。那是行不通的。您应该将service
设置为“bahadurxxx”,或者将服务器的主机名更改为“192.168.1.114”。
每个XMPP地址都有一个域,就像user@example.com
中的example.com
,就像电子邮件一样。这是您必须进行身份验证的域,也是您将通过XMPP为自己的服务器使用的地址。但是,XMPP服务器不一定需要在example.com
上运行,可能是服务器实际上在XMPP.example.com
上。SRV记录是指示example.com
驻留在xmpp.example.com
上的一种方法。
在Smack中,.setServiceName()
设置要使用的XMPP域(毫不奇怪,它已被重命名为.setXMPPDomain()
)。.sethost()
可以用作指示您要使用xmpp.example.com
另一种方式。
我得到以下错误,而登录成功连接后: 我在OpenFire服务器上打开用户,用户名8b6c219c36578e893c5e6d1d7b5af8a@myjob.uz。如果我使用asmack-2010.05.07所有工作正常,但当使用asmack-android-8-4.0.6我得到以下错误。\任何人知道如何修复它? 以下是连接方法:
我从关于如何使用jwt进行授权的演练中获取了第2节,这样我就可以从我的客户端获得一个访问令牌,并授权他们使用该api。然而,我似乎不能让这个工作。我不断收到邮递员发来的401消息,还附有一句: 教程链接:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ 访问
我正在尝试创建一个基本连接,并登录到我安装的Openfire服务器。我通过Openfire web管理界面创建的用户数据库中有以下用户: 我可以很好地连接到服务器,因为连接在我的sout中返回true。问题是,当它尝试登录时,会出现以下错误: 我有以下代码: 如果有人能纠正我做错的事情,我会非常感激。 我也尝试了用户名作为电子邮件将是例如
例如,将username设置为简单字符串username,也可以使用username@jabber.org(或@jabber.de) 我的上的、、。 还要配置SASL机制: null 所有这些尝试,我也都评说得一干二净。 我搜索了很多,发现这是旧的Smack库上的一个bug,但应该在新的Smack版本4.0以上的版本中修复。所以我不得不假设这个错误不是Smack中的bug。 我认为这个错误可能是因
当构建一个不使用HTTP基本身份验证(但是像api-key这样的其他东西)并且客户机提供无效凭据的REST API时,您应该返回什么HTTP状态代码?401未经授权还是403禁止? IANA HTTP状态代码注册表将RFC7235第3.1节列为“401未经授权”的责任人,其中指出: HTTP 401响应必须始终包含一个WWW-Authenticate标头,该标头指示客户端如何进行身份验证。HTTP
我的前端基于Angular 4,后端基于Spring Security的Spring Boot。我正在一个WAR文件中部署所有内容。 我在/src/main/resources中创建了一个静态/登陆文件夹,然后我将生成角度文件的网页包放在该文件夹中。 Angular负责登录过程,因此我在Spring Security中创建了以下规则: 不幸的是,当我尝试使用网络浏览器访问 /login页面进行登录