为什么80%的码农都做不了架构师?>>>
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); } } }
此时应该就可以了,这是单向认证代码。
转载于:https://my.oschina.net/lao4/blog/620559
- 点赞
- 收藏
- 分享
-
- 文章举报
原文始发于:Android https 自定义ssl证书使用
主题测试文章,只做测试使用。发布者:sys234,转转请注明出处:http://www.cxybcw.com/72692.html