基于Xfire自带示例的安全校验

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 02:46   11   0

网上关于Xfire的资料越来越多,关于Xfire实现WS-Security的文章也不少。本文基于Xfire自带示例简单介绍Xfire是如何实现WS-Security的。

Xfire的官方网站下载xfire-src-1.2.6,打开目录下的examples/book,将其导入工程(eclipse),目录结构如图:

部署web工程到tomcat目录,运行tomcat,启动服务器端。(这里步骤不做详细说明)。

eclipse中,运行org.codehaus.xfire.demo.BookServiceTest.java,运行正常,部署成功。

注意:查看BookServiceTest.java的源代码会发现它是采用本地的方式来调用web服务,我们现在试着把它改变为链接式的调用。

编写hong_XfireClientFactory.java,代码如下:

/*

package org.codehaus.xfire.demo;

public class hong_XfireClientFactory {

}*/

package org.codehaus.xfire.demo;

import java.net.MalformedURLException;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.Service;

import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import org.springframework.util.Assert;

public class hong_XfireClientFactory {

private static XFireProxyFactory serviceFactory = new XFireProxyFactory();

private static final Log log = LogFactory.getLog(hong_XfireClientFactory.class);

private hong_XfireClientFactory() {

}

public static <T> T getClient(String serviceURL, Class<T> serviceClass) {

Assert.notNull(serviceURL);

Assert.notNull(serviceClass);

Service serviceModel = new ObjectServiceFactory().create(serviceClass);

try {

return (T) serviceFactory.create(serviceModel, serviceURL);

} catch (MalformedURLException e) {

log.error(e.getMessage(), e);

return null;

}

}

}

编写hong_test.java,代码如下:

package org.codehaus.xfire.demo;

import org.codehaus.xfire.demo.BookService;

import org.codehaus.xfire.demo.hong_XfireClientFactory;

import org.codehaus.xfire.demo.handlers.OutHeaderHandler;

import org.codehaus.xfire.client.XFireProxy;

import org.codehaus.xfire.client.*;

import java.lang.reflect.Proxy;

public class hong_test {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String serviceURL = "http://localhost:8080/book-demo/services/BookService";

try {

BookService service = hong_XfireClientFactory.getClient(serviceURL, BookService.class);

//-----------在报头加入信息,供安全校验---------------------

XFireProxyproxy = (XFireProxy)Proxy.getInvocationHandler(service);

Client client = proxy.getClient();

client.addOutHandler(new OutHeaderHandler());

//---------------------------------

System.out.println("Book with ISBN '0123456789': " + service.findBook("0123456789").getTitle() + "");

} catch (Exception e) {

e.printStackTrace();

}

}

}

执行hong_test.java文件,成功!

注意,这里的调用添加了如下代码:

//-----------在报头加入信息,供安全校验---------------------

XFireProxyproxy = (XFireProxy)Proxy.getInvocationHandler(service);

Client client = proxy.getClient();

client.addOutHandler(new OutHeaderHandler());

//---------------------------------

这代码就是对报头添加信息,让CheckVersionHandler类(源码自带)进行校验,如果没有这段代码,运行后会抛出:

org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: Missing SOAP header

org.codehaus.xfire.fault.XFireFault: Missing SOAP header

at org.codehaus.xfire.fault.Soap11FaultSerializer.readMessage(Soap11FaultSerializer.java:31)

at org.codehaus.xfire.fault.SoapFaultSerializer.readMessage(SoapFaultSerializer.java:28)

at org.codehaus.xfire.soap.handler.ReadHeadersHandler.checkForFault(ReadHeadersHandler.java:111)

at org.codehaus.xfire.soap.handler.ReadHeadersHandler.invoke(ReadHeadersHandler.java:67)

at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)

at org.codehaus.xfire.client.Client.onReceive(Client.java:406)

at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:139)

at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)

at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)

at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)

at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)

at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)

at org.codehaus.xfire.client.Client.invoke(Client.java:336)

at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)

at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)

at $Proxy0.findBook(Unknown Source)

at org.codehaus.xfire.demo.hong_test.main(hong_test.java:26)

就是代表验证不合法,拒绝提供服务。

以上是个人使用Xfire的经验,希望能给给学习Xfire的朋友提供一些帮助

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP