Java 與 .NET 的基於 WS-Security的Web Services整合實現(上)
來源:互聯網
上載者:User
services|web
Java 與 .NET 的基於 WS-Security的Web Services整合實現
rottenapple
本文適用讀者:
Web Services開發人員
應具備的知識:
使用過VS.NET2003 +WSE開發過Web Services,會使用Jbuilder9開發簡單的java應用程式。
一:內容簡介
WS-Security描述通過訊息完整性,訊息機密性和單獨訊息認證提供保護品質的SOAP訊息傳遞增強。適用於下列場合:
1. 客戶必須能夠確定訊息來自哪個人並能夠證實發送方就是那個發送方聲稱的發送方。
2. 客戶必須能夠確定被傳送的資料沒有篡改。
本文介紹了如何?基於WS-Security協議的Java用戶端程式與 .net的web services的整合調用。
二:平台及工具
作業系統:win2000 server
軟體:VS.NET2003+WSE1.0 sp1
Jbuilder9
axis-wsse-1.0(axis實現的ws-security)
三:實現
1. 開啟VS.NET2003,建立一個ASP.Net Web Services工程。增加一個名稱為SumService的Web Services頁面,其核心代碼如下:
[SoapRpcMethod(Action="http://www.contoso.com/Rpc",RequestNamespace="http://www.contoso.com/SU",ResponseNamespace="http://www.contoso.com/SU")]
[WebMethod]
public int IntAdd(int a,int b)
{
SoapContext requestContext = HttpSoapContext.RequestContext;
if (requestContext == null)
throw new ApplicationException("Only SOAP requests are permitted.");
return a+b ;
}
2. 使用WSE Setting Tool 設定此Asp Web Services使用WSE功能,並在“安全”選項欄中添加一個密碼提供類(PasswordProvider)用來實現WS-Security的安全認證。同時,選中trace功能用以跟蹤此Web Services的接收到請求SOAP資訊和返回的SOAP資訊。
3. 添加Microsoft.Web.Services 引用,添加一個新的class,命名為PasswordProvider,此類實現了WSE中的IPasswordProvider介面,用來提供WS-Security的使用者身分識別驗證功能。其核心代碼如下:
public class PasswordProvider : IPasswordProvider
{
public PasswordProvider()
{
//
// TODO: Add constructor logic here
//
}
public string GetPassword(UsernameToken token)
{
if (token.Username == “username”)
{
return “password”;
}
else
{
return "love";
}
}
}
至此,一個實現了WS-Security中的UsernameToken的Web Services就基本實現了。此時建議使用.net先開發一個用戶端進行測試,測試成功後再開發相應的java用戶端程式。(如何開發請參見我以前寫的關於WSE的文章或到微軟MSDN上察看)