使用gSOAP开发实例(8) 自定义header实现用户名令牌认证(Usernametoken Authentication)
Posted on 2010-09-06 23:39 roy 阅读(1030) 评论(0) 编辑 收藏 引用 所属分类: C/C++
上一节介绍了怎样实现基本认证(Basic Authentication,以下简称basic方式),望文生义,也就是最简单的用户验证方式,本节稍微深入一些,介绍用户名令牌认证(Usernametoken Authentication,以下简称usernametoken方式)。
Usernametoken方式与basic方式不同的地方,在于后者会把用户名和密码以摘要(digest)的形式,置于HTTP信息头,而前者则把用户名以明文的形式、密码以明文或者摘要的形式,嵌入到一段XML文本中,再置于SOAP消息头当中。
如果使用soapUI调试客户端程序的话,会发现以下是basic方式发出的完整的SOAP消息:
POST https://test2.r-secure.com/Services/ECHO HTTP/0.9
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
User-Agent: Jakarta Commons-HttpClient/3.1
Content-Length: 292
Authorization: Basic VkYtSEstbVNNST0OdlR42EMZaD1BMyE=
Host: test2.r-secure.com
Cookie: $Version=0; MSP2LB=test2.test2f02; $Path=/
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.rsecure.com/ECHO">
<soapenv:Header/>
<soapenv:Body>
<echo:echo>
<echo:EchoMessage>hello</echo:EchoMessage>
</echo:echo>
</soapenv:Body>
</soapenv:Envelope>
以下是usernametoken方式发出的完整的SOAP消息:
POST https://test.r-secure.com/4.0/services/SecureEcho HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
User-Agent: Jakarta Commons-HttpClient/3.1
Host: test.r-secure.com
Content-Length: xxx
<soapenv:Envelope xmlns:echo="http://echo.ws.rsecure.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-32870670" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>roy</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">liang</wsse:Password>
<wsse:Nonce>LX4gh+njbEtCNAtkWkXDYA==</wsse:Nonce>
<wsu:Created>2010-08-11T06:02:25.874Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
<echo:customerId>G06164</echo:customerId>
</soapenv:Header>
<soapenv:Body>
<echo:sendEcho>
<echo:message>hello</echo:message>
</echo:sendEcho>
</soapenv:Body>
</soapenv:Envelope>
其中,加粗部分表示两者的主要区别,红字部分表示各自必不可少的元素。
由此可以看出,usernametoken方式的特点,是在SOAP的header中加入一个Security标签,把usernametoken信息放在这个Security标签里。至于header里的另外一个customerId标签,是该应用自身的额外要求。
gSOAP实现basic方式相对简单,不过实现usernametoken就比较复杂。gSOAP的用户指南推荐使用其自带的插件,在samples/wsse目录下的官方实例也是使用这个插件。但是,我个人认为这种方法比较累赘,自动生成的代码太多,不易看懂,而且似乎非常依赖于wsdl本身的写法。比如,samples/wsse目录下的官方实例含有下列表示SOAP header的结构体,但是,在我实际开发的应用并没有自动产生,即使强行加上去,编译执行通过,运行的时候也出现了相当多的错误。
struct SOAP_ENV__Header
{
struct _wsse__Security *wsse__Security
};
而且,从理论上讲,gSOAP不过是一个框架,定义了从SOAP对象到SOAP消息,以及从SOAP消息到SOAP对象的序列化过程,并且提供了一套与之相适应的API,使用gSOAP开发不过是在其框架范围内调用其API编程。框架的弊端,可想而知,限制了灵活,也限制了方便,更限制了创新。所以,我们可以使用gSOAP编程,但是也许没有必要全部照搬,至少在这个案例中,就没有必要照搬。
我们应有的思路是,既然customerId可以直接写到SOAP header中,那么与之并列的、含有usernametoken的Security也可以直接写到SOAP header中,完全不需要依赖于gSOAP的wsse插件。
与上节一样,基于保密原则,本节案例采用的wsdl同样是经过裁剪和替换的,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://echo.ws.rsecure.com" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://echo.ws.rsecure.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://echo.ws.rsecure.com">
<xsd:element name="sendEcho">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="message" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sendEchoResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="showVersionInformation">
<xsd:complexType/>
</xsd:element>
<xsd:element name="showVersionInformationResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="customerId" type="xsd:string"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sendEchoRequestHeaders">
<wsdl:part name="customerId" element="tns:customerId">
</wsdl:part>
</wsdl:message>
<wsdl:message name="showVersionInformationRequestHeaders">
<wsdl:part name="customerId" element="tns:customerId">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sendEchoResponse">
<wsdl:part name="parameters" element="tns:sendEchoResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="showVersionInformationRequest">
<wsdl:part name="parameters" element="tns:showVersionInformation">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sendEchoRequest">
<wsdl:part name="parameters" element="tns:sendEcho">
</wsdl:part>
</wsdl:message>
<wsdl:message name="showVersionInformationResponse">
<wsdl:part name="parameters" element="tns:showVersionInformationResponse">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="SecureEcho">
<wsdl:operation name="sendEcho">
<wsdl:input name="sendEchoRequest" message="tns:sendEchoRequest">
</wsdl:input>
<wsdl:output name="sendEchoResponse" message="tns:sendEchoResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="showVersionInformation">
<wsdl:input name="showVersionInformationRequest" message="tns:showVersionInformationRequest">
</wsdl:input>
<wsdl:output name="showVersionInformationResponse" message="tns:showVersionInformationResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="SecureEchoHttpBinding" type="tns:SecureEcho">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sendEcho">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sendEchoRequest">
<wsdlsoap:body use="literal"/>
<wsdlsoap:header message="tns:sendEchoRequestHeaders" part="customerId" use="literal">
</wsdlsoap:header>
</wsdl:input>
<wsdl:output name="sendEchoResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="showVersionInformation">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="showVersionInformationRequest">
<wsdlsoap:body use="literal"/>
<wsdlsoap:header message="tns:showVersionInformationRequestHeaders" part="customerId" use="literal">
</wsdlsoap:header>
</wsdl:input>
<wsdl:output name="showVersionInformationResponse">
<wsdlsoap:body use="literal"/>
<
onvif 1.02 c/c++源代码
4星 · 超过85%的资源 需积分: 34 111 浏览量
2011-04-20
09:26:20
上传
评论 1
收藏 1.94MB RAR 举报
南京路人甲
- 粉丝: 8
- 资源: 17
最新资源
- 学生成绩管理系统-C++版本
- 吉林大学离散数学2笔记.pdf
- 通道处理过程的模拟通常涉及对通道处理机制的理解与实现.txt
- Flume进阶-自定义拦截器jar包
- Dubins曲线算法讲解和在运动规划中的使用.pdf
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.dta
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.xlsx
- Reeds+Shepp曲线算法讲解和实现.pdf
- 毕业设计基于SpringBoot+MyBatisPlus+MySQL+Vue的外卖配送信息系统源代码+数据库
- 词向量(Word Embeddings)是自然语言处理(NLP)领域的一种重要技术.txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页