gpt4 book ai didi

android - 使用 AsyncHttpClient android 将 SSL 证书添加到 keystore

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:54 24 4
gpt4 key购买 nike

我想使用 AsyncHttpclient 将 ssl 证书添加到我的 keystore 。现在我接受所有证书,如下面的代码,

  private AsyncHttpClient asyncHttpClient;
private AppPreference mAppPreferences;
private UserPreference mUserPreference;
public CLHttpClient(Context context) {
this.context = context;
MySSLSocketFactory socketFactory = null;
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
socketFactory = new MySSLSocketFactory(trustStore);
} catch (Exception e) {
e.printStackTrace();
}

asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.setTimeout(30 * 1000);
if (socketFactory != null) {
socketFactory.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
asyncHttpClient.setSSLSocketFactory(socketFactory);
}
asyncHttpClient.setMaxRetriesAndTimeout(1, 30000);
asyncHttpClient.setUserAgent("android-async-http-1.4.9");
mAppPreferences = new AppPreference(context);
mUserPreference = new UserPreference(context);
}

我正在接受如下所有证书,

socketFactory.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

但我不想接受所有证书。而不是 ALLOW_ALL_HOSTNAME_VERIFIER 我想限制它接受我的特定证书。你能建议我这样做吗? 我在我的项目中使用编译 'com.loopj.android:android-async-http:1.4.9'

最佳答案

我已经像下面这样更改了我的代码,

private static final String TAG = CLHttpClient.class.getSimpleName();
private Context context;
private AsyncHttpClient asyncHttpClient;
private AppPreference mAppPreferences;
private UserPreference mUserPreference;
public CLHttpClient(Context context) {
this.context = context;
MySSLSocketFactory socketFactory = null;
try {
// KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
// trustStore.load(null, null);
socketFactory = new MySSLSocketFactory(buildKeyStore(context, R.raw.certificate_ca));
} catch (Exception e) {
e.printStackTrace();
}

asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.setTimeout(30 * 1000);
if (socketFactory != null) {
// socketFactory.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
asyncHttpClient.setSSLSocketFactory(socketFactory);
}
asyncHttpClient.setMaxRetriesAndTimeout(1, 30000);
asyncHttpClient.setUserAgent("android-async-http-1.4.9");
mAppPreferences = new AppPreference(context);
mUserPreference = new UserPreference(context);
}

和下面的方法,

 private static KeyStore buildKeyStore(Context context, int certRawResId) {
// init a default key store
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
// read and add certificate authority
Certificate cert = readCert(context, certRawResId);
keyStore.setCertificateEntry("ca", cert);
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return keyStore;
}

private static Certificate readCert(Context context, int certResourceId) {
// read certificate resource
InputStream caInput = context.getResources().openRawResource(certResourceId);

Certificate ca=null;
try {
// generate a certificate
CertificateFactory cf = CertificateFactory.getInstance("X.509");
ca = cf.generateCertificate(caInput);
} catch (CertificateException e) {
e.printStackTrace();
} finally {
try {
caInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return ca;
}

这对我有用。快乐编码...

关于android - 使用 AsyncHttpClient android 将 SSL 证书添加到 keystore ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41777999/

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