Home > 类库下载 > java类库 > body text

java keystore 实现ssl双向认证【客户端为php和java】

巴扎黑
Release: 2016-11-09 14:30:51
Original
2048 people have browsed it

1.首先搭建server端环境:

准备工作:tomcat6、jdk7、openssl、javawebservice测试项目一个

2.搭建过程:

参考http://blog.csdn.net/chow__zh/article/details/8998499

 1.1生成服务端证书

keytool -genkey -v -alias tomcat -keyalg RSA -keystore D:/SSL/server/tomcat.keystore -dname "CN=127.0.0.1,OU=zlj,O=zlj,L=Peking,ST=Peking,C=CN" -validity 3650 -storepass zljzlj -keypass zljzlj

 

说明:
keytool 是JDK提供的证书生成工具,所有参数的用法参见keytool –help
-genkey 创建新证书
-v 详细信息
-alias tomcat 以”tomcat”作为该证书的别名。这里可以根据需要修改
-keyalg RSA 指定算法
-keystore D:/SSL/server/tomcat.keystore 保存路径及文件名
-dname "CN=127.0.0.1,OU=zlj,O=zlj,L=Peking,ST=Peking,C=CN" 证书发行者身份,这里的CN要与发布后的访问域名一致。但由于我们是自己发行的证书,如果在浏览器访问,仍然会有警告提示。
-validity 3650证书有效期,单位为天
-storepass zljzlj 证书的存取密码
-keypass zljzlj 证书的私钥
    1.2 生成客户端证书 
    执行命令:
keytool ‐genkey ‐v ‐alias client ‐keyalg RSA ‐storetype PKCS12 ‐keystore D:/SSL/client/client.p12 ‐dname "CN=client,OU=zlj,O=zlj,L=bj,ST=bj,C=CN" ‐validity 3650 ‐storepass client ‐keypass client
说明:
    参数说明同上。这里的-dname 证书发行者身份可以和前面不同,到目前为止,这2个证书可以没有任何关系。下面要做的工作才是建立2者之间的信任关系。
    1.3 导出客户端证书 
    执行命令:
keytool ‐export ‐alias client ‐keystore D:/SSL/client/client.p12 ‐storetype PKCS12 ‐storepass client ‐rfc ‐file D:/SSL/client/client.cer
说明:
-export 执行导出
-file 导出文件的文件路径
    1.4 把客户端证书加入服务端证书信任列表 
    执行命令:
keytool ‐import ‐alias client ‐v ‐file D:/SSL/client/client.cer ‐keystore D:/SSL/server/tomcat.keystore ‐storepass zljzlj
说明:
参数说明同前。这里提供的密码是服务端证书的存取密码。
    1.5 导出服务端证书 
    执行命令:
keytool -export -alias tomcat -keystore D:/SSL/server/tomcat.keystore -storepass zljzlj -rfc -file D:/SSL/server/tomcat.cer
说明:
把服务端证书导出。这里提供的密码也是服务端证书的密码。
    1.6 生成客户端信任列表 
    执行命令:
keytool -import -file D:/SSL/server/tomcat.cer -storepass zljzlj -keystore D:/SSL/client/client.truststore -alias tomcat –noprompt
说明:
让客户端信任服务端证书
2. 配置服务端为只允许HTTPS连接
    2.1 配置Tomcat 目录下的/conf/server.xml 
Xml代码  收藏代码
           maxThreads="150" scheme="https" secure="true" clientAuth="true"    
       sslProtocol="TLS" keystoreFile="D:/SSL/server/tomcat.keystore"    
       keystorePass="zljzlj" truststoreFile="D:/SSL/server/tomcat.keystore"    
       truststorePass="zljzlj" />   
说明:

在server.xml里面这段内容本来是被注释掉的,如果想使用https的默认端口443,请修改这里的port参数。其中的clientAuth="true" 指定了双向证书认证。

2.将client.p12导入浏览器个人证书项中。

此时输入https://127.0.0.1:8443/会出现选择证书,点确定会提示https页面不安全是否继续,点继续。到此服务端搭建完成。

3.java调用server端直接上代码:

package test;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
/**
 * 
 * @author gshen
 *
 */
public class TestEcVoteNotice {
 public static void main(String [] args) throws Exception {  
   System.setProperty("javax.net.ssl.trustStorePassword","zljzlj");    
   System.setProperty("javax.net.ssl.keyStoreType","PKCS12") ;    
   System.setProperty("javax.net.ssl.keyStore","D:/SSL/client/client.p12") ;    
   System.setProperty("javax.net.ssl.keyStorePassword","client") ;          
       System.setProperty("javax.net.debug", "all");
       
     //wsdl地址
String endpoint = "https://192.168.1.146:8443/pro/ws/getInfoService?wsdl";
//http://jarfiles.pandaidea.com/ 搜索axis.jar并下载,Service类在axis.jar
Service service = new Service();
//http://jarfiles.pandaidea.com/ 搜索axis.jar并下载,Call类在axis.jar
Call call = null;
try {
call = (Call) service.createCall();
//设置Call的调用地址
call.setTargetEndpointAddress(new java.net.URL(endpoint));
//根据wsdl中  ,
//
call.setOperationName(new QName("http://ws.task.xm.com/","sayHello"));  
//参数1对应服务端的@WebParam(name = "tableName") 没有设置名称为arg0
call.addParameter("id", XMLType.SOAP_STRING, javax.xml.rpc.ParameterMode.IN);
           //调用方法的返回值
           call.setReturnType(org.apache.axis.Constants.XSD_STRING);  
           //调用用Operation调用存储过程(以服务端的方法为准)
String res = (String) call.invoke(new Object[] {"1"});  //调用存储过程
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
     }  
}
Copy after login

直接命令行运行或右键run as ,server端项目中我直接做了log打印,只要调用就会有打印。执行后

请看附件。

重点来了,接下来是php调用server,php的soapClient只识别DER、PEM或者ENG格式的证书,所以必须要把client.p12转换为php可识别的pem文件,这时用到了openssl,首先进入cmd命令行,敲入以下代码

Java代码

openssl pkcs12 -in D:\SSL\client\client.p12 -out D:\SSL\client\client-cer.pem -clcerts
Copy after login

如果提示openssl命令不识别则是你没安装openssl ,如果执行成功会提示你先输入client.p12的密码,输入后会让你输入导出的cer.pe的密码,输入后大功告成,client-cer.pem生成成功!。

这时上php代码:

Php代码

$params = array('id' => '2');  
  
    $local_cert = "./client-cer.pem";  
    set_time_limit(0);  
    try{  
        //ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache  
        $wsdl='https://192.168.1.146:8443/pro/ws/getInfoService?wsdl';  
    //  echo file_get_contents($wsdl);  
          
        $soap=new SoapClient($wsdl,   
                    array(  
                        'trace'=>true,  
                        'cache_wsdl'=>WSDL_CACHE_NONE,   
                        'soap_version'   => SOAP_1_1,   
                        'local_cert' => $local_cert, //client证书信息  
                        'passphrase'=> 'client', //密码  
                       // 'allow_self_signed'=> true  
                    )  
                );  
        $result=$soap->sayHello($params);  
        $result_json= json_encode($result);  
        $result= json_decode($result_json,true);  
        echo '结果为:' . json_decode($result['return'],true);  
    }catch(Exception $e) {  
        $result['success'] = '0';  
        $result['msg'] = '请求超时';  
        echo $e->getMessage();  
    }  
    echo '>>>>>>>>>>>';
Copy after login

 直接运行,也会出现附件中的结果,打完收工,憋了我整整三天时间,终于搞定了。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!