sve*_*ija 1 ssl android bouncycastle
http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/
我按照这个教程,一切似乎都很好(我没有在路上得到任何错误),但我又得到了
06-24 18:42:31.746: WARN/System.err(14807): javax.net.ssl.SSLException: Not trusted server certificate
06-24 18:42:31.756: WARN/System.err(14807): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
06-24 18:42:31.766: WARN/System.err(14807): Caused by: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
Run Code Online (Sandbox Code Playgroud)
我在http://subdomain.domain.com上有SSL - RapidSSL.我下载了(单个)证书并将其插入密钥库.添加了myHttpClient但又一次,我无法使https工作.
有什么建议?
编辑:在桌面上一切都很好 - 我没有得到任何错误/警告.
例如,尝试http://www.digicert.com/help/ - 粘贴到您网站的URL中,您将看到证书是否已正确安装.通常,为了正确安装证书,您不仅需要安装证书,还需要安装证书颁发机构的中间证书.他们通常不会使用他们的主要证书签署您的证书,而是使用一些中间证书,如果出现任何问题他们可能会失效,并且不像主要证书那样"宝贵" - 这意味着您的证书在链中排名第三:
主权证书 - >中间权威证书 - >您自己的证书
因此,您不仅要告诉您的客户您的证书,还要告诉您的证书.安装说明通常可在您的认证机构帐户中找到.
为实现这一目标,我遵循详细的逐步说明
为TOMCAT配置BouncyCastle
打开D:\ tools\apache-tomcat-6.0.35\conf\server.xml并添加以下条目
在这些更改后重新启动服务器.
MyHttpClient.java
package com.arisglobal.aglite.network;
import java.io.InputStream;
import java.security.KeyStore;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import com.arisglobal.aglite.activity.R;
import android.content.Context;
public class MyHttpClient extends DefaultHttpClient {
final Context context;
public MyHttpClient(Context context) {
this.context = context;
}
@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Register for port 443 our SSLSocketFactory with our keystore to the ConnectionManager
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
}
private SSLSocketFactory newSslSocketFactory() {
try {
// Get an instance of the Bouncy Castle KeyStore format
KeyStore trusted = KeyStore.getInstance("BKS");
// Get the raw resource, which contains the keystore with your trusted certificates (root and any intermediate certs)
InputStream in = context.getResources().openRawResource(R.raw.aglite);
try {
// Initialize the keystore with the provided trusted certificates.
// Also provide the password of the keystore
trusted.load(in, "aglite".toCharArray());
} finally {
in.close();
}
// Pass the keystore to the SSLSocketFactory. The factory is responsible for the verification of the server certificate.
SSLSocketFactory sf = new SSLSocketFactory(trusted);
// Hostname verification from certificate
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return sf;
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何在Activity类中调用上面的代码:
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
HttpResponse response = client.execute(...);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4821 次 |
| 最近记录: |