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

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

rottenapple

4. 打开 Jbuilder9 ,新建一个 java 类,命名为 TestNetService 。并将 axis-wsse-1.0 的 jar 包添加到 Jbuilder 的 jdk 中( Tools->configions jdks->class tab->add )代码如下:

package MyWebServiceJavaClient;

import java.util.Date;

import java.text.DateFormat;

import org.apache.axis.MessageContext;

import org.apache.axis.message.*;

import org.apache.axis.client.*;

import org.apache.axis.utils.*;

import javax.xml.namespace.QName;

import java.lang.Integer;

import javax.xml.rpc.ParameterMode;

import net.vitale.filippo.axis.handlers.WsseClientHandler;

/**

1<p>Title: </p>
1<p>Description: </p>
1<p>Copyright: Copyright (c) 2004</p>
1<p>Company: </p>
  • @author not attributable

  • @version 1.0

*/

public class TestNetService {

static String usernameS = null;

static String passwordS = null;

public TestNetService() {

}

public static void main(String[] args) {

try {

Integer i = new Integer(2);

Integer j = new Integer(2);

String endpoint="http://localhost/MyServices/WebServiceTest/SumService.asmx";

Service service = new Service();

Call call = (Call)service.createCall();

call.setTargetEndpointAddress(new java.net.URL(endpoint));

call.setOperationName(new QName("http://www.contoso.com/SU","IntAdd"));

call.addParameter("a",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);

call.addParameter("b",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);

call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);

call.setUseSOAPAction(true);

call.setSOAPActionURI("http://www.contoso.com/Rpc");

//add a user token

usernameS =”username”;

passwordS = "love";

call.setUsername(usernameS);

call.setPassword(passwordS);

call.setProperty(WsseClientHandler.PASSWORD_OPTION, WsseClientHandler.PASSWORD_DIGEST_WITH_NONCE);

call.setClientHandlers(new WsseClientHandler(), null);

Integer k = (Integer)call.invoke(new Object[]{i,j});

System.out.println( "result is " + k.toString() + ".");

}

catch (org.apache.axis.AxisFault e)

{

if (e.getFaultCode().toString() .equals("{http://schemas.xmlsoap.org/ws/2002/07/secext}FailedAuthentication"))

System.err.println("The usernameToken and password aren't right! ");

else {

System.err.println(e.getFaultCode().toString());

}

}

catch(Exception e)

{

System.err.println(e.toString()) ;

}

}

}

5. 编译并运行这个 java 程序 , 执行结果如下:

The username and password aren't right!

可以看到,在 Web Services 中的 PasswordProvider 类中, GetPassWord ()方法是用来返回相应的密码。在上面的示例中,由于 username=”usename” ,因此 GetPassWord 返回 ”password” ,而 java 传递过来的密码是 ”love” ,因此两者不符合。系统会抛出异常,我们在 Java 中进行捕获,并显示自己的提示信息。

6. 修改部分 Java 代码并运行

将 passwordS = "love”; 替换成 passwordS = "password"; 重新编译运行,结果如下:

result is 4.

这样,可以看到 Java 客户端发送的用户名和密码在 Web Services 得到了认证,并执行了 IntAdd 方法,返回正确的计算结果。至此,一个简单的基于 WS-Security 的 Java 客户端与 .Net Web Services 的互连就基本实现了。

四:可扩展的地方

1. Java 端的用户名,密码是可以从 UI 界面上得到。

2. Java 端的密码传输方式用三种,可以自由选择。

3. Web Service 端的密码可以从数据库, AD, 文件等处获得。

4. Web Service 端的验证错误后的异常信息可以自己制定。

5. 可以使用 X.509 作为证书,添加第三方数字签名认证(目前 asix-wsse1.0 没有实现)

6. 有兴趣的朋友可以看看 axis-wsse-1.0 的源代码,很简单,就一个文件,不过我看起来很费劲,因为调用了很多 axis 里面的东西,那个我也不熟悉,所以就看不明白,呵呵。

五:不足之处

1 . X.509 是包含在 WS-Security 中的,但是目前没有基于 java 的开源实现。 IBM 的 WebSphere 有相应的实现。

2 .我个人认为这也是基于 Web Services 的不同平台间相互调用的一个大的问题就是异常的处理。目前我个人觉得好的方法是纪录到日志中。如果想捕获不同系统提供的异常信息的确是一个困难的事情,不知道谁还有好的方法。我发现在网上介绍 Web Services 的文章书籍中很少有介绍这方面的东西,也许大家都不熟悉也就不敢乱写了(出了我之外)。

参考:

asix-wsse-1.0:http://sourceforge.net/projects/axis-wsse/

wse1.0:http://msdn.microsoft.com/webservices/building/wse/default.aspx

本人能力有限,希望不要误人子弟,有错误大家及时指出来,或者通过 email 联系

[email protected](MSN)

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