简体   繁体   English

如何让离子库信任自签名证书

[英]How do i get ion library to trust self signed certificates

I am trying to connect to REST Service that has to use self signed certificates (it's a Unify PBXs Web Services Interface). 我正在尝试连接到必须使用自签名证书的REST服务(它是Unify PBXs Web服务接口)。 The System will regenerate it's Certificates on Software updates and unless you load a certificate into the system there will always be a self signed one. 系统将在软件更新时重新生成它的证书,除非您将证书加载到系统中,否则将始终存在自签名证书。 When trying to connect with ion the connection is closed because of the self signed certificate (as far as google took me...). 当尝试连接离子时,连接因为自签名证书而关闭(据谷歌带我...)。 What do i need to add to my implementation to make io accept this cert? 我需要添加到我的实现中以使io接受此证书? I am using ion as follows. 我使用离子如下。

Ion.with(context)
     .load(...)
     .asString()
     .setCallback(new FutureCallback<String>() {
           @Override
           public void onCompleted(Exception e, String result) {
           }
      });

You can specify custom SSL Contexts and trust managers to use self signed certificates. 您可以指定自定义SSL上下文和信任管理器以使用自签名证书。

Here's an example from a unit test: 以下是单元测试的示例:

public void testKeys() throws Exception {
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

    ks.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
    kmf.init(ks, "storepass".toCharArray());


    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    KeyStore ts = KeyStore.getInstance(KeyStore.getDefaultType());
    ts.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
    tmf.init(ts);

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

    AsyncHttpServer httpServer = new AsyncHttpServer();
    httpServer.listenSecure(8888, sslContext);
    httpServer.get("/", new HttpServerRequestCallback() {
        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            response.send("hello");
        }
    });

    Thread.sleep(1000);

    AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setSSLContext(sslContext);
    AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setTrustManagers(tmf.getTrustManagers());
    AsyncHttpClient.getDefaultInstance().executeString(new AsyncHttpGet("https://localhost:8888/"), null).get();
}

You'll need to access ion's underlying http client instance as follows: 您需要访问ion的底层http客户端实例,如下所示:

Ion.getDefault(getContext()).getHttpClient().getSSLSocketMiddleware().setTrustManagers(...);
Ion.getDefault(getContext()).getHttpClient().getSSLSocketMiddleware().setSSLContext(...);

The key is a bks key store, bouncy castle. 关键是一个bks钥匙店,充气城堡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的应用程序中信任我所有的自签名证书 - How to trust all my self-signed certificates in my app 如何在android网络库(ION)中使用自签名SSL? - How to use self-signed SSL in android network library (ION)? 如何信任 Android 上的自签名证书? - How to trust self signed certificate on Android? 如何信任 SSL 证书在 android &lt;= 5 上过期 - How to trust SSL certificates with cross-signed root expired on android <= 5 如何将自签名证书添加到 HttpsURLConnection 中的默认证书 - How to ADD self-signed certificates to default certificates in HttpsURLConnection 未找到认证路径的信任锚。 在 android 上使用自签名客户端证书 - Trust anchor for certification path not found. Using self-signed client certificates on android Android 尝试信任自签名证书,出现无对等连接错误 - Android Trying to trust a self signed certificate, get a no peer connection error 在Android 6上由特定CA签署的仅信任证书 - Trust Only Certificates Signed by Specific CA on Android 6 如何获取值data.length()到Ion(HTTP LIB:Library)? - How do I get value data.length() to Ion (HTTP LIB:Library)? 如何使用 React Native 在 Android 上允许自签名证书 - How to allow Self-Signed Certificates on Android with React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM