ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 低调看直播体育app软件下载 >> 移动开发 >> Android https 自定义ssl证书使用

Android https 自定义ssl证书使用

来源:网络整理     时间:2016-02-24     关键词:

本篇文章主要介绍了"Android https 自定义ssl证书使用",主要涉及到方面的内容,对于移动开发感兴趣的同学可以参考一下: Android https自定义ssl证书使用1,服务端配置单向认证,给出server.crt2,通过命令生成bks文件keytool -import -ali...

Android https自定义ssl证书使用

1,服务端配置单向认证,给出server.crt

2,通过命令生成bks文件

keytool -import -alias cert123 -file server.crt -keystore cert123.bks -storetype BKS -providerClass org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath ./bcprov-jdk15on-146.jar -storepass cert160223

keytool使用java自带keytool工具

另外一个重要JCE Provider 必须使用http://www.bouncycastle.org/download/bcprov-jdk15on-146.jar ,而且必须此版本,其他版本会有问题

3,android 中代码配置

import java.io.InputStream;
import java.security.KeyStore;

import android.content.Context;

public class MyHttpClient extends DefaultHttpClient {

 private static Context context;

 public static void setContext(Context context) {
  MyHttpClient.context = context;
 }

 public MyHttpClient(HttpParams params) {
  super(params);
 }

 public MyHttpClient(ClientConnectionManager httpConnectionManager, HttpParams params) {
  super(httpConnectionManager, params);
 }

 @Override
    protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

        // 用我们自己定义的 SSLSocketFactory 在 ConnectionManager 中注册一个 443 端口  
        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");
            // 从资源文件中读取你自己创建的那个包含证书的 keystore 文件

            InputStream in = MyHttpClient.context.getResources().openRawResource(R.raw.codeprojectssl); //这个参数改成你的 keystore 文件名
            try {
                // 用 keystore 的密码跟证书初始化 trusted                            
                trusted.load(in, "这里是你的 keystore 密码".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.STRICT_HOSTNAME_VERIFIER); // 这个参数可以根据需要调整, 如果对主机名的验证不需要那么严谨, 可以将这个严谨程度调低些.
            return sf;
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
}

此时应该就可以了,这是单向认证代码。

以上就介绍了Android https 自定义ssl证书使用,包括了方面的内容,希望对移动开发有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_335686.html

相关图片

相关文章