Android开发环境、tomcat html
总体的步骤就是 java
1. 用keystore生成服务器端所用的密钥,用它配置服务器 android
2.客户端导入其中的公钥,将其添加到信任的证书库中。 web
下面是具体的参考资料。 apache
1.密码学基础(像我这样非科班出身的须要看一下,知其然还得知其因此然)注意:具体的配置可能不同,请找你的tomcat文档,SSL部分。 浏览器
<!-- 不配置APR时 --> <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="conf/cert/tomcat.keystore" keystorePass="password" />
b. 导入公钥。把xxxx.cer放在Android的assets文件夹中,以方便在运行时经过代码读取此证书。 tomcat
获取本地的证书 public static KeyStore getCertificate(Context context) { AssetManager assetManager = context.getAssets(); InputStream ins = null; KeyStore keyStore = null; try { ins = assetManager.open("darrenf.crt"); // 读取证书 CertificateFactory cerFactory = CertificateFactory.getInstance("X.509"); //Certificate的type Certificate cer = cerFactory.generateCertificate(ins); // 建立一个证书库,并将证书导入证书库 //android平台上支持的keystore type好像只有PKCS12,不支持JKS keyStore = KeyStore.getInstance("PKCS12", "BC"); keyStore.load(null, null); keyStore.setCertificateEntry("trust", cer); return keyStore; } catch (IOException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } finally { try { if(ins != null){ ins.close(); } } catch (IOException e) { e.printStackTrace(); } } return keyStore; }
// 链接服务器获取信息 public void connectServer() { // 获取本地证书 KeyStore keystore = CertificateUtils.getCertificate(getContext()); if(keystore == null){ Log.e(TAG, "获取证书错误"); return; } // 把咱的证书库做为信任证书库 SSLSocketFactory socketFactory = null; try { socketFactory = new SSLSocketFactory(keystore); // 容许全部主机 socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } catch (KeyManagementException e) { e.printStackTrace(); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } final Scheme sch = new Scheme("https", socketFactory, 443); Thread thread = new Thread() { public void run() { String path = "https://192.168.16.34:8443/SpringREST/simple/22"; HttpClient mHttpClient = new DefaultHttpClient(); mHttpClient.getConnectionManager().getSchemeRegistry().register(sch); HttpGet httpGet = new HttpGet(path); InputStream inputStream = null; ByteArrayOutputStream baos = null; try { HttpResponse response = mHttpClient.execute(httpGet); StatusLine stateLine = response.getStatusLine(); if (stateLine.getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); inputStream = entity.getContent(); baos = new ByteArrayOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = inputStream.read(buffer)) != -1) { baos.write(buffer, 0, len); } String content = new String(baos.toByteArray()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (baos != null) { baos.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }; thread.start(); }
<!-- 配置使http访问转向https --> <security-constraint> <web-resource-collection> <web-resource-name>SSL</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
为 Tomcat 安装 apr 服务器
http://pengranxiang.iteye.com/blog/1128905 socket
http://blog.sina.com.cn/s/blog_64a52f2a0101g35m.html ide
TOMCAT官方文档
http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration