我有一個Android應用程序連接到我們託管的SSL Web服務。 Web服務器是apache,擁有自己創建的CA和一個自簽名的SSL證書。Android SSL HTTP請求使用自簽名證書和CA
我已將我們的CA證書導入到安全性的用戶可信證書部分中的Android平板電腦上。
我已經測試訪問Web服務器,可以確認Web服務證書顯示爲有效的(下圖)
這裏是在安全設置的證書:
現在,當我嘗試訪問我的應用程序中的web服務時,我得到了「無對等證書」異常被觸發。
這是簡化了SSL的實現:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// allows network on main thread (temp hack)
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
SchemeRegistry schemeRegistry = new SchemeRegistry();
//schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
schemeRegistry.register(new Scheme("https", newSSLSocketFactory(), 443));
HttpParams params = new BasicHttpParams();
SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);
HttpClient client = new DefaultHttpClient(mgr, params);
HttpPost httpRequest = new HttpPost("https://our-web-service.com");
try {
client.execute(httpRequest);
} catch (Exception e) {
e.printStackTrace(); //
}
}
/*
* Standard SSL CA Store Setup //
*/
private SSLSocketFactory newSSLSocketFactory() {
KeyStore trusted;
try {
trusted = KeyStore.getInstance("AndroidCAStore");
trusted.load(null, null);
Enumeration<String> aliases = trusted.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
X509Certificate cert = (X509Certificate) trusted.getCertificate(alias);
Log.d("", "Alias="+alias);
Log.d("", "Subject DN: " + cert.getSubjectDN().getName());
Log.d("", "Issuer DN: " + cert.getIssuerDN().getName());
}
SSLSocketFactory sf = new SSLSocketFactory(trusted);
sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
return sf;
} catch (Exception e) {
// TODO Auto-generated catch block
throw new AssertionError(e);
}
}
}
while循環剛剛吐出來的證書,我可以看到我自己的CA在日誌中。但我仍然得到「無對等證書」例外。
10-17 18:29:01.234:我/的System.out(4006):沒有對方的證書
我必須以某種方式手動加載我的CA證書在此實現?
有手動試圖在此實現加載了CA證書嗎? – BON
您可以從http://www.startssl.com/(我將它們用於某些應用程序)爲您的域名獲得一個免費和可信的SSL證書,因此您無需處理在每個設備中添加您的CA想要使用您的應用程序。 –