如何使用 smack-android 连接到禁用安全功能的 ejabberd?

Posted

技术标签:

【中文标题】如何使用 smack-android 连接到禁用安全功能的 ejabberd?【英文标题】:How to connect to security disabled ejabberd using smack-android? 【发布时间】:2015-02-05 03:25:48 【问题描述】:

我在使用 smack 4.1.0-beta2 连接到我的 ejabberd 2.1.11 服务器时遇到问题。我认为这与 SSL 证书有关,不想处理那些我只是在没有启动 TLS 的情况下运行 ejabberd 的东西。我知道这是可行的,因为我可以使用 psi 连接到 ejabberd,而不会警告我一些自签名证书。

无论如何,我正在使用以下 ConnectionConfiguration 的

XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword("username", "password");
configBuilder.setServiceName("myapp.ddns.net");
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
configBuilder.setCompressionEnabled(false);
AbstractXMPPConnection xmppConnection = new XMPPTCPConnection(configBuilder.build());

所以当我尝试 xmppConnection.connect() 时,我得到以下异常

02-05 01:01:42.965  17047-17047/net.ddns.myapp W/DNSUtil﹕ Exception while resovling SRV records for myapp.ddns.net. Consider adding '_xmpp-(server|client)._tcp' DNS SRV Records
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
            at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175)
            at libcore.io.IoBridge.sendto(IoBridge.java:463)
            at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
            at java.net.DatagramSocket.send(DatagramSocket.java:287)
            at de.measite.minidns.Client.query(Client.java:199)
            at de.measite.minidns.Client.query(Client.java:174)
            at de.measite.minidns.Client.query(Client.java:236)
            at de.measite.minidns.Client.query(Client.java:149)
            at org.jivesoftware.smack.util.dns.minidns.MiniDnsResolver.lookupSRVRecords(MiniDnsResolver.java:79)
            at org.jivesoftware.smack.util.DNSUtil.resolveDomain(DNSUtil.java:162)
            at org.jivesoftware.smack.util.DNSUtil.resolveXMPPDomain(DNSUtil.java:119)
            at org.jivesoftware.smack.AbstractXMPPConnection.populateHostAddresses(AbstractXMPPConnection.java:551)
            at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:506)
            at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:810)
            at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:355)
            at net.ddns.myapp.MainActivity.onCreate(MainActivity.java:61)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
            at android.app.ActivityThread.access$600(ActivityThread.java:127)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4507)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            at dalvik.system.NativeStart.main(Native Method)

还有

02-05 01:01:42.980  17047-17047/net.ddns.myapp E/MyApp﹕ Error while establishing XMPPConnection
    org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: myapp.ddns.net:5222 Exception: null
            at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:551)
            at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:810)
            at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:355)
            at net.ddns.myapp.MainActivity.onCreate(MainActivity.java:61)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
            at android.app.ActivityThread.access$600(ActivityThread.java:127)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4507)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

【参考方案1】:

对于android.os.NetworkOnMainThreadException,你需要

if (android.os.Build.VERSION.SDK_INT > 9) 
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

你也可以尝试设置主机值

   configBuilder.setHost("myapp.ddns.net");

【讨论】:

【参考方案2】:

这是我的错误,因为我不知道 StrictMode.ThreadPolicy,与 Smack 或 ejabberd 没有直接关系,它是基本的网络。

找到答案here

【讨论】:

以上是关于如何使用 smack-android 连接到禁用安全功能的 ejabberd?的主要内容,如果未能解决你的问题,请参考以下文章

如何禁用暂停/播放通过 Airplay 连接到 Apple TV 的 AVPlayer 的功能

如何在 ASP.NET Core 生产服务器上禁用“尝试重新连接到服务器”消息

如何在 ASP.NET Core 生产服务器上禁用“尝试重新连接到服务器”消息

vm安装好虚拟机后,如何通过ssh远程连接到远程服务器上去

AirPlay:连接到 Apple TV 时音量控制被禁用

禁用“允许远程连接到此服务器”后,我仍然可以从另一台机器连接