xml web service 從誕生那天就說自己都么都么好,還津津樂道的說internet也會因此而進入一個新紀元,可5年多來,xml web service并沒有像當初宣揚的那樣火起來,盡管在一些領域之內,也有人牛刀小試,但從整體而言,service還并沒有得到廣泛的應用,原因有很多,有一些來源于目前各大廠商都堅持自己的service標準,不能形成統一,也有對現有的穩定系統不愿進行更改的原因,但還包括web service本身的原因,最明顯的應該是兩個:1) 安全,2)性能。畢業設計的時候,寫的是高性能web service的開發和應用,下面,我想用幾篇文章來闡述一下有關xml web service安全的幾個解決方案。歡迎各位大蝦來砸。
如何解決網絡服務的安全問題,我主要從以下兩個層面進行分析:
1) 確保調用者的合法身份-保證來源的合法
2) 在傳輸中不被非法監聽和篡改。
當然還會有其他方面的安全隱患,希望大家能多多提出,我也好能進一步總結。
如果您想更快的掌握本文提到的技術,您以前必須了解xml web service的工作原理,并且親自開發并部署或者使用過xml web service,只是您并不相信您部署的xml web service是安全的。
本節先介紹一種最為簡單的確保調用者合法的解決方案-將用戶名和密碼附加在soap消息頭部,在服務器端進行用戶名密碼驗證。這種方式從解決了原網絡服務不能針對特定對象產生響應的問題。但因為仍以明文格式
傳輸,所以不能有效地防止信息在傳輸過程中被偷窺,篡改或偽造。
如果您以前已經使用了這種方法,請略過此篇文章,我下篇文章中將講述其他方式,更加合理的解決方案,歡迎您繼續關注。
下面是實現此種解決方案的步驟,請您一步一步來
第一步:首先您需要創建一個xml web service的服務項目,創建方法如下
打開visual studio 2005,在起始頁上點擊創建項目,選擇visual c#中的asp.net web 服務應用程序,輸入項目名稱
第二步:在該項目中創建一個擴展的soapheader對象mysoapheader,如下
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.web.services.protocols;
namespace webservice1
{
public class mysoapheader:soapheader
{
private string _username;
private string _pwd;
/**//// <summary>
/// 用戶名
/// </summary>
public string username
{
get
{
return _username;
}
set
{
_username = value;
}
}
/**//// <summary>
/// 密碼
/// </summary>
public string pwd
{
get
{
return _pwd;
}
set
{
_pwd = value;
}
}
}
}
第三步:創建一個xml web service,另添加一個要求使用soapheader的網絡服務方法
using system;
using system.data;
using system.web;
using system.collections;
using system.web.services;
using system.web.services.protocols;
using system.componentmodel;
namespace webservice1
{
/**//// <summary>
/// service1 的摘要說明
/// </summary>
[webservice(namespace = "http://tempuri.org/")]
[webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
[toolboxitem(false)]
public class service1 : system.web.services.webservice
{
public mysoapheader header = new mysoapheader();
[webmethod]
[soapheader("header")]
public string helloworld()
{
if (header == null)
{
return "您沒有設置soapheader,不能正常訪問此服務!";
}
if (header.username != "jillzhang" || header.pwd != "123456")
{
return "您提供的身份驗證信息有誤,不能正常訪問此服務!";
}
return "hello world";
}
}
}
第四步:創建一個調用xml web service的console應用程序,如下:
using system;
using system.collections.generic;
using system.text;
namespace consoleapplication1
{
class program
{
static void main(string[] args)
{
localhost.service1 ws = new consoleapplication1.localhost.service1();
//ws.mysoapheadervalue = new consoleapplication1.localhost.mysoapheader();
//ws.mysoapheadervalue.username = "jillzhang";
//ws.mysoapheadervalue.pwd = "123456";
console.writeline(ws.helloworld());
}
}
}
下面的分析,對于大家來說,應該是最重要的,很多人不清楚soapheader的工作原理,為什么這么怪異的寫法竟能產生神奇的效果,下面我將不同情形下的soap消息解析出來,大家仔細觀察這個信息,并可以清晰地掌握了soapheader的工作原理了.
首先,先看看沒有設置soapheader的情況下,soap消息為:
-----soap請求 在 2007年05月22日 12時39分40秒
<?xml version="1.0" encoding="utf-8"?><soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"><soap:body><helloworld xmlns="http://tempuri.org/" /></soap:body></soap:envelope>
-----soap響應 在 2007年05月22日 12時39分40秒
<?xml version="1.0" encoding="utf-8"?><soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"><soap:body><helloworldresponse xmlns="http://tempuri.org/"><helloworldresult>您提供的身份驗證信息有誤,不能正常訪問此服務!</helloworldresult></helloworldresponse></soap:body></soap:envelope>
再看看在設置了soapheader之后的soap的請求和響應信息
-----soap請求 在 2007年05月22日 12時42分20秒
<?xml version="1.0" encoding="utf-8"?><soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"><soap:header><mysoapheader xmlns="http://tempuri.org/"><username>jillzhang</username><pwd>123456</pwd></mysoapheader></soap:header><soap:body><helloworld xmlns="http://tempuri.org/" /></soap:body></soap:envelope>
-----soap響應 在 2007年05月22日 12時42分20秒
<?xml version="1.0" encoding="utf-8"?><soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"><soap:body><helloworldresponse xmlns="http://tempuri.org/"><helloworldresult>hello world</helloworldresult></helloworldresponse></soap:body></soap:envelope>
點正是通過這個節點,soapmessage將信息傳遞給了網絡服務端,網絡服務端便可以從中解析出來,并加以處理,從上面的soapmessage中,我們也看出,用戶名和密碼是以明文的格式傳輸的,這樣,soapheader就更像http協議中的cookie了,我們可以參考cookie的使用,來擴展soapheader,讓它變得更加安全些,但總的看來,通過直接設置soapheader的方法提高安全性還是有一定限制的。在安全不是特別重要的應用情形中,推薦采用此種解決方案,因為它方便快捷,靈活易用。
下一節,我將介紹一下,如何獲取soapmessage.
新聞熱點
疑難解答