美文网首页
android双向认证

android双向认证

作者: TMAC_EAH | 来源:发表于2018-12-18 22:35 被阅读0次
    服务器:php
    客户端:android
    

    操作步骤

    1.生成客户端keystore
    
    keytool -genkeypair -alias client -keyalg RSA -validity 3650 -keypass woaizml -storepass woaizml -keystore client.jks
    
    2.生成服务端keystore
    keytool -genkeypair -alias server -keyalg RSA -validity 3650 -keypass woaizml -storepass woaizml -keystore server.keystore
    
    3.导出客户端证书
    keytool -export -alias client -file client.cer -keystore client.jks -storepass woaizml 
    
    4.导出服务端证书
    keytool -export -alias server -file server.cer -keystore server.keystore -storepass woaizml 
    
    5.重点:证书交换
    将客户端证书导入服务端keystore中,再将服务端证书导入客户端keystore中, 一个keystore可以导入多个证书,生成证书列表。
    生成客户端信任证书库(由服务端证书生成的证书库):
        keytool -import -v -alias server -file server.cer -keystore truststore.jks -storepass woaizml 
    将客户端证书导入到服务器证书库(使得服务器信任客户端证书):
        keytool -import -v -alias client -file client.cer -keystore server.keystore -storepass woaizml
    
    6.生成Android识别的BKS库文件
    用Portecle工具转成bks格式,最新版本是1.10。
    下载链接:https://sourceforge.net/projects/portecle/
    运行protecle.jar将client.jks和truststore.jks分别转换成client.bks和truststore.bks,然后放到android客户端的assert目录下
     
    >File -> open Keystore File -> 选择证书库文件 -> 输入密码 -> Tools -> change keystore type -> BKS -> save keystore as -> 保存即可
     
    这个操作很简单,如果不懂可自行百度。
     
    我在Windows下生成BKS的时候会报错失败,后来我换到CentOS用OpenJDK1.7立马成功了,如果在这步失败的同学可以换到Linux或Mac下操作,
    将生成的BKS拷贝回Windows即可。
    
    7.配置Tomcat服务器
    
    修改server.xml文件,配置8443端口
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="true" sslProtocol="TLS"
               keystoreFile="${catalina.base}/key/server.keystore" keystorePass="123456"
               truststoreFile="${catalina.base}/key/server.keystore" truststorePass="123456"/>
     
    备注: - keystoreFile:指定服务器密钥库,可以配置成绝对路径,本例中是在Tomcat目录中创建了一个名为key的文件夹,仅供参考。 
          - keystorePass:密钥库生成时的密码 
          - truststoreFile:受信任密钥库,和密钥库相同即可 
          - truststorePass:受信任密钥库密码
    
    8.Android App编写BKS读取创建证书自定义的SSLSocketFactory
    
    private final static String CLIENT_PRI_KEY = "client.bks";
    private final static String TRUSTSTORE_PUB_KEY = "truststore.bks";
    private final static String CLIENT_BKS_PASSWORD = "123456";
    private final static String TRUSTSTORE_BKS_PASSWORD = "123456";
    private final static String KEYSTORE_TYPE = "BKS";
    private final static String PROTOCOL_TYPE = "TLS";
    private final static String CERTIFICATE_FORMAT = "X509";
     
    public static SSLSocketFactory getSSLCertifcation(Context context) {
      SSLSocketFactory sslSocketFactory = null;
      try {
        // 服务器端需要验证的客户端证书,其实就是客户端的keystore
        KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);// 客户端信任的服务器端证书
        KeyStore trustStore = KeyStore.getInstance(KEYSTORE_TYPE);//读取证书
        InputStream ksIn = context.getAssets().open(CLIENT_PRI_KEY);
        InputStream tsIn = context.getAssets().open(TRUSTSTORE_PUB_KEY);//加载证书
        keyStore.load(ksIn, CLIENT_BKS_PASSWORD.toCharArray());
        trustStore.load(tsIn, TRUSTSTORE_BKS_PASSWORD.toCharArray());
        ksIn.close();
        tsIn.close();
        //初始化SSLContext
        SSLContext sslContext = SSLContext.getInstance(PROTOCOL_TYPE);
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(CERTIFICATE_FORMAT);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(CERTIFICATE_FORMAT);
        trustManagerFactory.init(trustStore);
        keyManagerFactory.init(keyStore, CLIENT_BKS_PASSWORD.toCharArray());
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); 
     
        sslSocketFactory = sslContext.getSocketFactory();
     
      } catch (KeyStoreException e) {...}//省略各种异常处理,请自行添加
      return sslSocketFactory;
    }
    

    摘抄 https://www.jianshu.com/p/64172ccfb73b

    php配置摘抄

    https://www.cnblogs.com/interdrp/p/4880891.html

    相关文章

      网友评论

          本文标题:android双向认证

          本文链接:https://www.haomeiwen.com/subject/oevrkqtx.html