Java 要調用遠程Https webservice 必需具用遠程伺服器提供的用戶端信任書及密鑰.將client.keystore和client.truststore拷貝到classes\test目錄下. package test;import org.apache.axis.client.Call;import org.apache.axis.client.Service; public class TestEcVoteNotice { public static void main(String [] args) throws Exception { System.setProperty("javax.net.ssl.keyStore", "test\\client.keystore"); System.setProperty("javax.net.ssl.keyStorePassword", "abc"); System.setProperty("javax.net.ssl.trustStore", "test\\client.truststore"); System.setProperty("javax.net.ssl.trustStorePassword", "abc"); //System.setProperty("javax.net.debug", "ssl"); //System.setProperty("https.protocols", "TLSv1"); //System.setProperty("java.protocol.handler.pkgs","javax.net.ssl"); String endpoint = "https://localhost:" +"8443"+ "/axis/services/EcVoteNotice"; //String endpoint = "http://localhost:" +"8080"+ "/axis/services/EcVoteNotice"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress( new java.net.URL(endpoint) ); call.setOperationName("toStringP"); String res = (String) call.invoke( new Object[] {"Box"} ); call.setOperationName("toString"); String res2 = (String) call.invoke( new Object[] {} ); System.out.println( res+"/"+res2 ); } }在classes目錄下執行.java -cp %AXISCLASSPATH% test.TestEcVoteNotice
WebService 是基於SOAP協議傳輸的,SOAP是以XML檔案形式進行資訊傳輸,是明文,這是不安全的,所以我們可以在WebService加上SSL/HTTPS協議來進行資料轉送
基於Axis的WebService可以很好的實現,在這裡我們使用tomcat伺服器
使用JDK內建的工具建立密匙庫和信任庫。
1)通過使用以下的命令來建立伺服器端的密匙庫:
keytool -genkey -alias Server -keystore server.keystore -keyalg RSA
輸入keystore密碼: strongit
您的名字與姓氏是什嗎?
[Unknown]: Server
您的組織單位名稱是什嗎?
[Unknown]: ec
您的組織名稱是什嗎?
[Unknown]: ec
您所在的城市或地區名稱是什嗎?
[Unknown]: nanchang
您所在的州或省份名稱是什嗎?
[Unknown]: jiangxi
該單位的兩字母國家代碼是什麼
[Unknown]: CN
CN=Server, OU=ec, O=ec, L=beijing, ST=beijing, C=CN 正確嗎?
[否]: y
輸入<Server>的主密碼
(如果和 keystore 密碼相同,按斷行符號):
以上命令執行完成後,將獲得一個名為server.keystore的密匙庫。
2)產生用戶端的信任庫。首先輸出RSA認證:
keytool -export -alias Server -file test_axis.cer -storepass strongit-keystore server.keystore
然後把RSA認證輸入到一個新的信任庫檔案中。這個信任庫被用戶端使用,被用來驗證伺服器端的身份。
keytool -import -file test_axis.cer -storepass changeit -keystore client.truststore -alias serverkey -noprompt
以上命令執行完成後,將獲得一個名為client.truststore的信任庫。
3)同理產生用戶端的密匙庫client.keystore和伺服器端的信任庫server.truststore.方便起見給出.bat檔案
gen-cer-store.bat內容如下:
1 |
set SERVER_DN="CN=Server, OU=ec, O=ec, L=nanchang, S=jiangxi, C=CN" |
2 |
set CLIENT_DN="CN=Client, OU=ec, O=ec, L=nanchang, S=jiangxi, C=CN" |
3 |
set KS_PASS=-storepass strongit
|
4 |
set KEYINFO=-keyalg RSA |
1 |
keytool -genkey -alias Server -dname %SERVER_DN% %KS_PASS% -keystore server.keystore %KEYINFO% -keypass strongit <br> keytool -export -alias Server -file test_axis.cer %KS_PASS% -keystore server.keystore <br> keytool -import -file test_axis.cer %KS_PASS% -keystore client.truststore -alias serverkey -noprompt <br><br> keytool -genkey -alias Client -dname %CLIENT_DN% %KS_PASS% -keystore client.keystore %KEYINFO% -keypass strongit <br> keytool -export -alias Client -file test_axis.cer %KS_PASS% -keystore client.keystore <br> keytool -import -file test_axis.cer %KS_PASS% -keystore server.truststore -alias clientkey -noprompt |
好的,現在我們就有了四個檔案:server.keystore,server.truststore,client.keystore,client.truststore
更改Tomcat的設定檔(server.xml),增加以下部署描述符:(其實裡面有,只是被注釋掉了)
2 |
maxThreads="150"
minSpareThreads="25"
maxSpareThreads="75" |
3 |
enableLookups="false"
disableUploadTimeout="true" |
4 |
acceptCount="100"
scheme="https"
secure="true" |
5 |
clientAuth="true"
keystoreFile="f:\server.keystore"
keystorePass="changeit" |
6 |
truststoreFile="f:\server.truststore"
truststorePass="changeit" |
這裡主要講如何使用JDK內建的工具建立密匙庫和信任庫