构建安全的Xml Web Service系列之初探使用Soap头
如何解决网络服务的安全问题,我主要从以下两个层面进行分析:
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消息为:
<?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的请求和响应信息
<?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>
大家可以比较前后两个Soap消息的异同,会发现,加了SoapHeader的请求SoapMessage比没有加的多了一个节
点<soap:Header>正是通过这个节点,SoapMessage将信息传递给了网络服务端,网络服务端便可以从中解析出来,并加以处理,从上面的SoapMessage中,我们也看出,用户名和密码是以明文的格式传输的,这样,SoapHeader就更像Http协议中的Cookie了,我们可以参考Cookie的使用,来扩展SoapHeader,让它变得更加安全些,但总的看来,通过直接设置SoapHeader的方法提高安全性还是有一定限制的。在安全不是特别重要的应用情形中,推荐采用此种解决方案,因为它方便快捷,灵活易用。
转载于:https://www.cnblogs.com/humors/archive/2008/10/16/1312400.html
构建安全的Xml Web Service系列之初探使用Soap头相关推荐
- 构建安全的Xml Web Service系列之如何察看SoapMessage
上一篇文章地址:构建安全的Xml Web Service系列一之初探使用Soap头 (5-22 12:53) 要分析Xml Web Service的安全性,首先要解决的问题是我们能了 ...
- 构建安全的Xml Web Service系列之SSL篇
首先介绍一下SSL, SSL 的英文全称是 "Secure Sockets Layer" ,中文名为 "安全套接层协议层 ",它是网景( Netscape )公 ...
- 什么是 XML Web Service
什么是 XML Web Service 2008-09-10 11:33 XML Web Service 是在 Internet 上进行分布式计算的基本构造块.开放的标准以及对用户和应用程序之间的通信 ...
- XML Web Service 安全性
XML Web Service 安全性 来源: 天极网 XML Web Service 安全吗? 鉴于安全性涉及诸多方面(例如身份验证和授权.数据隐私和完整性等),以及 SOAP 规范中根本没有提及安 ...
- [Axis2与Eclipse整合开发Web Service系列之三] 服务端返回值
前言 在前面的三篇中 [Axis2与Eclipse整合开发Web Service系列之一] 生成Web Service Client(将WSDl 转化成 Java代码) [Axis2与Eclipse整 ...
- [Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端(续)
前言 本篇是承接上一篇: [Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端 在上一篇粗略地介绍了如何使用Top-Down的方式创建 ...
- Web Service 系列 → 第一个 Hello Word
一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intrane ...
- SharePoint Web Service系列: Add或Update其他各种类型的项
在前面,我们讨论了如何Add或Update类型为User的项.因为User类型多少比较特殊.作为SharePoint Web Service系列讨论的结束,我们将讨论各种其他类型的项如何来写. 在SD ...
- [Zend PHP5 Cerification] Lectures -- 4. XML Web Service
XML Well Formed – Single rootlevel tag – Tags must beopened and closed properly – Entities mustbe ...
最新文章
- openssl 开启AES-NI指令集性能增加
- Java Swing中的聊天气泡
- 人口增长模型_未来中国近一半人口将生活在20强城市,这是异想天开还是大势所趋?...
- mvc ajax helpers,ASP.NET MVC 实践系列4-Ajax应用
- ip冲突 scan windows_如何检测IP有冲突 - 卡饭网
- 【Henu ACM Round#19 A】 Vasya the Hipster
- 「镁客早报」小米“10亿赌约”输给格力;SpaceX本周将首次试飞新飞船...
- 二维条码 PDF417 zxing-cpp解码详细过程
- 技术干货大集锦(一)
- 解决 The file will have its original line endings in your working directory
- centos修改镜像源
- Java用户注册手机短信验证码校验功能实现
- GOOGLE卫星地图
- php伪装图片,GIF 图片伪装技术
- 带大家读python数据分析一书(三)
- tomcat开启远程调试功能
- Latex【Error】Reference:Something‘s wrong--perhaps a missing \item. \end{thebibliography} 参考文献报错
- JQuery点名表案例
- 华强北的AirPods耳机谁家比较靠谱?
- 电子词典的实现(一)
热门文章
- typora export to word is excellent
- 虚拟打印机开发日志(一):使用x64 WIN7编译环境编译的完整步骤
- 阿里云人脸属性API,我已经成功了(高兴高兴)
- Elasticsearch分布式一致性原理剖析(三)-Data篇
- solidity mapping of mapping
- PHP实习之路—NO.1(看LINUX、APACHE、MYSQL、PHP文档)
- 看我如何发现Uber合作方网站XXE 0day漏洞并获得9000美元赏金
- python统计httpd 进程的内存占用百分比
- Android测试中被测应用挂了怎么办?
- Rpm包的安装与yum的配置