基于Xfire SOAP Header的WebService安全验证

WebSerice是一种开放的web服务,任何人都可以访问,但我们有时候需要考虑只有付费用户才能使用WS,所以,我们就需要对WS加入安全验证机制,当然,可以利用防火墙的IP过滤,web应用的配置从最外层去隔离非法用户,但在内层,我们也可以使用SOAP Header的方式,由客户端发送验证数据,服务端验通过后基WS访问权限

首先根据我的这篇Blog

http://blog.csdn.net/daryl715/archive/2007/07/25/1707161.aspx

配置WS Server和WS Client,其中Client端的测试代码类名由Client改为ClientTest,因为我们要用到Xfire的一个名为Client的类

 

首先我们编写服务端验证类继承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);
              }

          }

 

}

 

下面是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);
     }

 

}

 

 

 

     

你可能感兴趣的:(Web,webservice,防火墙,Blog,SOAP)