gpt4 book ai didi

android - (a) 击打 : TLS socket is closed when trying to login with XMPP

转载 作者:行者123 更新时间:2023-11-30 03:48:07 25 4
gpt4 key购买 nike

我正在尝试使用 asmack(Android Gingerbread)连接到 google talk。我已经设置了 connectionConfig.setTruststoreType("BKS") - 所以我解决了证书问题。现在失败发生在 XMPPConnection.proceedTLSReceived() 函数中,该函数在服务器为“starttls”请求发送“proceed”之后调用。

这个函数应该初始化 TSL 套接字。它失败并出现异常“java.net.SocketException:套接字已关闭”。

// Verify certificate presented by the server
context.init(kms,
new javax.net.ssl.TrustManager[]{new ServerTrustManager(getServiceName(), config)},
//new javax.net.ssl.TrustManager[]{new OpenTrustManager()},
new java.security.SecureRandom());
Socket plain = socket;
// Secure the plain connection
socket = context.getSocketFactory().createSocket(plain, plain.getInetAddress().getHostName(), plain.getPort(), false);
socket.setSoTimeout(0); ////// THIS LINE THROWS THE EXCEPTION

有什么线索吗?

套接字描述为:Socket 上的 SSL 套接字[addr=talk.google.com/173.194.70.125,port=5222,localport=34840]

它在模拟器和我的 Galaxy S I9000 上失败。

最佳答案

事实证明,SSL 连接存在超时问题。我的应用程序花费了太多时间来生成 SSL 套接字(通过普通套接字),以至于对方直接放弃了...这,或者存在关键问题(但我没有收到任何身份验证错误)。

我通过以下方式解决了这个问题:

  1. 升级到 API 级别 15 - 使用 AndroidCSStore,而不是 BKS(我用于 API 10)。
  2. 使用 getAuthToken 获取密码- 它实际上是一个 token ,但稍后在连接时用作密码。
  3. 我没有空闲时间来真正弄清问题的根源。它现在可以工作了,这才是最重要的。

我在下面发布了我的代码。 SASLXOAuth2 机制完成。但是 Main 中的代码是有条不紊的,并不完整。我从该站点上的其他答案中撕下了一些代码,我为我的剽窃提前道歉。

Main.java - 启动 asmack

// Init asmack, and register new mechanisms.
asmack = SmackAndroid.init(ctx);

SASLAuthentication.registerSASLMechanism("X-OAUTH2", SASLXOAuth2Mechanism.class);
SASLAuthentication.supportSASLMechanism("X-OAUTH2", 0);

ConnectionConfiguration connectionConfig =
new ConnectionConfiguration (getHost(), getPort(), getService());
connectionConfig.setSASLAuthenticationEnabled(true);
connectionConfig.setSecurityMode(true);
connectionConfig.setRosterLoadedAtLogin(true);
connectionConfig.setReconnectionAllowed(true);
connectionConfig.setSendPresence(false);
//connectionConfig.setCompressionEnabled(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
connectionConfig.setTruststoreType("AndroidCAStore");
connectionConfig.setTruststorePassword(null);
connectionConfig.setTruststorePath(null);
} /*else {
connectionConfig.setTruststoreType("BKS");
String path = System.getProperty("javax.net.ssl.trustStore");
if (path == null)
path = System.getProperty("java.home") + File.separator + "etc"
+ File.separator + "security" + File.separator
+ "cacerts.bks";
connectionConfig.setTruststorePath(path);
connectionConfig.setTruststorePassword("changeit");
//} */

XMPPConnection con = new XMPPConnection(connectionConfig);

SASLXOAuth2Mechanism.java - XOAuth2 机制

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.sasl.SASLMechanism;
import org.jivesoftware.smack.util.Base64;

import de.measite.smack.Sasl;

public class SASLXOAuth2Mechanism extends SASLMechanism
{
static final String AUTHENTICATOR_URL = "http://www.google.com/talk/protocol/auth";
protected String authenticationText = null;
static final String TAG = "SASLXOAuth2Mechanism";

public SASLXOAuth2Mechanism(SASLAuthentication saslAuthentication) {
super(saslAuthentication);
}

@Override
protected String getName()
{
return "X-OAUTH2";
}

@Override
public void authenticate(String username, String host, String password) throws IOException, XMPPException {
this.password = password;
this.authenticationId = username;

StringBuilder credentials = new StringBuilder();
credentials.append("\0");
credentials.append(username);
credentials.append("\0");
credentials.append(password);
authenticationText = Base64.encodeBytes(credentials.toString().getBytes("UTF-8"), Base64.DONT_BREAK_LINES);

String[] mechanisms = { "PLAIN" };
Map<String,String> props = new HashMap<String,String>();
sc = Sasl.createSaslClient(mechanisms, username, "xmpp", host, props, this);
authenticate();
}

protected void authenticate() throws IOException, XMPPException {
// Send the authentication to the server
getSASLAuthentication().send(new XOAuth2AuthMechanism(getName(), authenticationText));
}

/**
* Initiating SASL authentication by select a mechanism.
*/
public class XOAuth2AuthMechanism extends Packet {
final private String name;
final private String authenticationText;

public XOAuth2AuthMechanism(String name, String authenticationText) {
super();
this.name = name;
this.authenticationText = authenticationText;
}

public String toXML() {
StringBuilder stanza = new StringBuilder();
stanza.append("<auth mechanism=\"").append(name);
stanza.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" auth:service=\"oauth2\" xmlns:auth=\"").append(AUTHENTICATOR_URL);
stanza.append("\">");
if (authenticationText != null) {
stanza.append(authenticationText);
}
stanza.append("</auth>");
return stanza.toString();
}
}
}

关于android - (a) 击打 : TLS socket is closed when trying to login with XMPP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14623967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com