【发布时间】:2015-04-17 21:13:09
【问题描述】:
我从 namecheap.com 为我的域 neelo.de 购买了有效的通配符证书。现在我尝试通过 Android 的 WSS 进行连接,但总是得到“javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certificate path not found.”。但我不知道为什么。证书不是自签名的,我从谷歌阅读了文档:https://developer.android.com/training/articles/security-ssl.html
但这些问题只能通过自签名证书调用。我用nodejs试了一下,效果很好:
WebSocket = require "ws"
ws = new WebSocket("wws://api.neelo.de")
ws.on "open", -> console.log "OPEN"
ws.on "close", -> console.log "CLOSE"
ws.on "error", (err) -> console.log err
ws.on "message", (data) -> console.log data
这是我用于加载密钥库的 android 代码:
public WebSocketClient(Context context) throws Exception {
super(new URI("wss://api.neelo.de"), new Draft_76());
this.context = context;
this.messageReceiver = messageReceiver;
configureKeyStore();
}
void configureKeyStore() throws Exception {
Log.d(TAG, "Configure key store");
KeyStore ks = KeyStore.getInstance(STORE_TYPE);
InputStream in = getContext().getResources().openRawResource(R.raw.android_keystore);
ks.load(in, STORE_PASSWORD.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
kmf.init(ks, STORE_PASSWORD.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLSocketFactory factory = sslContext.getSocketFactory();
super.setSocket(factory.createSocket());
}
有人有想法吗?感谢帮助!这个问题已经花了我 3 天的时间......
【问题讨论】:
标签: java android websocket ssl-certificate