Java 与 .NET 的基于 WS-Security的Web Services集成实现(上)

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

Published At
Categories with Web编程
Tagged with
comments powered by Disqus