免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2535 | 回复: 0
打印 上一主题 下一主题

web services xfire安全 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-01 15:55 |只看该作者 |倒序浏览
基于Xfire自带示例的安全校验
网上关于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 getClient(String serviceURL, Class 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);
           //-----------在报头加入信息,供安全校验---------------------
           XFireProxy proxy = (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文件,成功!
    注意,这里的调用添加了如下代码:
           //-----------在报头加入信息,供安全校验---------------------
           XFireProxy proxy = (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的朋友提供一些帮助


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/47207/showart_1070622.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP