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上察看)
|