XFire身份验证

一、创建web services 工程(XFire)

 

二、加入身份验证功能

 

1、首先编写服务端验证类,继承AbstractHandler类

 

package test;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Element;

public class AuthenticationHandler extends AbstractHandler {

public void invoke(MessageContext cfx) throws Exception {
   if (cfx.getInMessage().getHeader() == null) {
    throw new org.codehaus.xfire.fault.XFireFault("请求必须包含验证信息",
      org.codehaus.xfire.fault.XFireFault.SENDER);
   }
   Element token = cfx.getInMessage().getHeader().getChild(
     "AuthenticationToken");
   if (token == null) {
    throw new org.codehaus.xfire.fault.XFireFault("请求必须包含身份验证信息",
      org.codehaus.xfire.fault.XFireFault.SENDER);
   }

   String username = token.getChild("Username").getValue();
   String password = token.getChild("Password").getValue();
   try {
    // 进行身份验证 ,只有abcd@1234的用户为授权用户
    if (username.equals("abcd") && password.equals("1234"))
     // 这语句不显示
     System.out.println("身份验证通过");
    else
     throw new Exception();
   } catch (Exception e) {
    throw new org.codehaus.xfire.fault.XFireFault("非法的用户名和密码",
      org.codehaus.xfire.fault.XFireFault.SENDER);
   }
}
}

 

2、Client构造授权信息

package test;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Element;

public class ClientAuthenticationHandler extends AbstractHandler {

    private String username = null;

    private String password = null;

    public ClientAuthenticationHandler() { 
    }

    public ClientAuthenticationHandler(String username,String password) { 
    this.username = username; 
        this.password = password;
    }

    public void setUsername(String username) { 
        this.username = username; 
    }

    public void setPassword(String password) { 
        this.password = password; 
    }

    public void invoke(MessageContext context) throws Exception {

        //为SOAP Header构造验证信息
        Element el = new Element("header"); 
        context.getOutMessage().setHeader(el); 
        Element auth = new Element("AuthenticationToken"); 
        Element username_el = new Element("Username"); 
        username_el.addContent(username); 
        Element password_el = new Element("Password"); 
        password_el.addContent(password); 
        auth.addContent(username_el); 
        auth.addContent(password_el); 
        el.addContent(auth); 
    } 
}

 

3、修改services.xml为webservice绑定Handler

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service xmlns="http://xfire.codehaus.org/config/1.0">
   <name>Hello</name>
   <namespace>http://test/HelloService</namespace>
   <serviceClass>test.IHello</serviceClass>
   <implementationClass>test.HelloImpl</implementationClass>
   <inHandlers> 
   <handler handlerClass ="test.AuthenticationHandler" ></handler > 
   </inHandlers>
   <style>wrapped</style>
   <use>literal</use>
   <scope>application</scope>
</service>
</beans>

 

4、新建一个ClientTest,用来测试

package test;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.*;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

public class ClientTest {

/**
* @param args
*/
public static void main(String[] args) {
   // TODO Auto-generated method stub
   try {
    Service serviceModel = new ObjectServiceFactory().create(IHello.class);
    IHello service = (IHello) new XFireProxyFactory().create(serviceModel,
        "http://dracom-d1514b82:8080/web_services3/services/Hello");   
    XFireProxy proxy = (XFireProxy)Proxy.getInvocationHandler(service);
    Client client = proxy.getClient();
    //发送授权信息
    client.addOutHandler(new ClientAuthenticationHandler("abcd","1234"));
    //输出调用web services方法的返回信息
    System.out.println(service.getMessage("你好aaa"));
   } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值