<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java世界

    學(xué)習(xí)筆記

    常用鏈接

    統(tǒng)計

    積分與排名

    天籟村

    新華網(wǎng)

    雅虎

    最新評論

    基于Xfire SOAP Header的WebService安全驗證教程 .

    WebSerice是一種開放的web服務(wù),任何人都可以訪問,但我們有時候需要考慮只有付費用戶才能使用WS,所以,我們就需要對WS加入安全驗證機制,當然,可以利用防火墻的IP過濾,web應(yīng)用的配置從最外層去隔離非法用戶,但在內(nèi)層,我們也可以使用SOAP Header的方式,由客戶端發(fā)送驗證數(shù)據(jù),服務(wù)端驗通過后基WS訪問權(quán)限

    首先根據(jù)我的這篇Blog

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

    配置WS Server和WS Client,其中Client端的測試代碼類名由Client改為ClientTest,因為我們要用到Xfire的一個名為Client的類 

     

    首先我們編寫服務(wù)端驗證類繼承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的用戶為授權(quán)用戶
                     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發(fā)送授權(quán)信息

     

    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構(gòu)造驗證信息
             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); 
         }
     



    }

    為ClientTest.java加入以下代碼

     XFireProxy proxy = (XFireProxy)Proxy.getInvocationHandler(service);
    Client client = proxy.getClient();
    client.addOutHandler(new ClientAuthenticationHandler("abcd1","1234"));

    等等,還沒有完,修改Services.xm為WS綁定Handler

      

    <?xml version="1.0" encoding="UTF-8"?>


    <beans>
    <service xmlns="http://xfire.codehaus.org/config/1.0">
    <name>HelloService</name>
    <namespace>http://test/HelloService</namespace>
    <serviceClass>test.IHelloService</serviceClass>
    <implementationClass>test.HelloServiceImpl</implementationClass>
     
    <inHandlers> 
     
    <handler  handlerClass ="test.AuthenticationHandler" ></handler > 
     
    </inHandlers>
    </service>
    </beans>


    這樣我們就完成了編碼,下面啟動tomcat,運行客戶端代碼,本文為abcd@1234位授權(quán)用戶,使用abcd@1234,可以正常訪問WS,如果用錯誤帳號,則會有以下異常

     

    Exception in thread "main" org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: 非法的用戶名和密碼
    org.codehaus.xfire.fault.XFireFault: 非法的用戶名和密碼
        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.getUser(Unknown Source)
        at test.ClientTest.main(ClientTest.java:39)

     

    如果不在CientTest加以下增加Heade則會有以下異常

     XFireProxy proxy = (XFireProxy)Proxy.getInvocationHandler(service);
    Client client = proxy.getClient();
     client.addOutHandler(new ClientAuthenticationHandler("abcd1","1234"));

    Exception in thread "main" org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: 請求必須包含驗證信息
    org.codehaus.xfire.fault.XFireFault: 請求必須包含驗證信息
        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.getUser(Unknown Source)
        at test.ClientTest.main(ClientTest.java:35)

    posted on 2013-10-24 16:28 Rabbit 閱讀(3428) 評論(1)  編輯  收藏

    評論

    # re: 基于Xfire SOAP Header的WebService安全驗證教程 . 2014-03-19 17:33 whuang

    寫得很清楚,謝謝  回復(fù)  更多評論   


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲性日韩精品国产一区二区| 国产a级特黄的片子视频免费| 中文字幕免费在线看电影大全 | 亚洲精品永久www忘忧草| 两个人看的www高清免费观看| 伊人久久大香线蕉亚洲| 国产成人亚洲综合一区 | 国产精品高清全国免费观看| 亚洲乱码无人区卡1卡2卡3| 一级成人a毛片免费播放| 亚洲成人免费在线| 一级毛片在线免费播放| 成人特黄a级毛片免费视频| 亚洲欧美日韩综合久久久| 四虎影永久在线高清免费| 一级毛片免费视频网站| 亚洲一区二区三区香蕉| 午夜精品射精入后重之免费观看| 在线免费观看亚洲| 特级无码毛片免费视频尤物| 亚洲国色天香视频| 18观看免费永久视频| 亚洲综合精品第一页| 在线观看视频免费完整版| 麻豆一区二区三区蜜桃免费| 免费无码AV电影在线观看| 色欲aⅴ亚洲情无码AV蜜桃| 成人激情免费视频| 思思久久99热免费精品6| 国产jizzjizz视频全部免费| 一区二区三区在线免费| 亚洲精品电影天堂网| 国产一卡二卡≡卡四卡免费乱码| 免费a级毛片无码a∨免费软件| 亚洲国产成人手机在线电影bd| 99久久国产免费中文无字幕| 亚洲视频在线观看2018| 免费人成视频在线| 国产又黄又爽又大的免费视频 | 亚洲人成依人成综合网| 成人毛片18岁女人毛片免费看|