Java 與 .NET 的基于 WS-Security的Web Services集成實現(下)
2024-07-10 13:01:44
供稿:網友
java 與 .net 的基于 ws-security的web services集成實現(下)
rottenapple
4. 打開jbuilder9 ,新建一個java 類,命名為testnetservice。并將axis-wsse-1.0的jar包添加到jbuilder的jdk中(tools->configions jdks->class tab->add)代碼如下:
package mywebservicejavaclient;
import java.util.date;
import java.text.dateformat;
import org.apache.axis.messagecontext;
import org.apache.axis.message.*;
import org.apache.axis.client.*;
import org.apache.axis.utils.*;
import javax.xml.namespace.qname;
import java.lang.integer;
import javax.xml.rpc.parametermode;
import net.vitale.filippo.axis.handlers.wsseclienthandler;
/**
* <p>title: </p>
* <p>description: </p>
* <p>copyright: copyright (c) 2004</p>
* <p>company: </p>
* @author not attributable
* @version 1.0
*/
public class testnetservice {
static string usernames = null;
static string passwords = null;
public testnetservice() {
}
public static void main(string[] args) {
try {
integer i = new integer(2);
integer j = new integer(2);
string endpoint="http://localhost/myservices/webservicetest/sumservice.asmx";
service service = new service();
call call = (call)service.createcall();
call.settargetendpointaddress(new java.net.url(endpoint));
call.setoperationname(new qname("http://www.contoso.com/su","intadd"));
call.addparameter("a",org.apache.axis.encoding.xmltype.xsd_date,javax.xml.rpc.parametermode.in);
call.addparameter("b",org.apache.axis.encoding.xmltype.xsd_date,javax.xml.rpc.parametermode.in);
call.setreturntype(org.apache.axis.encoding.xmltype.xsd_int);
call.setusesoapaction(true);
call.setsoapactionuri("http://www.contoso.com/rpc");
//add a user token
usernames =”username”;
passwords = "love";
call.setusername(usernames);
call.setpassword(passwords);
call.setproperty(wsseclienthandler.password_option, wsseclienthandler.password_digest_with_nonce);
call.setclienthandlers(new wsseclienthandler(), null);
integer k = (integer)call.invoke(new object[]{i,j});
system.out.println( "result is " + k.tostring() + ".");
}
catch (org.apache.axis.axisfault e)
{
if (e.getfaultcode().tostring() .equals("{http://schemas.xmlsoap.org/ws/2002/07/secext}failedauthentication"))
system.err.println("the usernametoken and password aren't right! ");
else {
system.err.println(e.getfaultcode().tostring());
}
}
catch(exception e)
{
system.err.println(e.tostring()) ;
}
}
}
5. 編譯并運行這個java程序,執行結果如下:
the username and password aren't right!
可以看到,在web services中的passwordprovider類中,getpassword()方法是用來返回相應的密碼。在上面的示例中,由于username=”usename”,因此getpassword返回”password”,而java傳遞過來的密碼是”love”,因此兩者不符合。系統會拋出異常,我們在java中進行捕獲,并顯示自己的提示信息。
6. 修改部分java代碼并運行
將passwords = "love”;替換成passwords = "password";重新編譯運行,結果如下:
result is 4.
這樣,可以看到java客戶端發送的用戶名和密碼在web services得到了認證,并執行了intadd方法,返回正確的計算結果。至此,一個簡單的基于ws-security的java 客戶端與.net web services的互連就基本實現了。
四:可擴展的地方
1. java端的用戶名,密碼是可以從ui界面上得到。
2. java端的密碼傳輸方式用三種,可以自由選擇。
3. web service端的密碼可以從數據庫,ad,文件等處獲得。
4. web service端的驗證錯誤后的異常信息可以自己制定。
5. 可以使用x.509作為證書,添加第三方數字簽名認證(目前asix-wsse1.0沒有實現)
6. 有興趣的朋友可以看看axis-wsse-1.0的源代碼,很簡單,就一個文件,不過我看起來很費勁,因為調用了很多axis里面的東西,那個我也不熟悉,所以就看不明白,呵呵。
五:不足之處
1.x.509是包含在ws-security中的,但是目前沒有基于java的開源實現。ibm的websphere有相應的實現。
2.我個人認為這也是基于web services的不同平臺間相互調用的一個大的問題就是異常的處理。目前我個人覺得好的方法是紀錄到日志中。如果想捕獲不同系統提供的異常信息的確是一個困難的事情,不知道誰還有好的方法。我發現在網上介紹web services的文章書籍中很少有介紹這方面的東西,也許大家都不熟悉也就不敢亂寫了(出了我之外)。
中國最大的web開發資源網站及技術社區,