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.
新闻热点
疑难解答